Question: Implement a singly linked list version of a Queue by completing the enqueue and dequeue functions from the starter code below. enqueue should add a
Implement a singly linked list version of a Queue by completing the enqueue and dequeue functions from the starter code below. enqueue should add a new item to the queue at the rear on the right and dequeue should remove and return the item from the front on the left. Your code should keep track of the front and rear Nodes for easy access to both ends of the list. Do not use the Java LinkedList data type since you are implementing it yourself. Nor should you store the Nodes in a list since that would defeat the purpose of the linked list implementation. Copy and paste the main method at the end of this file and see that it runs correctly.
class Node: def __init__(self, initdata): self.data = initdata # The data held within the current Node self.next = None # The next Node object (NOT the data) class Queue: def __init__(self): self.front = None # The front Node object self.rear = None # The rear Node object self.width = 0 def isEmpty(self): return(self.width == 0) def enqueue(self, item):
#implement this function
def dequeue(self):
#implement this function
def size(self): return(self.width)
# the root(=main) starts here
q = Queue() print(q.isEmpty())
print(q.size())
q.enqueue(136) print(q.isEmpty())
print(q.size())
q.enqueue(120)
print(q.isEmpty())
print(q.size())
j=q.dequeue()
print(item removed is;,j)
print(q.isEmpty())
print(q.size())
j=q.dequeue()
print(item removed is;,j)
print(q.isEmpty())
print(q.size())
q.enqueue(101) q.enqueue(135)
q.enqueue(105)
print(q.isEmpty())
print(q.size())
j=q.dequeue()
print(item removed is;,j)
print(q.isEmpty())
print(q.size())
j=q.dequeue()
print(item removed is;,j)
print(q.isEmpty())
print(q.size())
j=q.dequeue()
print(item removed is;,j)
print(q.isEmpty())
print(q.size())
#the root(=main) ends here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
