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):
|
2016-06-20 08:08:26 +08:00
|
|
|
self.head = None
|
|
|
|
self.tail = None
|
2015-05-15 18:14:39 +08:00
|
|
|
|
|
|
|
def enqueue(self, data):
|
|
|
|
node = Node(data)
|
2016-06-20 08:08:26 +08:00
|
|
|
# Empty list
|
|
|
|
if self.head is None and self.tail is None:
|
|
|
|
self.head = node
|
|
|
|
self.tail = node
|
2015-05-15 18:14:39 +08:00
|
|
|
else:
|
2016-06-20 08:08:26 +08:00
|
|
|
self.tail.next = node
|
|
|
|
self.tail = node
|
2015-05-15 18:14:39 +08:00
|
|
|
|
|
|
|
def dequeue(self):
|
|
|
|
# Empty list
|
2016-06-20 08:08:26 +08:00
|
|
|
if self.head is None and self.tail is None:
|
2015-05-15 18:14:39 +08:00
|
|
|
return None
|
2016-06-20 08:08:26 +08:00
|
|
|
data = self.head.data
|
2015-05-15 18:14:39 +08:00
|
|
|
# Remove only element from a one element list
|
2016-06-20 08:08:26 +08:00
|
|
|
if self.head == self.tail:
|
|
|
|
self.head = None
|
|
|
|
self.tail = None
|
2015-05-15 18:14:39 +08:00
|
|
|
else:
|
2016-06-20 08:08:26 +08:00
|
|
|
self.head = self.head.next
|
2016-02-22 05:21:05 +08:00
|
|
|
return data
|