08 Queues
08 Queues
Introduction to Queues
• A queue is a waiting line – seen in daily life
$ $
Front Rear
In Queue
a. Items can be removed only at the front
b. Items can be added only at the other end, the back
1 2 3 Original Queue
Front Rear
Front Rear
2 3 4
Remove the element
from the Queue
Front Rear
Types Of Queues
• Linear Queue
• Circular Queue
• Double Ended Queue (Deque)
• Priority Queue
Double Ended Queue
Double ended queues, called deques for short, are a generalized form
of the queue. It is exactly like a queue except that elements can be
added to or removed from the head or the tail.
However, no element can be added and deleted from the middle.
Circular Queue
• This queue is not linear but circular.
"Rear" most index, if and only if the "Front" Figure: Circular Queue having
Rear = 5 and Front = 0
element has moved forward. otherwise it will be
09/10/08 14
Algorithms for Insert Operations in Circular Queue
For Insert Operation
Insert-Circular-Q(CQueue, Rear, Front, N, Item)
Here, CQueue is a circular queue.
Rear represents the location in which the data element is to be inserted and
Front represents the location from which the data element is to be removed.
N is the maximum size of CQueue and
Item is the new item to be added.
Initailly Rear = -1 and Front = -1.
1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
09/10/08 15
For Delete Operation
Delete-Circular-Q(CQueue, Front, Rear, Item)
CQueue is the place where data are stored.
Rear represents the location in which the data element is to be inserted and
Front represents the location from which the data element is to be removed.
Front element is assigned to Item.
Initially, Front = -1.
*..Delete without Insertion
09/10/08 16
Initailly Rear = -1 and Front = -1.
Example- ENQUEUE 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
Circular queue with N = 5. then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
1. Initially, Rear = 0, Front = 0. 4. Insert 20, Rear = 3, Front = 0.
Front
Rear
Rear
Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front
09/10/08 17
Rear
Initailly Rear = -1 and Front = -1.
Example- ENQUEUE 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
Circular queue with N = 5. then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
1. Initially, Rear = -1, Front =-1 4. Insert 20, Rear = 2, Front = 0.
Front
Rear
Rear
Rear
3. Insert 50, Rear = 1, Front = 0. 6. Delete front, Rear = 3, Front =1.
Front Rear Front
09/10/08 18
Rear
Initially, Front = -1. Initailly Rear = -1 and Front = -1.
1. If Front = -1 then Print: “Circular Queue Underflow” and Return . 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
2. Set Item := CQueue [Front]
then Print: “Circular Queue Overflow” and Return.
3. If Front = N – 1 then Set Front = 0 and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. If Front = Rear then Set Front = Rear = -1 and Return. 4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Set Front := Front + 1 5. Return
6. Return.
7. Insert 100, Rear = 4, Front = 1. 10. Delete front, Rear = 0, Front = 2.
Front Rear
ENQUEUE/DEQUEUE
Circular queue with N = 5. Front
Rear
Front
Front
09/10/08 19
Double Ended Queue
Double ended queues, called deques for short, are a generalized form
of the queue. It is exactly like a queue except that elements can be
added to or removed from the head or the tail.
However, no element can be added and deleted from the middle.