Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 5

PRIORITY QUEUE

Collection of elements such that

Each element has been assigned a priority

The order in which elements are deleted and


processed comes from the following rule

An elememt of higher priority is processed before any


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

Can be implemented using

One-way list

Multiple queues

Array representation of priority


queue

By using two dimensional array, we can represent priority queue.

Example: PQ[2][20] ,

first rows is used to stores the queue elements,

Second row is used to store the priority of the elements

Two pointers FRONT and REAR are used to represent two ends of the
queue.

ITEM

priority 10

Operations

Insertions to the rear

Deletions from front

Priority Queue insertion


Input: ITEM and PRIORITY to be inserted into PQ
Output: ITEM inserted at REAR end of PQ
Data structure: 2D array PQ with FRONT and REAR pointer
Steps:
If (REAR=N)
print queue is full and exit
else
if FRONT=0 FRONT=1
endif
REAR=REAR+1
PQ[0][REAR]=ITEM
PQ[1][REAR]=PRIORITY
sort_PQ(PQ[])
endif
Stop

Priority Queue Deletion


Input: priority queue with two pointer fromt and rear
Output: One item is deleted from the front end of the priority queue
Data Structure: Priority queue is implemented by using Array
Steps:

If(front=-1) AND(rear=-1)
Printf(Priority Queue is empty) and Exit
Else
Item=PQ[0][front]
Priority=PQ[1][front]
If(front=rear)
Front=Rear=-1
Else
Front=front+1
EndIf
EndIf

You might also like