由于队列的动态由队头指针与队尾指针共同反映,所以我们在实现先入后出的同时还要实现队头元素与队尾元素的访问。对于普通的队列,我们使用列表实现其顺序存储,使用其它方法实现其链式存储。
顺序存储
由于我们使用list作为queue的底层、用Queue类对list进行了简单封装,所以在顺序存储结构中我们可以方便的利用列表的方法。具体代码:
class Queue(object):
'定义队列。'
def __init__(self):
'初始化一个空列表。'
self.items = []
def IsEmpty(self):
'方法:判断队列是否为空。'
return self.items == []
def get_size(self):
'方法:求队列长度。'
return len(self.items)
def get_TopValue(self):
'方法:访问队头元素。'
if self.IsEmpty():
return None
else:
return self.items[0]
def get_BottomValue(self):
'方法:访问队尾元素。'
if self.IsEmpty():
return None
else:
return self.items[-1]
def push(self,data):
'方法:进队。'
self.items.append(data)
def pop(self):
'方法:出队。'
if self.IsEmpty():
raise IndexError('队列为空。')
else:
self.items.pop(0)
简单测试:
my_queue = Queue()
print('Is my queue empty?\n{}'.format(my_queue.IsEmpty()))
[my_queue.push(i + 1) for i in range(10)]
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))
for i in range(11):
print('第{}次:'.format(i + 1))
my_queue.pop()
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))
测试结果:
Is my queue empty?
True
The top value is 1
The bottom value is 10
第1次:
The top value is 2
The bottom value is 10
第2次:
The top value is 3
The bottom value is 10
第3次:
The top value is 4
The bottom value is 10
第4次:
The top value is 5
The bottom value is 10
第5次:
The top value is 6
The bottom value is 10
第6次:
The top value is 7
The bottom value is 10
第7次:
The top value is 8
The bottom value is 10
第8次:
The top value is 9
The bottom value is 10
第9次:
The top value is 10
The bottom value is 10
第10次:
The top value is None
The bottom value is None
第11次:
Traceback (most recent call last):
File "带链的队列.py", line 58, in <module>
my_queue.pop()
File "带链的队列.py", line 44, in pop
raise IndexError('队列为空。')
IndexError: 队列为空。
链式存储
我们通过实例化一个Node类来表示节点,其中存储有数据值data和后件next,具体代码为
class Node(object):
'定义节点。'
def __init__(self):
'初始化数据域和指针域。'
self.data = None
self.next = None
class Queue(object):
'定义队列。'
def __init__(self):
'初始化队头top与队尾bottom。'
self.top = None
self.bottom = None
self.size = 0
def IsEmpty(self):
'方法:判断是否为空队。'
if self.size == 0:
return True
else:
return False
def get_size(self):
'方法:得到队列大小。'
return self.size
def get_TopValue(self):
'方法:访问队头元素。'
if self.IsEmpty():
return None
else:
return self.top.data
def get_BottomValue(self):
'方法:访问队尾元素。'
if self.IsEmpty():
return None
else:
return self.bottom.data
def push(self,data):
'方法:入队。'
node = Node()
node.data = data
if self.IsEmpty():
self.top = node
self.bottom = node
else:
self.bottom.next = node
self.bottom = node
self.size += 1
def pop(self):
'方法:出队。'
if not self.IsEmpty():
self.top = self.top.next
else:
raise IndexError('队列为空。')
self.size -= 1
简单测试:
my_queue = Queue()
print('Is my queue empty?\n{}'.format(my_queue.IsEmpty()))
[my_queue.push(i + 1) for i in range(10)]
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))
print('The size of my queue is {}'.format(my_queue.get_size()))
for i in range(11):
print('第{}次:'.format(i + 1))
my_queue.pop()
print('The top value is {}'.format(my_queue.get_TopValue()))
print('The bottom value is {}'.format(my_queue.get_BottomValue()))
print('The size of my queue is {}'.format(my_queue.get_size()))
测试结果:
Is my queue empty?
True
The top value is 1
The bottom value is 10
The size of my queue is 10
第1次:
The top value is 2
The bottom value is 10
The size of my queue is 9
第2次:
The top value is 3
The bottom value is 10
The size of my queue is 8
第3次:
The top value is 4
The bottom value is 10
The size of my queue is 7
第4次:
The top value is 5
The bottom value is 10
The size of my queue is 6
第5次:
The top value is 6
The bottom value is 10
The size of my queue is 5
第6次:
The top value is 7
The bottom value is 10
The size of my queue is 4
第7次:
The top value is 8
The bottom value is 10
The size of my queue is 3
第8次:
The top value is 9
The bottom value is 10
The size of my queue is 2
第9次:
The top value is 10
The bottom value is 10
The size of my queue is 1
第10次:
The top value is None
The bottom value is None
The size of my queue is 0
第11次:
Traceback (most recent call last):
File "带链的队列.py", line 84, in <module>
my_queue.pop()
File "带链的队列.py", line 70, in pop
raise IndexError('队列为空。')
IndexError: 队列为空。