2015-05-15 18:14:39 +08:00
|
|
|
class Node(object):
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-05-15 18:14:39 +08:00
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
self.next = None
|
|
|
|
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-05-15 18:14:39 +08:00
|
|
|
class Queue(object):
|
2015-07-12 03:39:59 +08:00
|
|
|
|
2015-05-15 18:14:39 +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
|
2016-02-22 05:21:05 +08:00
|
|
|
data = self.first.data
|
2015-05-15 18:14:39 +08:00
|
|
|
# Remove only element from a one element list
|
2016-02-22 05:21:05 +08:00
|
|
|
if self.first == self.last:
|
2015-05-15 18:14:39 +08:00
|
|
|
self.first = None
|
|
|
|
self.last = None
|
|
|
|
else:
|
|
|
|
self.first = self.first.next
|
2016-02-22 05:21:05 +08:00
|
|
|
return data
|