2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
File: deque.py
|
2022-11-30 12:15:50 +08:00
|
|
|
Created Time: 2022-11-29
|
|
|
|
Author: Peng Chen (pengchzn@gmail.com)
|
2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
import sys, os.path as osp
|
2022-11-25 02:04:38 +08:00
|
|
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
2023-03-03 03:07:22 +08:00
|
|
|
from modules import *
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2022-12-02 00:09:34 +08:00
|
|
|
|
|
|
|
""" Driver Code """
|
2022-11-30 12:15:50 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
""" 初始化双向队列 """
|
2023-03-12 18:49:52 +08:00
|
|
|
deque: Deque[int] = collections.deque()
|
2022-11-30 12:15:50 +08:00
|
|
|
|
|
|
|
""" 元素入队 """
|
2023-03-12 02:45:10 +08:00
|
|
|
deque.append(2) # 添加至队尾
|
|
|
|
deque.append(5)
|
|
|
|
deque.append(4)
|
|
|
|
deque.appendleft(3) # 添加至队首
|
|
|
|
deque.appendleft(1)
|
|
|
|
print("双向队列 deque =", deque)
|
2022-11-30 12:15:50 +08:00
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
""" 访问元素 """
|
2023-03-12 18:49:52 +08:00
|
|
|
front: int = deque[0] # 队首元素
|
2022-12-02 00:09:34 +08:00
|
|
|
print("队首元素 front =", front)
|
2023-03-12 18:49:52 +08:00
|
|
|
rear: int = deque[-1] # 队尾元素
|
2022-12-02 00:09:34 +08:00
|
|
|
print("队尾元素 rear =", rear)
|
2022-11-30 12:15:50 +08:00
|
|
|
|
|
|
|
""" 元素出队 """
|
2023-03-12 18:49:52 +08:00
|
|
|
pop_front: int = deque.popleft() # 队首元素出队
|
2022-12-02 00:09:34 +08:00
|
|
|
print("队首出队元素 pop_front =", pop_front)
|
2023-03-12 02:45:10 +08:00
|
|
|
print("队首出队后 deque =", deque)
|
2023-03-12 18:49:52 +08:00
|
|
|
pop_rear: int = deque.pop() # 队尾元素出队
|
2022-12-02 00:09:34 +08:00
|
|
|
print("队尾出队元素 pop_rear =", pop_rear)
|
2023-03-12 02:45:10 +08:00
|
|
|
print("队尾出队后 deque =", deque)
|
2022-11-30 12:15:50 +08:00
|
|
|
|
2022-12-01 06:53:08 +08:00
|
|
|
""" 获取双向队列的长度 """
|
2023-03-12 18:49:52 +08:00
|
|
|
size: int = len(deque)
|
2022-12-02 00:09:34 +08:00
|
|
|
print("双向队列长度 size =", size)
|
2022-11-30 12:15:50 +08:00
|
|
|
|
2022-12-01 06:53:08 +08:00
|
|
|
""" 判断双向队列是否为空 """
|
2023-03-12 18:49:52 +08:00
|
|
|
is_empty: bool = len(deque) == 0
|
2022-12-02 00:09:34 +08:00
|
|
|
print("双向队列是否为空 =", is_empty)
|