A priority queue is like a queue but when an item is added it is given a priority and it will advance ahead of any items in the queue with lower priority (while remaining behind items with the same priority or with higher priority). The primitive operations are createpq, isempty, front (which should return the string from the front item in the queue without any information about its priority), deletefront, frontpri (which should return the priority of the front item), and addtopq (which should take two arguments, a string and a priority).

You are required to write in either Java or Python 3 a class that implements a priority queue of strings with methods for all of the primitive operations (other than createpq, which is not needed since the constructor (or Python __init__ method) should ensure that any new priority queue object is empty). The class should also have a (Java) toString or (Python) __str__ method that generates a string of the form <"hello":7,"hi":2>. The data in the class must be private. If you choose to use Java you are not allowed to make use of the PriorityQueue class from the Collections Framework.

For this exercise priorities are to be represented by integers in the range 1 to 20, with 1 denoting the highest priority and 20 the lowest. The front, frontpri and deletefront methods should throw/raise an exception when applied to an empty queue; the addtopq method should throw/raise an exception if the priority is not in the range 1 to 20. If you choose to use Python all methods should also raise an exception if an argument of the wrong type is supplied.

You may make use of any classes supplied for this module such as ListCell and QueueException.

You should also write and submit code that tests the behaviour of all of the operations, generating output indicating what methods are being called and what results are returned and displaying the contents of the queue whenever changes are made. A fragment of the output might look like

Queue contents: < "hello":1,"hi":5> Adding "ho" with priority 3 Queue contents: < "hello":1,"ho":3,"hi":5> Calling frontpri: 1 returned Calling front: "hello" returned

You should include code to test the behaviour of the front, frontpri, and removefront operations when applied to an empty queue this will require three separate try-catch or try-except blocks. You should also test the behaviour of the addtopq method when supplied with an out-of-range priority argument.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.