0% found this document useful (0 votes)
3 views

06 Queues

Uploaded by

shajlala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

06 Queues

Uploaded by

shajlala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Queues

CS 302 – Data Structures


Section 5.3
What is a queue?
• It is an ordered group of homogeneous items
• Queues have two ends:
– Items are added at one end.
– Items are removed from the other end.
• FIFO property: First In, First Out
– The item added first is also removed first
Queue Implementation
template<class ItemType> private:
class QueueType { int front;
public: int rear;
QueueType(int); ItemType* items;
~QueueType(); int maxQue;
void MakeEmpty(); };
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);
Implementation issues
• Optimize memory usage.
• Conditions for a full or empty queue.
• Initialize front and rear.
linear array circular array
Optimize memory usage
Full/Empty queue conditions
“Make front point to the element preceding the front
element in the queue!”

NOW!
Initialize front and rear
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::QueueType(int max)
{
maxQue = max + 1;
front = maxQue - 1; O(1)
rear = maxQue - 1;
items = new ItemType[maxQue];
}
Queue Implementation (cont.)

template<class ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items; O(1)
}
Queue Implementation (cont.)

template<class ItemType>
void QueueType<ItemType>::MakeEmpty()
{
front = maxQue - 1; O(1)
rear = maxQue - 1;
}
Queue Implementation (cont.)
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
return (rear == front);
} O(1)

template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
return ( (rear + 1) % maxQue == front);
}
O(1)
Enqueue (ItemType newItem)
• Function: Adds newItem to the rear of the
queue.
• Preconditions: Queue has been initialized
and is not full.
• Postconditions: newItem is at rear of queue.
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Enqueue
(ItemType newItem)
{
rear = (rear + 1) % maxQue;
items[rear] = newItem; O(1)
}
Dequeue (ItemType& item)
• Function: Removes front item from queue
and returns it in item.
• Preconditions: Queue has been initialized
and is not empty.
• Postconditions: Front element has been
removed from queue and item is a copy of
removed element.
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Dequeue
(ItemType& item)
{
front = (front + 1) % maxQue;
O(1)
item = items[front];
}
Queue overflow

• The condition resulting from trying to add


an element onto a full queue.
if(!q.IsFull())
q.Enqueue(item);
Queue underflow

• The condition resulting from trying to


remove an element from an empty queue.
if(!q.IsEmpty())
q.Dequeue(item);
Example: recognizing palindromes
• A palindrome is a string that reads the same
forward and backward.

Able was I ere I saw Elba


Example: recognizing palindromes

(1) Read the line of text into both a stack and a queue.

(2) Compare the contents of the stack and the queue


character-by-character to see if they would produce the
same string of characters.
Example: recognizing palindromes
#include <iostream.h>
#include <stdio.h>
for (int i=0;i<str.length();i++) {
#include <ctype.h> ch = str[i];
#include <string>
#include "stacktype.h"
if(isalpha(ch)) {
#include "queue.h"

using namespace std;


if(!s.IsFull())
s.Push(toupper(ch));
int main()
{
StackType<char> s; if(!q.IsFull())
QueueType<char> q;
q.Enqueue(toupper(ch));
string str("talat"); }
char ch;
}
char sItem, qItem;
int mismatches = 0;
Example: recognizing palindromes
while( (!q.IsEmpty()) && (!s.IsEmpty()) ) {

s.Pop(sItem);
q.Dequeue(qItem);

if(sItem != qItem)
++mismatches;
}
if (mismatches == 0)
cout << "That is a palindrome" << endl;
else
cout << "That is not a palindrome" << endl;

return 0;
}
Exercise 37 (page 337): Implement a client
function that returns the number of items in a
queue. The queue is unchanged.

int Length(QueueType& queue)


Function: Determines the number of items in the
queue.
Precondition: queue has been initialized.
Postconditions: queue is unchanged

You may not assume any knowledge of how the queue


is implemented.
int Length(QueType& queue)
{
QueType tempQ;
How would you implement it
ItemType item;
as member function?
int length = 0;
while (!queue.IsEmpty())
{
queue.Dequeue(item);
f r
tempQ.Enqueue(item);
}
r
while (!tempQ.IsEmpty()) f
{
tempQ.Dequeue(item);
queue.Enqueue(item); rear - front maxQue – (front – rear)
length++; What would be the time
} requirements in this case
return length; using big-O?
}

You might also like