Write a template version of a class that implements a priority queue.
Queues are discussed in Chapter 13 and priority queues are discussed in
Chapter 18. To summarize, a priority queue is essentially a list of items
that is always ordered by priority. Each item that is added to the list
requires an associated priority value. For this problem, make the priority
an integer where 0 is the highest priority and larger values are lower in priority.
Removing an item from the queue removes the item with the highest
priority.
The add function of the priority queue should take a generic type and then
an integer priority. In the example below, the generic type is a char and
we have added three items to the queue:
q.add('X', 10);
q.add('Y', 1);
q.add('Z', 3);
The remove function should return and remove from the priority queue
the item that has the highest priority. Given the example above we would
expect the following:
cout << q.remove(); // Outputs Y (priority 1)
cout << q.remove(); // Returns Z (priority 3)
cout << q.remove(); // Returns X (priority 10)
Test your queue on data with priorities in various orders (e.g., ascending,
descending, mixed). You can implement the priority queue by storing the
items using a list(s) of your choice (e.g., vector, array, linked list, or
GenericList described in this chapter) and then performing a linear
search for the item with the lowest integer value in the remove function. In future courses you may study a data structure called a heap that affords a more efficient way to implement a priority queue.
Sorry the answer is not available at the moment…
If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.
Spread the 'tradition of sharing'.