第21课 队列

定义

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个整数,按从小到大 顺序放入队列,然后出队列打印。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容