Skip to content

Instantly share code, notes, and snippets.

@restartthedream
Created December 7, 2014 21:39
Show Gist options
  • Save restartthedream/04593d987342e9dd6abc to your computer and use it in GitHub Desktop.
Save restartthedream/04593d987342e9dd6abc to your computer and use it in GitHub Desktop.
An example of in-class work. Construction of a FIFO Queue using nodes in python.
""" Name : Michael Minogue
Course : CMPS 1500-01
Lab Section : Tuesday 11:00 am-12:00 pm
Assignment : hw9pr1
Date : 11/10/2014
"""
class Node:
# data
# next
# constructor
def __init__(self, data):
self.data = data
self.next = None
def __str__(self):
return str(self.data)
class Queue:
# constructor
def __init__(self):
self.front = None
self.rear = None
self.size = 0
def __str__(self):
# front is head (first) node of list
current = self.front
thelist = ''
while(current!=None):
thelist = thelist + str(current) + ' '
current = current.next
return thelist
def enqueue(self,item):
if self.front == None: #Case if Queue is completely empty
self.front = Node(item)
self.size += 1
elif self.rear == None: #Checks if Queue has only 1 node
self.front.next = Node(item)
self.rear = self.front.next
self.size += 1
else: #Normal case to append to rear
self.rear.next = Node(item)
self.rear = self.rear.next
self.size += 1
def dequeue(self):
if self.size != 0: #Checks if Queue has nodes to remove
toreturn = self.front
self.front = self.front.next
self.size -= 1
return toreturn
else: #Case if Queue is empty
return None
def isEmpty(self):
if self.size == 0:
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment