queue.pptx (1)
queue.pptx (1)
1. Concepts of queue
2. Queue by array
3. Queue by circular array
4. Queue by linked list
5. Deques
6. Priority queue
7. Applications of queues
Queue implementations
• using an array
• using a linked list
12 9 7 18 14 36 45
FRONT REAR
0 1 2 3 4 5 6 7 8 9
9 7 18 14 36 45
FRONT REAR
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Rear
Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front
Rear
09/10/08 17
7. Insert 100, Rear = 5, Front = 2. 10. Delete front, Rear = 1, Front = 3.
Front Rear
Front
Rear
Front
Rear Front
Front
09/10/08 18
Inserting an Element in a Circular Queue
• For insertion we check for three conditions which are as follows:
▪ If front=0 and rear= MAX – 1, then the circular queue is full.
90 49 7 18 14 36 45 21 99 72
front=0 1 2 3 4 5 6 7 8 rear = 9
▪ If rear != MAX – 1, then the rear will be incremented and value will
be inserted
90 49 7 18 14 36 45 21 99
front=0 1 2 3 4 5 6 7 rear= 8 9
49 7 18 14 36 45 21 99 72
front=1 2 3 4 5 6 7 8 rear= 9
9 10 7 18 14 36 45 21 99 72
rear=1 front=2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
▪ If the queue is not empty and after returning the value on front, if
front = rear, then it means now the queue has become empty and
so front and rear are set to -1.
Delete this element and set
81 rear = front = -1
0 1 2 3 4 5 6 7 8 front=rear= 9
▪ If the queue is not empty and after returning the value on front, if
front = MAX -1, then front is set to 0.
72 63 9 18 27 39 81
0 1 2 3 4 rear= 5 6 7 8 front= 9
1 7 3 4 2 6 5 X
FRONT REAR
29 37 45 54 63
0 1 2 LEFT = 3 4 5 6 RIGHT = 7 8 9
63 27 18
42
RIGHT = 0 1 2 3 4 5 6 LEFT = 7 8 9
P2
P3
© Oxford University Press 2014. All rights reserved.
Linked List Representation of Priority Queues
A 1 B 2 C 3 D 3 E 4 X
A 1 B 2 C 3 D 3 E 4 F 4 X