Professional Documents
Culture Documents
Priority Queue
Priority Queue
By
Ravi Kant Sahu
Asst. Professor
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Introduction
A Priority Queue is a collection of elements such that each
element has been assigned a priority and such that the order in
which elements are processed comes from the following rules:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
Time sharing systems: Programs of high priority are processed
first.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
One-way list Representation
of
Priority queues
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
One-Way List Representation
Each node in the list contains three types of information fields
(Information INFO, Priority Number PRN, and a link LINK).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Deletion in a Priority Queue
1. Set ITEM = INFO [START].
3. Process ITEM.
4. Exit.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion in a Priority Queue
1. Traverse the One-Way list until finding a node X whose priority
number exceeds N.
3. If no such node is found, insert the ITEM as the last element of the
list.
4. Exit.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array Representation
of
Priority Queues
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array Representation of Priority Queues
Use a separate queue for each level of priority (for each priority
number).
Each such queue will appear in its own circular array and have its
own pointers FRONT and REAR.
FRONT REAR
2 4 1 2 3 4 5
3 [3 X Y Z ]
0
[0 P ]
[ ]
5 2
[ A B D ]
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Deletion in a Priority Queue
Delete and process the first element in a priority queue
maintained by a two-dimensional array QUEUE.
3. Exit.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion in a Priority Queue
Insert an ITEM with priority number M to a priority queue
maintained by a two-dimensional array QUEUE.
2. Exit.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Review Question
Q. 1: What is the difference between Queue, Deque and Priority
Queue?
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)