Question: (e) (6 marks) Suppose we want to add functionality to the cinema priority queue implementation that allows the user to change the priority of an
(e) (6 marks)
Suppose we want to add functionality to the cinema priority queue implementation that allows the user to change the priority of an item in the queue. You may assume the items are unique.
Complete the problem definition below, stating any further assumptions you make. If you make no assumptions, say so.
Write your answer here
Operation: change_priority Inputs/Outputs: queue, a CinemaPriorityQueue Inputs: item, an object, new priority, an integer Preconditions: Postconditions: Assumptions:
--------------
QUESTIOIN BEFORE
-----------
(c) (10 marks)
Edit the insert function in the code below to convert your algorithm into Python code. You must observe the coding style guidance in Chapter 5.3 and Chapter 10.3.
In [ ]:
class CinemaPriorityQueue:
"""A linked list implementation of an unbounded min-priority queue."""
class Node:
"""A node in a linked list."""
def __init__(self, item: object, priority_value: int) -> None:
"""Initialise the node with the given item and priority value."""
self.item = item
self.priority = priority_value
self.next = None
def __init__(self) -> None:
"""Initialise the queue to be empty."""
self.head = None
def is_empty(self) -> bool:
"""
Preconditions: true
Postconditions: the output is true if the queue is empty, false otherwise
"""
return self.head == None
def print(self) -> None:
"""Print out the queue"""
if self.head == None:
print('The queue is empty') else:
current = self.head
while current != None:
print(current.item, current.priority)
current = current.next
def insert(self, item: object, priority_value: int) -> None:
"""Insert item according to priority.
Preconditions: true
Postconditions: post-self is the sequence
pre-self with item inserted after
the last item in self with the same priority
"""
pass
#*Write your code solution here*
(d) (12 marks)
We have provided a print operation print() that you can use to print out the queue. Test your code. You may use additional print statements to explain what you are testing with each test. Make sure you test for each of the three cases:
- insert into an empty queue
- insert at the front of the queue
- insert after some existing node.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
