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  n elememt of higher priority is processed before any element of lower priority Tw o elements with same priority are processed according to the order in which they were added to the !ueue Can be implemented using One"way list #ultiple !ueues

Priority Queue

  • Upload
    robinpt

  • View
    216

  • Download
    0

Embed Size (px)

DESCRIPTION

Priority Queue

Citation preview

  • PRIORITY QUEUECollection of elements such thatEach element has been assigned a priorityThe order in which elements are deleted and processed comes from the following ruleAn elememt of higher priority is processed before any element of lower priorityTwo elements with same priority are processed according to the order in which they were added to the queueCan be implemented usingOne-way listMultiple 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.

    ITEM1234567priority10877641

  • OperationsInsertions to the rearDeletions from front

  • Priority Queue insertionInput: ITEM and PRIORITY to be inserted into PQOutput: ITEM inserted at REAR end of PQData structure: 2D array PQ with FRONT and REAR pointerSteps:If (REAR=N)print queue is full and exitelse if FRONT=0 FRONT=1endifREAR=REAR+1PQ[0][REAR]=ITEMPQ[1][REAR]=PRIORITYsort_PQ(PQ[])endifStop

  • Priority Queue DeletionInput: priority queue with two pointer fromt and rearOutput: One item is deleted from the front end of the priority queueData Structure: Priority queue is implemented by using ArraySteps: 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 Stop