In: Computer Science
in python please ,, A SOON AS POSSIBLE PLEASE #Class invariant for SimpleQueue, a queue based on a linked list # 1. The queue is a linked list of ListNode objects. # 2. A SimpleQueue object has instance variables self.head, a reference to the node at the front of the queue, self.tail, a reference # to the node at the end of the queue, and self.size, the number of nodes in the queue. # 3. If self.size = 0, then self.head and self.tail are None. # 4. The ListNode object at the end (referred to by self.tail) has link None. For all other ListNode objects, # the link refers to the next ListNode in the queue. # 5. The new item is enqueued at the tail of the SimpleQueue. # 6. An item is dequeued from the head or front of the SimpleQueue.
class SimpleQueue: '''a class for a simple queue implemented as a linked list with head and tail references''' def __init__(self): '''constructor that creates an empty queue''' def enqueue(self, item): '''post: ListNode with item as its data has been inserted at the tail of the queue''' def dequeue(self): '''post: The ListNode at the head of the Queue has been removed from the queue and its item has been returned.''' def front(self): '''post: Returns the item at the front of the queue. The ListNode at the front is not removed. ''' def length(self): '''post: Returns the number of items in the queue'''
class ListNode(object): def __init__(self, item = None, link = None): '''post: creates a ListNode with the specified data value and link''' self.item = item self.link = link
SOURCE CODE: *Please follow the comments to better understand the code. **Please look at the Screenshot below and use this code to copy-paste. ***The code in the below screenshot is neatly indented for better understanding. # Class invariant for SimpleQueue, a queue based on a linked list # 1. The queue is a linked list of ListNode objects. # 2. A SimpleQueue object has instance variables self.head, a reference to the node at the front of the queue, self.tail, a reference # to the node at the end of the queue, and self.size, the number of nodes in the queue. # 3. If self.size = 0, then self.head and self.tail are None. # 4. The ListNode object at the end (referred to by self.tail) has link None. For all other ListNode objects, # the link refers to the next ListNode in the queue. # 5. The new item is enqueued at the tail of the SimpleQueue. # 6. An item is dequeued from the head or front of the SimpleQueue. class SimpleQueue: """a class for a simple queue implemented as a linked list with head and tail references""" def __init__(self): """constructor that creates an empty queue""" self.front = None self.rear = None def enqueue(self, item): """post: ListNode with item as its data has been inserted at the tail of the queue""" temp = ListNode(item) # Check if queue is empty if self.rear is None: self.front = temp self.rear = temp else: self.rear.link = temp self.rear = temp def dequeue(self): """post: The ListNode at the head of the Queue has been removed from the queue and its item has been returned.""" if self.length() == 0: return None temp = self.front self.front = temp.link if self.front is None: self.rear = None return str(temp.item) def front(self): """post: Returns the item at the front of the queue. The ListNode at the front is not removed. """ return self.front def length(self): """post: Returns the number of items in the queue""" temp = self.front count = 0 while temp is not None: count += 1 temp = temp.link return count class ListNode(object): def __init__(self, item=None, link=None): """post: creates a ListNode with the specified data value and link""" self.item = item self.link = link # TEST here def main(): queue = SimpleQueue() print('The length is:', queue.length()) print('inserting 10,20,30..') queue.enqueue(10) queue.enqueue(20) queue.enqueue(30) print('Now, The length is:', queue.length()) print('The Front is:', queue.front.item) print('\nDequeue: ', queue.dequeue()) print('Dequeue: ', queue.dequeue()) print('Now, The length is:', queue.length()) print('The Front is:', queue.front.item) main()
================
OUTPUT