IT259 - DSA - Queue
IT259 - DSA - Queue
Passengers in queue
at Railway station, Bus
stop, Movie ticket
counter, etc.
Queue Data Structure 3
Applications of Queue
• Resource Sharing
Output A B C D Input
front rear
n-1 3 2 1 0
DCBA
After A leaves,
n-1 3 2 1 0
DCB
1. [Overflow?]
If R = = (N-1)
then Write (“Queue OVERFLOW”)
Return
2. [Increment rear pointer]
R=R+1
3. [Insert element]
Q [R] = Y
4. [Is front pointer properly set?]
If F == (-1)
then F = 0
Return
Queue Data Structure 14
Static Queue Operations
Dequeue (Q, F, R). Given F and R, the pointers to the front and rear elements of a
queue, respectively, and the queue Q to which they correspond, this function deletes
and returns the last element of the queue. Y is a temporary variable.
1. [Underflow?]
If F == (-1)
then Write (“Queue UNDERFLOW”)
Return (-1) //-1 denotes an empty queue
2. [Delete element]
Y = Q [F]
3. [After Deletion, Is Queue empty? ]
If F = = R
F = (-1)
R = (-1)
else
F=F+1
4. [Return Deleted element]
Return (Y) Queue Data Structure 15
Dynamic Queue Operations
Enqueue(F, R , x) : F is the front pointer to the first (front most) element
of the Queue and R is the rear pointer to last element of the Queue.
Insert Y at the rear end of the queue. Initially, F and R are NULL.
1. [Underflow?]
If F == NULL
then Write (“Queue is empty”)
Return
2. [Delete element]
Y = INFO(F)
3. [After Deletion, Is Queue empty? ]
If (F = = R)
F = NULL
R = NULL
else
F = LINK(F)
4. [Return Deleted element]
Return (Y)
n-1 3 2 1 0
Y X ……
rear front
Circular Array
rear=3
3
2
C
n-1 3 2 1 0 front=0 1
B
…… CBA
0 A
3 rear=3 front=1 3
2 2
1 BC 1 BC
0 A 0
n-1 n-1
Return
3. [Insert element]
Q [R] = Y
4. [Is front pointer properly set?]
If F = = (-1)
then F = 0
Return
Queue Data Structure 25
--------------------------------------------------------------------------------------------------------------------------
CIRCULAR QUEUE
Function CQ_Dequeue( F, R, Q, N). Given F and R, pointers to the front and rear
elements of a circular queue, respectively, and an array Q consisting of N elements, this
function deletes and returns the last element of the queue. Y is a temporary variable.
1. [Underflow?]
If F == (-1)
then Write (“UNDERFLOW”)
Return(-1)
2. [Delete element]
Y = Q [F]
3. [Queue empty? ]
If F == R
then F = (-1)
R= (-1)
Return (Y)
4. [Increment front pointer]
If F == N-1
then F = 0
else F = F + 1
Return (Y)
Queue Data Structure 26
Example
• Show circular queue contents with front and rear
after each step with size=5.
(i) Insert 10, 20, 30.
(ii) Delete
(iii) Insert 40, 50, 60, 70.
30
20 2
4
30
30
Queue Data Structure 30
3
Customer Service In Royal Bank
Deletion Deletion
…………...
Insertion Insertion
Insertion
Deletion …………...
Deletion
Input-restricted deque
Front Rear
Insertion
…………... Insertion
Deletion
Output-restricted deque
● ●
Priority p1
F R
● ●
Priority p2
F R
● ●
Priority pn
Packet transmission