In: Computer Science
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class
class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
class Node: def __init__(self, data): self.data = data self.next = None def __str__(self): return str(self.data) class LinkedList: def __init__(self): self.head = None self.tail = None self.count = 0 def append(self, item): n = Node(item) if self.head is None: self.head = n self.tail = n else: self.tail.next = n self.tail = n self.count += 1 def remove_head(self): if not self.is_empty(): value = self.head self.head = self.head.next if self.head is None: self.tail = None self.count -= 1 return value def is_empty(self): return self.head is None def __len__(self): return self.count class Queue: def __init__(self): self.items = LinkedList() # has to be O(1) def enqueue(self, item): self.items.append(item) # has to be O(1) def dequeue(self): return self.items.remove_head() def is_empty(self): return self.items.is_empty() def __len__(self): return self.items.count # Testing the classes here. ignore/remove the code below if not required q = Queue() q.enqueue(4) q.enqueue(3) q.enqueue(9) q.enqueue(1) q.enqueue(6) print(len(q)) print(q.is_empty()) while not q.is_empty(): print(q.dequeue())