interactive-coding-challenges/stacks_queues/queue_list/queue_list.py

36 lines
906 B
Python
Raw Normal View History

class Node(object):
2015-07-03 11:05:10 +08:00
def __init__(self, data):
self.data = data
self.next = None
class Queue(object):
2015-07-03 11:05:10 +08:00
def __init__(self):
self.first = None
self.last = None
def enqueue(self, data):
node = Node(data)
if self.first is None and self.last is None:
self.first = node
self.last = node
else:
self.last.next = node
self.last = node
def dequeue(self):
# Empty list
if self.first is None and self.last is None:
return None
2015-07-03 11:05:10 +08:00
# Remove only element from a one element list
elif self.first == self.last:
data = self.first.data
self.first = None
self.last = None
return data
else:
data = self.first.data
self.first = self.first.next
return data