"""This is an incompleted implementation. Students are supposed to implement the enqueue() method""" """A linked list implementation of priority queue.""" class PriorityQueue: def __init__( self ): """Constructor to create an empty queue.""" self.head = None self.tail = None self.count = 0 def is_empty( self ): """Check to see if the queue is empty.""" return self.count == 0 def __len__( self ): """Return the length of the queue.""" return self.count def enqueue( self, item, priority ): """Insert the new node with item and priority into the queue.""" # 1. make a new entry (PriorityEntry) that contains item and priority # 2. make a new node (PriorityNode) that contains the entry and next # 3. insert the new node in a correct place pass def dequeue( self ): """Remove an item at the front.""" assert not self.is_empty(), "Cannot dequeue from an empty queue." item = self.head self.head = self.head.next if self.head == None: # now an empty queue self.tail = None self.count -= 1 return item.data def peek( self ): """Examine the value of the first node.""" assert not self.is_empty(), "Cannot peek from an empty queue." node = self.head return node.data class PriorityNode: """The node contains a piece of data and a 'next' link.""" def __init__( self, data ): self.data = data self.next = None class PriorityEntry: """A priority entry contains a piece of data and a priority.""" def __init__( self, item, priority ): self.item = item self.priority = priority def __str__( self ): """Return a string form of the node for printing.""" value = 'Priority : ' + str( self.priority ) + \ ' data: ' + str( self.item ) return value