1 / 10

Priority Queue

Priority Queue. most urgent first, not first in the line. Priority Queue. collection of items each item has a priority associated with it only element that can be removed is element with highest priorityno access to interior of collection. remove element. insert new element. 40. 28. 52.

acton
Download Presentation

Priority Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Priority Queue most urgent first, not first in the line

  2. Priority Queue • collection of items • each item has a priority associated with it • only element that can be removed is element with highest priorityno access to interior of collection remove element insert new element 40 28 52 93 26 80 D. Goforth, COSC 2006, fall 2003

  3. Priority Queue applications • multi-tasking operating system • Cinefest movie line-up • hospital emergency room D. Goforth, COSC 2006, fall 2003

  4. Queues and Priority Queues • queue • priority queue (conceptual) insert new element remove element 40 78 12 33 26 80 rear front remove element insert new element 90 28 52 93 26 80 D. Goforth, COSC 2006, fall 2003

  5. Priority Queue methods (same) • public void insert (Object item) • add new item to priority queue(enqueue) • public Object getFront() • remove item of highest priority(dequeue) if there are many items of same priority, remove item inserted first • public boolean isEmpty() • true when no items in priority queue • public int size() • number of items in priority queue D. Goforth, COSC 2006, fall 2003

  6. Implementations – many variations • list • array • array of queues D. Goforth, COSC 2006, fall 2003

  7. implementation as array of queues • useful with small number of priorities (i.e., many elements have same priority) • e.g. Cinefest line-ups 1st 2nd 3rd D. Goforth, COSC 2006, fall 2003

  8. Linked listimplementation of a priority queue extend Queue class and override insert method so elements are kept in priority order q.insert(“pppp”); pppp 5 aaa mm tttt xxx q manyNodes 4 front rear D. Goforth, COSC 2006, fall 2003

  9. implementation of insertin linked list insert in priority order BUT after elements of same priority insert new element 43 28 32 43 49 80 D. Goforth, COSC 2006, fall 2003

  10. The insert method public void insert (Object item) { if (isEmpty()) { front = new Node(item, null); // only item in queue rear = front; } else if (((Event)item).compareTo ( front.getData() ) < 0) // before first front = new Node(item,front); else if ( ((Event)item).compareTo ( rear.getData() ) >= 0 ) // after last { rear.addNodeAfter(item); rear = rear.getLink(); } else // in the interior of the list { Node cursor = front; while ( ((Event)item).compareTo ( cursor.getLink().getData() ) >= 0) cursor = cursor.getLink(); cursor.addNodeAfter(item); } manyNodes++; } D. Goforth, COSC 2006, fall 2003

More Related