You are on page 1of 14

Data Structures

Lecture 15: Priority Queues

By
Ravi Kant Sahu
Asst. Professor

Lovely Professional University, Punjab


Outlines
 Introduction

 One-Way List Representation of Priority Queues

 Array Representation of Priority Queues

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:

o An element of higher priority is processed before any element of


lower priority.
o Two elements with the same priority are processed according to
the order in which they were added to the queue.

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).

 A node X precedes a node Y in the list when:


o X has higher priority than Y.
o Both X and Y have same priority but X was added before Y.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Deletion in a Priority Queue
1. Set ITEM = INFO [START].

2. Delete First node from the list.

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.

2. Insert ITEM in front of node X.

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.

1. [Find the first non-empty queue.]


Find the smallest K such that FRONT [K] ≠ NULL.

2. Delete and process the front element in row K of 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.

1. Insert ITEM as the REAR element in row K of 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?

Q. 2: What is the need of Priority Queue?

Q. 3: How Priority Queues are maintained in memory?

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

You might also like