Queues
By Mansoor Ahmad
What is a queue?
• It is an ordered group of homogeneous items of
elements.
• Queues have two ends:
– Elements are added at one end.
– Elements are removed from the other end.
• The element added first is also removed first
(FIFO: First In, First Out).
Queue Specification
• Definitions: (provided by the user)
– MAX_ITEMS: Max number of items that might be on
the queue
– ItemType: Data type of the items on the queue
• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Enqueue (ItemType newItem)
– Dequeue (ItemType& item)
Implementing Queue
Using linked List:
front
2
5
7
1 1 7 5 2
front
rear rear
Implementing Queue
Using linked List:
front
2
5
7
1 1 7 5 2
front
rear rear
front
2
5
7 1 7 5 2
front
rear rear
dequeue()
Implementing Queue
Using linked List:
front
2
5
7
1 1 7 5 2
front
rear rear
front
2
5
7 9
7 5 2
front
rear rear
enqueue(9)
9
Implementing Queue
int dequeue()
{
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
}
void enqueue(int x)
{
Node* newNode = new Node();
newNode->set(x);
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
}
Implementing Queue
int front()
{
return front->get();
}
int isEmpty()
{
return ( front == NULL );
}
Queue using Linked List
Queue using Linked List
Queue using Linked List
Queue using Linked List Ex 2
Queue using Linked List Ex 2
Queue using Array
If we use an array to hold queue elements,
both insertions and removal at the front
(start) of the array are expensive.
This is because we may have to shift up to
“n” elements.
For the stack, we needed only one end; for
queue we need both.
To get around this, we will not shift upon
removal of an element.
Queue using Array
front
2
5
7
1
rear
6
5 7
0
0 1 3
2 4
front
1 7 5 2
3
rear
Queue using Array
front
2
5
7
1
rear
6
5 7
0
0 1 3
2 4
front
1 7 5 2
4
rear
enqueue(6)
6
6
Queue using Array
front
2
5
7
1
rear
6
5 7
0
0 1 3
2 4
front
1 7 5 2
5
rear
enqueue(8)
6
6
8
8
Queue using Array
front
2
5
7
rear
6
5 7
1
0 1 3
2 4
front
7 5 2
5
rear
dequeue()
6
6
8
8
Queue using Array
front
2
5
rear
6
5 7
2
0 1 3
2 4
front
5 2
5
rear
dequeue()
6
6
8
8
Queue using Array
front
2
5
rear
6
5 7
2
0 1 3
2 4
front
5 2
7
rear
enqueue(9)
enqueue(12)
6
6
8
8
9
9
12
12
enqueue(21) ??
Queue using Array
We have inserts and removal running in
constant time but we created a new
problem.
Cannot insert new elements even though
there are two places available at the start
of the array.
Solution: allow the queue to “wrap
around”.
Queue using Array
Basic idea is to picture the array as a
circular array.
front
2
5
rear
2
front
7
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
2
6
8
9
12
Queue using Array
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
front
2
5
rear
2
front
0
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
2
6
8
9
12
enqueue(21)
21
21
8
size
7
noElements
Queue using Array
int isFull()
{
return noElements == size;
}
int isEmpty()
{
return noElements == 0;
}
front
2
5
rear
2
front
1
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
2
6
8
9
12
enqueue(7)
21
21
8
size
8
noElements
7
7
Queue using Array
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
front rear
4
front
1
rear
6 8 9 12
6
5
7
0 1
3
2
4
6
8
9
12
dequeue()
21
21
8
size
6
noElements
7
7
Code of Queue using Array
Code of Queue using Array
Code of Queue using Array Ex2
Code of Queue using Array Ex2
Implementation issues
• Implement the queue as a circular
structure.
• How do we know if a queue is full or
empty?
• Initialization of front and rear.
• Testing for a full or empty queue.
Make front point to the element preceding the front
element in the queue (one memory location will be
wasted).
Initialize front and rear
Queue is empty
now!!
rear == front
Queue Implementation
template<class ItemType>
class QueueType {
public:
QueueType(int);
QueueType();
~QueueType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);
private:
int front;
int rear;
ItemType* items;
int maxQue;
};
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::QueueType(int
max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::
MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
}
Queue Implementation (cont.)
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
return (rear == front);
}
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
return ( (rear + 1) % maxQue == front);
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Enqueue
(ItemType newItem)
{
rear = (rear + 1) % maxQue;
items[rear] = newItem;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Dequeue
(ItemType& item)
{
front = (front + 1) % maxQue;
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
• We will read the line of text into both a stack
and a queue.
• 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
Example: recognizing palindromes
#include <iostream.h>
#include <ctype.h>
#include "stack.h"
#include "queue.h“
int main()
{
StackType<char> s;
QueType<char> q;
char ch;
char sItem, qItem;
int mismatches = 0;
cout << "Enter string: " << endl;
while(cin.peek() != 'n') {
cin >> ch;
if(isalpha(ch)) {
if(!s.IsFull())
s.Push(toupper(ch));
if(!q.IsFull())
q.Enqueue(toupper(ch));
}
}
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;
}
Example: recognizing palindromes
Case Study: Simulation
• Queuing System: consists of servers and
queues of objects to be served.
• Simulation: a program that determines how
long items must wait in line before being
served.
Case Study: Simulation (cont.)
• Inputs to the simulation:
(1) the length of the simulation
(2) the average transaction time
(3) the number of servers
(4) the average time between job
arrivals
Case Study: Simulation (cont.)
• Parameters the simulation must vary:
(1) number of servers
(2) time between arrivals of items
• Output of simulation: average wait time.
Exercises (Chapt 4)
• 26, 29-34, 39-41, 46, 47.

Queues in C++ detailed explanation and examples .ppt