Queue
Queue
QUEUE
Queue is a linear data structure in which
insertion is done from one end and deletion is
done from other end.
In stack insertion is from front end and deletion
are done from rear end.
The item inserted first is deleted first.
F=0
R=0
After insertion of second element.
F=0 R=1
STATIC IMPLEMENTATION OF QUEUE
After insertion of second element.
1 2 3 Queue with 3 elements
F=0 R=2
F=1 R=2
F=2
R=2
STATIC IMPLEMENTATION OF QUEUE
Now insert an element in the queue.
3 4 Queue with 2 elements
F=2 R=3
F=2 R=4
100
After insertion of second node.
200 rear
front
100
1 200 2 NULL
100 200
After insertion of third node.
front
200 300 rear
2 300 3 NULL
200 300
INSERT OPERATION
DELETE OPERATION
TRAVERSE OPERATION
CIRCULAR QUEUE
LIMITATION OF SIMPLE QUEUES
LIMITATION: FALSE-OVERFLOW
Suppose 6 calls to enqueue () have been made, so now the
queue array is full
2 3 1 6 4 10
[0] [1] [2] [3] [4] [5]
If we try to insert,
Overflow occurs
Assume 3 calls to dequeue ( ) are made Though some
cells are empty
6 4 10
Front Rear
6 4 10
INSERTION
ENQUEUE: CIRCULAR QUEUE
[2] [3]
Front = -1
Rear = -1
[1] [4]
[0] [5]
ENQUEUE:CIRCULAR QUEUE
[2] [3]
Front = 0
Rear = 0
[1] [4]
10
Rear [0] [5]
Front
ENQUEUE 10
ENQUEUE: CIRCULAR QUEUE
[2] [3]
Front = 0
Rear Rear = 1
20
[1] [4]
10
[0] [5]
Front
ENQUEUE 20
ENQUEUE: CIRCULAR QUEUE
Rear
[2] [3]
30
Front = 0
Rear = 2
20
[1] [4]
10
[0] [5]
Front
ENQUEUE 30
ENQUEUE: CIRCULAR QUEUE
[2] [3]
30 40 Front = 0
Rear = 3
20
[1] [4]
10
[0] [5]
Front
ENQUEUE 40
ENQUEUE: CIRCULAR QUEUE
[2] [3]
30 40 Front = 0
Rear = 4
20
[1] 50 [4]
10
[0] [5]
Front
ENQUEUE 50
CIRCULAR QUEUE
[2] [3]
40
Front = 0
30
Rear = 5
20
[1] 50 [4]
ENQUEUE 60
10
[0] 60 [5]
Rear
DELETION
[2] [3]
30 40
20
[1] 50 [4]
10
[0] 60 [5]
Rear=5
Front=0
DEQUEUE: CIRCULAR QUEUE
[2] [3] Queue After First Dequeue
30 40
Front
20
[1] 50 [4] Front = 1
Rear = 5
[0] 60 [5]
DEQUEUE: CIRCULAR QUEUE
Front
[2] [3]
30 40
Queue After 2nd Dequeue
[1] 50 [4]
[2] [3]
40
Front = 2
30
Rear = 0
[1] 50 [4]
70
[0] 60 [5]
Rear=0 Insert 70
ALGORITHM: DELETION
Input: Here QUEUE is an array with N locations. FRONT
and REAR points to the front and rear elements of the
QUEUE.
1. If (FRONT == -1) Then
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is left]
(a) Set FRONT = -1
(b) Set REAR = -1
6. Else
7. Set FRONT = (FRONT + 1)%N
8. Print: ITEM deleted
9. Exit
DELETE OPERATION
TRAVERSE OPERATION
PRIORITY QUEUES
PRIORITY QUEUES
Each element has been assigned a priority.
Ex: 2, 3, 8, 6, 1
2 3 8 6 1
Rear
PRIORITY QUEUES
Deletion:
While deleting, Normal Queue rules are not followed.
Two elements with the same priority are deleted according to the
order in which they were added to the queue.
DELETION: PRIORITY QUEUES
Rear
2 3 8 6 1
[0] [1] [2] [3] [4] [5]
3 6 1
[0] [1] [2] [3] [4] [5]
DELETION: PRIORITY QUEUES
Rear
3 6 1
3 6 1
Ascending
An ascending priority queue is a collection of items into which
items can be inserted arbitrarily and from which only the smallest
item can be removed.
Descending:
An descending priority queue is a collection of items into which
items can be inserted arbitrarily and from which only the highest
item can be removed.
DOUBLE ENDED QUEUE :DEQUEUE
64
APPLICATIONS OF QUEUES
People waiting in line at a bank/reservation counter.