- 注意
- 代码实现
- -- codeing = utf-8 --
# @Time : 2/6/2021 4:11 PM
# @Autor : Caesar
# @File : queue.py
# @ Software : PyCharm
_”””队尾添加|3|2|1|队头移除”””
_from collections import deque - Create a queue
queue = deque() - Add an element
# Time complexity : O(1)
queue.append(1)
queue.append(2)
queue.append(3)
# [1, 2, 3]
print(queue) - Get an element from the head of queue
# Time complexity: O(1)
tempt1 = queue[0]
print(tempt1) - Remove an element from the tail of queue
# Time complexity : O(1)
# 移除的操作是有返回值的,而列表中的移除:list.pop()是默认删除,不会以值传递的形式输出结果
tempt2 = queue.popleft()
print(tempt2)
# [2, 3] - Queue is empty?
# Time complexity: O(1)
length = len(queue)
print(length) - Time complexity: O(N)
while len(queue) != 0:
temp = queue.popleft()
print(temp)
注意
队列的创建方式,需要导入包
常见的操作:添加元素,移除元素,读取元素以及获取队列长度
队列的约束条件是:先进先出(FIFO), 队尾添加(类list.pop())队头移除