Feb 25, 2008
Assignment #6: Priority Queue
Wed Mar 5th 2:15pm
For this assignment, you will be implementing a class of your own: a priority queue, which is a
variation on the standard queue. The standard queue processes element in the first-in, first-out
("FIFO") manner typical of ordinary waiting lines. Queues can be handy, but a FIFO strategy isn't
always what's needed. A hospital emergency room, for example, needs to schedule patients according
to priority. A patient with a more critical problem will pre-empt others even if they have been waiting
longer. This is a
, where elements are prioritized relative to each other and when asked
to dequeue one, it is the highest priority element in the queue that is removed. There are many
practice uses for a priority queue.
The main focus of this assignment is to implement a priority queue class in several different ways.
You'll have a chance to experiment with arrays, linked lists, and a special kind of tree called a heap.
Once you have debugged your implementations, you will run some tests and consider the strengths
and weaknesses of the various versions. We provide client code that tests and times the performance
of the class, your role will be to act as the implementer only.
Ideally the priority queue would be written as a template class. However, given this is your first class
implementation project, we're going to hold off on making it fully generic and instead use integer
This priority queue will store a collection of integers, where the integer itself is used as the
priority. Larger integers are considered higher priority than smaller ones and are dequeued ahead of
smaller values. Here is the basic priority queue interface:
void enqueue(int newElem);
Note the priority queue interface is quite similar to that of the ordinary queue. For the detailed
specification of the behavior and usage of these functions, see the
interface file included in
the starter files. Just six member functions, how hard can that be? :-)
Implementing the priority queue
A priority queue can be implementing using a variety of data structures, each with different tradeoffs
between memory required, runtime performance, complexity of code, etc. In this assignment, you will
consider four different implementations. One implementation stores the queue elements in an
unsorted vector. The second represents the priority queue as a sorted linked list. The third is a hybrid
cross between an array and a linked list, a
. The last represents the priority queue as
specially-ordered binary tree called a
(not to be confused with the heap where memory is
dynamically allocated by
). The first two implementations are provided to you pre-written, the last
two will be yours to write.