Question: Help me create a new class called StatQueue where the operations dequeue() and enqueue(e) should be rewrote in the StatQueue class. Here is the code:
Help me create a new class called StatQueue where the operations dequeue() and enqueue(e) should be rewrote in the StatQueue class.
Here is the code: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class LinkedQueue:
"""FIFO queue implementation using a singly linked list for storage."""
#-------------------------- nested _Node class --------------------------
class _Node:
"""Lightweight, nonpublic class for storing a singly linked node."""
__slots__ = '_element', '_next' # streamline memory usage
def __init__(self, element, next):
self._element = element
self._next = next
#------------------------------- queue methods -------------------------------
def __init__(self):
self._head = None
self._tail = None
self._size = 0 # number of queue elements
def __len__(self):
return self._size
def is_empty(self):
return self._size == 0
def first(self):
if self.is_empty():
print('Queue is empty')
return ""
return self._head._element
def dequeue(self):
if self.is_empty():
print('Queue is empty')
return ""
# raise Empty('Queue is empty')
answer = self._head._element
self._head = self._head._next
self._size -= 1
if self.is_empty():
self._tail = None
return answer
def enqueue(self, e):
"""Add an element to the back of queue."""
newest = self._Node(e, None)
if self.is_empty():
self._head = newest
else:
self._tail._next = newest
self._tail = newest
self._size += 1
def disp(self):
next = self._head
while next != None:
print(next._element)
next = next._next
if __name__ == '__main__':
lQueue = LinkedQueue()
print(lQueue.is_empty())
lQueue.enqueue(1)
lQueue.enqueue(2)
lQueue.enqueue(3)
print(lQueue.is_empty())
lQueue.disp()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
