myQueue

Run Settings
LanguagePython
Language Version
Run Command
class Node: def __init__(self, value, next=None): self.value = value self.next = next class Queue: def __init__(self): self.head = None self.tail = None self.length = 0 def peek(self): return self.head.value def enqueue(self, value): if self.isEmpty(): newNode = Node(value) self.tail = newNode self.head = newNode else: newNode = Node(value) self.tail.next = newNode self.tail = newNode self.length+=1 def dequeue(self): if self.isEmpty(): raise Exception("Queue is empty.") elif self.head == self.tail: returnVal = self.head.value self.head = None self.tail = None else: returnVal = self.head.value self.head = self.head.next self.length-=1 return returnVal def isEmpty(self): return (True if self.length == 0 else False) print("Hello World!") myQueue = Queue() print(myQueue.isEmpty()) assert(myQueue.isEmpty() == True) myQueue.enqueue("Jason") myQueue.enqueue("Joseph") assert(myQueue.isEmpty() == False) myQueue.enqueue("Caroline") assert(myQueue.peek()=="Jason") print(myQueue.dequeue()) assert(myQueue.peek()=="Joseph") print(myQueue.dequeue()) assert(myQueue.peek()=="Caroline") print(myQueue.dequeue()) assert(myQueue.isEmpty() == True) try: print(myQueue.dequeue()) except Exception as e: print(f"exception: {e}") print("Goodbye Cruel World")
Editor Settings
Theme
Key bindings
Full width
Lines