Question: Make a FIFO queue that will also implementing a priority to the elements in our queue. It will not always be taking the first element

Make a FIFO queue that will also implementing a priority to the elements in our queue.

It will not always be taking the first element in the queue, but the first element with the highest priority.

To do this, create a class to hold an item that will be placed in the queue and its priority in addition to the PriorityQueue class itself.

For this, to simplify some matters, assume that the elements will be Strings and that the priorities can be only from 0-5, with 5 being the highest.

The operations that will be implemented for the PriorityQueue will be detailed below.

Create the element class as a generic class so that any type may be used and not just Strings. This will require some slight changes to the PriorityQueue class as well.

PriorityQueue Operations to Implement:

enqueue() Will take a new element and place it in the back of the queue.

dequeue() Will remove the next element from the queue, based on position and priority.

isEmpty() Will return whether the queue is empty or not.

peek() Returns the next item that will be removed from the queue, but does not modify the queue. size() Returns the length of the queue.

toString() - Returns a String representation of the current queue.

Helpful Hints:

It will need some methods for the class that contains the elements placed in the queue.

In the toString() method, it will be useful to include the priorities, especially for testing.

Sample Run/Output

Current queue size is: 3

The current queue is:

[(Tom, 3), (Dick, 4), (Harry, 2)]

Dequeueing next element in line.The current queue is:

[(Tom, 3), (Harry, 2)]

Current queue size is: 5

The current queue is:

[(Tom, 3), (Harry, 2), (Larry, 2), (Curly, 3), (Moe, 4)]

The next element to be removed is: Moe

Dequeueing next element in line.

The current queue is:

[(Tom, 3), (Harry, 2), (Larry, 2), (Curly, 3)]

Dequeueing next element in line.The current queue is:

[(Harry, 2), (Larry, 2), (Curly, 3)]

Current queue size is: 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!