定义
queue 是一种线性数据结构,它以先进先出 (FIFO) 方式存储项目。
image.png
- enqueue: 入队列
- dequeue: 出队列
- front: 前
- real: 尾部
# Python program to
# demonstrate queue implementation
# using list
# Initializing a queue
queue = []
# Adding elements to the queue
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
# Removing elements from the queue
print("\nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("\nQueue after removing elements")
print(queue)
# Uncommenting print(queue.pop(0))
# will raise and IndexError
# as the queue is now empty
习题
函数实现enqueue, dequeue。 提供enqueue/dequeue 函数供调用。
随机生成100个整数,按从小到大 顺序放入队列,然后出队列打印。