Queues: CS 302 - Data Structures Sections 5.3 and 5.4
Queues: CS 302 - Data Structures Sections 5.3 and 5.4
Array-based
Linked-list-based
Array-based Implementation
template<class ItemType> private:
class QueueType { int front, rear;
public: ItemType* items;
QueueType(int); int maxQue;
~QueueType(); };
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
Array-based 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];
}
Array-based Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items; O(1)
}
Array-based Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::MakeEmpty()
{
front = maxQue - 1; O(1)
rear = maxQue - 1;
}
Array-based 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 overflow
if(!q.IsFull())
q.Enqueue(item);
Array-based 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 underflow
if(!q.IsEmpty())
q.Dequeue(item);
Array-based Queue
Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Dequeue
(ItemType& item)
{
front = (front + 1) % maxQue;
O(1)
item = items[front];
}
Linked-list-based
Implementation
• Allocate memory for each new element
dynamically
• Link the queue elements together
• Use two pointers, qFront and qRear, to
mark the front and rear of the queue
A “circular” linked queue design
qRear = NULL
newNode
Enqueue
template <class ItemType>
void QueueType<ItemType>::Enqueue(ItemType
Enqueue
newItem)
{
NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;
newNode->info = newItem;
newNode->next = NULL;
if(qRear == NULL) // special case
qFront = newNode; O(1)
else
qRear->next = newNode;
qRear = newNode;
}
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.
Dequeue (queue contains more
than one elements)
Special Case: queue contains a
single element
• We need to reset qRear to NULL also
qFront After dequeue:
Node qFront = NULL
qRear qRear = NULL
Dequeue
template <class ItemType>
void QueueType<ItemType>::Dequeue(ItemType&
Dequeue item)
{
NodeType<ItemType>* tempPtr;
tempPtr = qFront;
item = qFront->info;
qFront = qFront->next; O(1)
if(qFront == NULL) // special case
qRear = NULL;
delete tempPtr;
}
qRear, qFront - revisited
• Are the relative positions of qFront and
qRear important?
What about
this?
qRear, qFront - revisited
• Are the relative positions of qFront and
qRear important?
Hard to
dequeue!
Other Queue functions
template<class ItemType>
QueueType<ItemType>::QueueType()
{
qFront = NULL;
qRear = NULL; O(1)
}
template<class ItemType>
void QueueType<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while(qFront != NULL) {
tempPtr = qFront; O(N)
qFront = qFront->next;
delete tempPtr;
}
qRear=NULL;
}
Other Queue functions (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
MakeEmpty();
}
O(N)
Other Queue functions (cont.)
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
return(qFront == NULL);
}
O(1)
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
NodeType<ItemType>* ptr;
(1) Read the line of text into both a stack and a queue.
{ if(!s.IsFull())
StackType<char> s; s.Push(toupper(ch));
QueType<char> q; if(!q.IsFull())
char ch; q.Enqueue(toupper(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: Implement a client function that
returns the number of items in a queue. The queue
is unchanged.