"""
队列的链式存储
"""
class QueueError(Exception):
pass
# 创建结点类
class Node(object):
def __init__(self,value,next = None):
self.value = value
self.next = next
class Queue:
def __init__(self):
self.front = Node(None)
self.rear = self.front
# 判断是否为控队列
def is_empty(self):
return self.rear is self.front
# 入队列
def push(self,value):
self.rear.next = Node(value)
self.rear = self.rear.next
# 出队列
def pop(self):
if self.is_empty():
raise QueueError("空队列")
p = self.front
self.front = p.next
return p.next.value
# 清空队列
def clear(self):
self.front = self.rear
if __name__ == "__main__":
que = Queue()
print(que.is_empty())
que.push(10)
que.push(20)
que.push(30)
print(que.is_empty())
while not que.is_empty():
print(que.pop())
python队列的链式存储结构
最新推荐文章于 2020-01-10 20:21:47 发布