Question
In class, we discussed the abstract data type Queue. A queue is a collection of items where the addition of new items happens at one
In class, we discussed the abstract data type Queue. A queue is a collection of items where the addition of new items happens at one end (tail) and the removal of existing items occurs at the other end (head) (FIFO). Use the Node class (an object with a data field and a pointer to the next element) to implement the queue data structure with the following operations:
Queue() creates a new queue that is empty. It needs no parameters and returns nothing enqueue(item) adds a new Node with value=item to the tail of the queue. It needs the value of the Node and returns nothing.
dequeue() removes the head Node from the queue. It needs no parameters and returns the value of the Node removed from the queue. The queue is modified.
isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.
len() returns the number of items in the queue. It needs no parameters and returns an integer. EXAMPLE
>>> x=Queue()
>>> x.isEmpty()
True
>>> x.dequeue()
'Queue is empty' # None is fine too
>>> x.enqueue(1)
>>> x.enqueue(2)
>>> x.enqueue(3)
>>> x.enqueue(4)
>>> print(x)
Head:Node(1) Tail:Node(4) Queue:1 2 3 4 >>> x.isEmpty()
False
>>> len(x)
4
>>> x.dequeue()
1
>>> x.dequeue()
2
>>> x.dequeue()
3
>>> x.dequeue()
4
>>> x.dequeue()
'Queue is empty'
>>> print(x)
Head:None Tail:None Queue:
>>> x.enqueue(3)
>>> x.enqueue(2)
>>> print(x)
Head:Node(3) Tail:Node(2) Queue:3 2
>>> x.dequeue()
3
>>> print(x)
Head:Node(2) Tail:Node(2) Queue:2
Below is the given starter code
class Node: def __init__(self, value): self.value = value self.next = None def __str__(self): return "Node({})".format(self.value)
__repr__ = __str__ class Queue: ''' >>> x=Queue() >>> x.isEmpty() True >>> x.dequeue() 'Queue is empty' >>> x.enqueue(1) >>> x.enqueue(2) >>> x.enqueue(3) >>> x.dequeue() 1 >>> print(x) Head:Node(2) Tail:Node(3) Queue:2 3 ''' def __init__(self): self.head=None self.tail=None
def __str__(self): temp=self.head out=[] while temp: out.append(str(temp.value)) temp=temp.next out=' '.join(out) return ('Head:{} Tail:{} Queue:{}'.format(self.head,self.tail,out))
__repr__=__str__
def isEmpty(self): #write your code here
def __len__(self): #write your code here
def enqueue(self, value): #write your code here
def dequeue(self): #write your code here
Please write the code in Python 3. Thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started