Skip to content

Instantly share code, notes, and snippets.

@j-quelly
Created May 9, 2019 20:10
Show Gist options
  • Save j-quelly/00135816b45966aa19e5cafa432f87bd to your computer and use it in GitHub Desktop.
Save j-quelly/00135816b45966aa19e5cafa432f87bd to your computer and use it in GitHub Desktop.
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Queue:
def __init__(self):
self.head = None
self.tail = None
self.num_elements = 0
def enqueue(self, value):
new_node = Node(value)
if self.head is None:
self.head = new_node
self.tail = self.head
else:
# add data to the next attribute of the tail (i.e. the end of the queue)
self.tail.next = new_node
# shift the tail (i.e., the back of the queue)
self.tail = self.tail.next
self.num_elements += 1
def dequeue(self):
if (self.is_empty()):
return None
value = self.head.value
self.head = self.head.next
self.num_elements -= 1
return value
def size(self):
return self.num_elements
def is_empty(self):
return self.num_elements == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment