Skip to content

Instantly share code, notes, and snippets.

@calthoff
Last active April 16, 2021 23:09
Show Gist options
  • Save calthoff/6be66ab72494da67c2301e43cd9f4846 to your computer and use it in GitHub Desktop.
Save calthoff/6be66ab72494da67c2301e43cd9f4846 to your computer and use it in GitHub Desktop.
class Node:
def __init__(self, data, next=None):
self.data = data
self.next = next
class Queue:
def __init__(self):
self.front = None
self.rear = None
self.size = 0
def enqueue(self, item):
self.size += 1
node = Node(item)
if self.front is None:
self.front = node
self.rear = node
else:
self.rear.next = node
self.rear = node
def dequeue(self):
self.size -= 1
if self.front is None:
raise IndexError('pop from empty queue')
temp = self.front
self.front = self.front.next
if self.front is None:
self.front = None
return temp.data
def size(self):
return self.size
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(5)
for i in range(5):
print(queue.dequeue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment