Queue
Queue
Queue
Queue
Rear Front
The Queue Operation
$ $
Front
Rear
The Dequeue Operation
Front
Rear
Example
Operation Content of queue
Enqueue(B) B
Enqueue(C) B, C
Dequeue() C
Enqueue(G) C, G
Enqueue (F) C, G, F
Dequeue() G, F
Enqueue(A) G, F, A
Dequeue() F, A
Array Implementation of Queue
Analysis:
Consider the following structure:
int Num[MAX_SIZE];
We need to have two integer variables that tell:
the index of the front element
the index of the rear element
We also need an integer variable that tells:
the total number of data in the queue
int FRONT =-1,REAR =-1;
int QUEUESIZE=0;
Array Implementation of Queue
4 8 6
An array of integers
to implement a
queue of integers
a) Simple Array Implementation of Queue
4 8 6
Front Rear
To dequeue data from the queue
check if there is data in the queue
QUEUESIZE > 0 ?
Yes:
Copy the data in Num[FRONT]
Increment FRONT
Decrement QUEUESIZE
No:
Queue Underflow
A Dequeue Operation
2 last
4 8 6
Front Rear
To enqueue data to the queue
check if there is space in the queue
REAR<MAX_SIZE-1 ?
Yes:
Increment REAR
Store the data in Num[REAR]
Increment QUEUESIZE
FRONT = = -1?
Yes:
Increment FRONT
No:
Queue Overflow
An Enqueue Operation
3 last
8 6 2
Front Rear
b) Circular Array Implementation of Queue
2 6 1
Front Rear
An Enqueue Operation
0 last
4 2 6 1
Rear
Front
Linked List Implementation
Linked List Implementation
13 10 15
nul
l
head_ptr
tail_ptr
Types of Queue
Deque (pronounced as Deck)
Front Rear
Demerging Queues:
Is the process of creating two or more queues
from a single queue.
Used to give priority for some groups of data
Priority Queue
Algorithm:
create empty females and males queue
while (PriorityQueue is not empty)
{
Data=DequeuePriorityQueue();//delete data at the front
if(gender of Data is Female)
EnqueueFemale(Data);
else
EnqueueMale(Data);
}
Priority Queue
Merging Queues:
Is the process of creating a priority queue from
two or more queues.
Algorithm:
create an empty priority queue
while(FemalesQueue is not empty)
EnqueuePriorityQueue(DequeueFemalesQueue());
while(MalesQueue is not empty)
EnqueuePriorityQueue(DequeueMalesQueue());
Application of Queues