Skip to content

Instantly share code, notes, and snippets.

@yasith
Created October 28, 2012 16:07
Show Gist options
  • Save yasith/3969012 to your computer and use it in GitHub Desktop.
Save yasith/3969012 to your computer and use it in GitHub Desktop.
Queue with a linked list
class EmptyQueueException(Exception):
pass
class LinkedListQueueNode:
def __init__(self, data):
self._next = None
self._data = data
def set_next(self, n):
self._next = n
def get_next(self):
return self._next
def get_data(self):
return self._data
class LinkedListQueue:
def __init__(self):
self._front = None
self._end = None
def enqueue(self, data):
new_node = LinkedListQueueNode(data)
if self._front == None:
self._front = self._end = new_node
else:
self._end.set_next(new_node)
def dequeue(self):
if self._front == None:
raise EmptyQueueException, "Queue is empty"
else:
value = self._front.get_data()
self._front = self._front.get_next()
return value
def front(self):
if self._front == None:
raise EmptyQueueException, "Queue is empty"
else:
return self._front.get_data()
def is_empty(self):
return self._front == None
def max(self):
if self._front == None:
raise EmptyQueueException, "Queue is empty"
else:
m = self._front.get_data()
node = self._front.get_next()
while node != None:
if m < node.get_data():
m = node.get_data
return m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment