2022-11-24 18:04:38 +00:00
|
|
|
'''
|
2022-12-01 16:09:34 +00:00
|
|
|
File: que.py
|
2022-11-30 04:15:50 +00:00
|
|
|
Created Time: 2022-11-29
|
|
|
|
Author: Peng Chen (pengchzn@gmail.com)
|
2022-11-24 18:04:38 +00:00
|
|
|
'''
|
|
|
|
|
2022-11-30 04:15:50 +00:00
|
|
|
import os.path as osp
|
|
|
|
import sys
|
|
|
|
|
2022-11-24 18:04:38 +00:00
|
|
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
|
|
|
from include import *
|
|
|
|
|
2022-11-30 04:15:50 +00:00
|
|
|
|
2022-12-01 16:09:34 +00:00
|
|
|
""" Driver Code """
|
|
|
|
if __name__ == "__main__":
|
2022-11-30 04:15:50 +00:00
|
|
|
""" 初始化队列 """
|
2022-12-01 16:09:34 +00:00
|
|
|
# 在 Python 中,我们一般将双向队列类 deque 看左队列使用
|
|
|
|
# 虽然 queue.Queue() 是纯正的队列类,但不太好用,因此不建议
|
|
|
|
que = collections.deque()
|
2022-11-30 04:15:50 +00:00
|
|
|
|
|
|
|
""" 元素入队 """
|
2022-12-01 16:09:34 +00:00
|
|
|
que.append(1)
|
|
|
|
que.append(3)
|
|
|
|
que.append(2)
|
|
|
|
que.append(5)
|
|
|
|
que.append(4)
|
|
|
|
print("队列 que =", que)
|
2022-11-30 04:15:50 +00:00
|
|
|
|
|
|
|
""" 访问队首元素 """
|
2022-12-01 16:09:34 +00:00
|
|
|
front = que[0];
|
|
|
|
print("队首元素 front =", front);
|
2022-11-30 04:15:50 +00:00
|
|
|
|
|
|
|
""" 元素出队 """
|
2022-12-01 16:09:34 +00:00
|
|
|
pop = que.popleft()
|
|
|
|
print("出队元素 pop =", pop)
|
|
|
|
print("出队后 que =", que)
|
2022-11-30 04:15:50 +00:00
|
|
|
|
|
|
|
""" 获取队列的长度 """
|
2022-12-01 16:09:34 +00:00
|
|
|
size = len(que)
|
|
|
|
print("队列长度 size =", size)
|
2022-11-30 04:15:50 +00:00
|
|
|
|
|
|
|
""" 判断队列是否为空 """
|
2022-12-01 16:09:34 +00:00
|
|
|
is_empty = len(que) == 0
|
|
|
|
print("队列是否为空 =", is_empty)
|