Priority queue

From Rosetta Code
Revision as of 02:08, 4 August 2011 by rosettacode>Ledrug (init draft)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Priority queue is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Priority queue is a useful concept in programming. It is somewhat similar to a Queue, with the important distinction: each item is added to a priority queue with a priority level, and will be later removed from the queue with the top priority element first.

Task Create a priority queue. The queue must support at least two operations:

  1. Insertion. An element is added to the queue with a priority (a numeric value).
  2. Top item removal. Deletes the element or one of the elements with the current top priority and return it.

Optionally, other operations may be defined, such as peeking (find what current top priority/top element is), merging (combining two priority queues into one), etc.

To test your implementation, insert a number of elements into the queue, each with some random priority. Then dequeue them sequentially; now the elements should be sorted by priority.

The implementation should be efficient. You may choose to impose certain limits such as small range of allowed priority levels, limited capacity, etc. If so, discuss the reasons behind it.