Queue
Queue
Queue
• In our everyday life we come across many situation where we are wait for the
desired service, there we have to get into a waiting line for our turn to get serviced.
} F=-1
R= -1
F=0
R= 5
q[0] q[1] q[2] q[3] q[4] q[5]
After 3 delete operations:
14 15 16
F=3
R= 5
Call Insert function for 17. It will result in overflow as R= n-1 (where n is the size of the
queue)
This is the disadvantage of simple queue as there are blank spaces in an array but we
cannot store the elements.
• The order in which the elements should get added or removed is decided based the priority of
the element.
• The element with a higher priority is processed before lower priority element.
• Smallest number means highest priority and greater number means less priority.
• Method 1:
Element Q[0] Q[1] Q[2] Q[3] Q[4]
Names Consider Insert operation for value 16
with priority 2.
Values 11 12 13 14 15
16
Priority 5 1 6 4 3
2
R
F
F R
Priority 5 6 4 3 2
R
F
Prepared By : Vaishali Koria Data Structure and Algorithms 33
Priority Queue
• Two methods to access the elements of priority Queue:
• Method 2:
Element Q[0] Q[1] Q[2] Q[3] Q[4]
Names Consider Insert operation for value 16
with priority 2.
Values 11 12 13 14 15
16
Priority 5 1 6 4 3
2
R
F
Priority 1 2 3 4 5 6
Priority 2 3 4 5 6
F R
F R
F R
Insertion will be done in queue with matching priority queue of element. Suppose we
want to insert 26 with priority 2.
Element Q[0] Q[1] Q[2] Q[3] Q[4] Q[5]
Priority 2 : Names
Values 16 17 18 19 20 26
F R
Prepared By : Vaishali Koria Data Structure and Algorithms 37
Multi Queue implementation of Priority Queue
Priority 1 : Element Q[0] Q[1] Q[2] Q[3] Q[4]
Names
Values 11 12 13 14 15
F R
• There are two types of Deque. These two types are due to the restrictions put to perform
either the insertions or deletions only at one end.