View the step-by-step solution to:

CS106B Handout 27 Spring 2010 Assignment 4: So, it's finally time for you to be implementing a class of your own: a priority queue, which is a...

I don't understand how to do this at all. Can I get walked through this coding process?
CS106B Handout 27 Spring 2010 May 5 th , 2010 Assignment 4: Priority Queue Thanks to Julie and Mehran for polishing off this assignment. So, 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 the reader. The standard queue is a collection of elements managed in a first-in, first-out 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 simplistic 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 in arbitrary order, 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). Due: Friday, May 14 th at 5:00 p.m. The PQueue Interface The priority queue will be a collection of integers, where the integer itself is used as the priority. Smaller integers should be considered higher priority than larger ones and, thus, removed before larger integers. Here are the functions that make up the public interface of the priority queue: class PQueue { public: PQueue(); ~PQueue(); int size(); bool isEmpty(); void add(int elem); int extractMin(); private: // implementation dependent member variables and helper methods };
Background image of page 01
2 add is used to insert a new element to the priority queue. extractMin returns the value of highest priority (i.e., the smallest) element in the queue and removes it. For the detailed descriptions on how these methods behave, 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 fourth implementation will represent the priority queue as a heap (not to be confused with the heap where memory is dynamically allocated via new ). The first two implementations are provided to you, but the last two will be yours to write. Unsorted Vector implementation The unsorted vector implementation is already written and given to you in the starter project. You do not have to write anything for this, but be sure to go over and be familiar with the code provided. This implementation is layered on top of our Vector class. add simply appends the new element to the end. When it comes time to extractMin the minimum element (i.e. the one with the highest priority in our version), it performs a linear search to find it. Sorted linked list implementation The sorted linked list implementation is also given to you so you can observe and test it. The linked list implementation is a singly linked list of values, where the values are kept in sorted (i.e., smallest to largest) order to facilitate finding and removing the smallest element quickly. However, inserting is a bit trickier because of the need to search for the right position to insert the new value. Sorted chunklist implementation Now it's your turn! Neither the array nor the linked list is a particularly strong performer in terms of space or time efficiency, so next you'll try concocting a combination of the two to see what advantages a hybrid might offer. We still maintain the sorted property of the original linked list but try to reduce some of the memory overhead and slow traversal time by making each node not a single element, but a block of elements. Thus, the chunklist combines the array and linked-list approaches so that the actual structure consists of a singly linked set of blocks, where each block contains a fixed size array capable of holding several elements rather than a single one.
Background image of page 02
Show entire document

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question