{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


26-Assignment-4-PQueue - CS106X Winter 2008 Handout 26...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 26 Winter 2008 February 8, 2008 Assignment 4: Priority Queue Thanks to Julie and Mehran for polishing off this assignment. Now, it's finally time for you to be implementing a class of your own: a priority queue, which is a variation on the standard queue described in Chapter 10 of the reader. The standard queue is a collection of elements managed in first-in, first-out ("FIFO") manner. The first element added to the collection is always the first element extracted; the second is second; so on and so on. In some cases, a FIFO strategy may be too simple for the activity being modeled. A hospital emergency room, for example, needs to schedule patients according to priority. A patient who arrives with a more serious problem should pre-empt others even if they have been waiting longer. This is a priority queue , where elements are added to the queue by priority, but when time comes to extract the next element, it is the highest priority element in the queue that is removed. Such an object would be useful in a variety of situations. In fact, you can even use a priority queue to implement sorting; insert all the values into a priority queue and extract them one by one to get them in sorted order. 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 chunk lists (more on those later). 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, while your role will be to act as the implementer. Due: Friday, February 15 th at 1:15 p.m. The PQueue Interface The priority queue will initially be a collection of integers, where the integer itself is used as the priority. Larger integers should be considered higher priority than smaller ones and, thus, removed before smaller values. Here are the functions that make up the public interface of the priority queue:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 class PQueue { public: PQueue(); ~PQueue(); int size(); bool isEmpty(); void enqueue(int elem); int extractMax(); private: // implementation dependent member variables and helper methods }; enqueue is used to add a new element to the priority queue. extractMax returns the value of highest priority (i.e., largest) element in the queue and removes it. For the detailed specification of the behavior and usage of these functions, see the pqueue.h interface file included in the starter files. Implementing the priority queue There are many data structures you could choose to represent and manipulate a priority queue, with various tradeoffs between the amount of memory required, speed of the insert and extract operations, complexity of code to get the job done, etc. The first implementation will store the priority queue elements in an unsorted array. The second implementation will represent the queue as a sorted linked list. The third implementation is a hybrid cross between an array and a linked list, a chunklist . The
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 12

26-Assignment-4-PQueue - CS106X Winter 2008 Handout 26...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online