# 队列（抽象数据类型）

队列是一种抽象数据类型，它存储一个元素集合，并按先进先出（FIFO）的顺序取出元素：最先加入的元素最先被移除。新元素从一端加入，这一端称为队尾（rear 或 tail），对应的操作通常命名为入队（enqueue）；元素从另一端移除，这一端称为队头（front 或 head），对应的操作命名为出队（dequeue）。<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup><sup> • </sup><sup>[2](https://xlinux.nist.gov/dads/HTML/queue.html)</sup> 美国国家标准与技术研究院（NIST）的《算法与数据结构词典》将其定义为一个只能访问最早加入的元素的集合。<sup>[2](https://xlinux.nist.gov/dads/HTML/queue.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 排序规则 | 先进先出（FIFO），也称后进后出（LILO）；一个元素只有在它前面的所有元素都被移除后才会被取出<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup> |
| 核心操作 | 入队（在队尾添加）、出队（从队头移除并返回）、队头或窥视（查看队头元素但不移除）、判空测试；出队和队头操作在空队列上无效<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup><sup> • </sup><sup>[3](https://courses.cis.cornell.edu/courses/cs2110/2026fa/lectures/lec15/)</sup> |
| 访问限制 | 操作不允许访问最后或中间的元素，只能访问第一个；队头操作返回第一个元素而不移除它，其作用类似于栈的取顶（Top）操作<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup><sup> • </sup><sup>[15](https://cs.berea.edu/cppds/BasicDS/TheQueueAbstractDataType.html)</sup> |
| 典型开销 | 用链表将队头存放在表头、在表尾插入时，所有队列操作的最坏情况时间复杂度均为 O(1)<sup>[3](https://courses.cis.cornell.edu/courses/cs2110/2026fa/lectures/lec15/)</sup> |
| 与栈的对比 | 栈是后进先出（LIFO），只在一端操作；队列两端开放，一端用于插入，一端用于移除<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup> |
| 标准库形式 | C++ 的 `std::queue` 是建立在序列容器之上的容器适配器，默认底层容器为 `std::deque`；Python 的 `queue` 模块提供线程安全的 FIFO 队列，`collections.deque` 提供快速的原子 `append()` 和 `popleft()`<sup>[4](https://en.cppreference.com/cpp/container/queue)</sup><sup> • </sup><sup>[5](https://docs.python.org/3.15/library/queue.html)</sup> |
| 分布式形式 | 亚马逊简单队列服务（Amazon Simple Queue Service）提供托管队列，标准队列提供至少一次投递和尽力排序，FIFO 队列提供恰好一次处理，并在消息组内保持顺序<sup>[6](https://aws.amazon.com/documentation-overview/sqs/)</sup> |

## 操作

队列抽象数据类型由其操作而非实现来规定。一组常见的形式化操作包括：创建（Create，新建一个空队列）、判空（Empty，测试队列是否不含元素）、入队（Enqueue，在队尾添加新元素）、出队（Dequeue，移除并返回队头元素）、取队头（Front，返回队头元素而不移除）。出队和取队头不能在空队列上执行。<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup> 不同文献中操作名称各异：添加元素可能称为 Enter、Insert 或 Enqueue，移除元素可能称为 Delete、Remove 或 Dequeue。<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup>

NIST 词典给出了一个公理化定义，其中出队被分解为移除（remove）和取队头（front），公理如 front(add(v, new())) = v 和 remove(add(v, new())) = new()，其中 new() 返回一个空队列。<sup>[2](https://xlinux.nist.gov/dads/HTML/queue.html)</sup> 有界队列具有最大容量和判满测试，被视为一般队列的一种特化。<sup>[2](https://xlinux.nist.gov/dads/HTML/queue.html)</sup>

## 实现

**链表。** 将队列的队头放在单链表的表头、在表尾进行插入时，单链表可以高效地支持队列。这避免了从单链表尾部移除元素所需的 O(N) 时间，于是每个队列操作都具有 O(1) 的最坏情况时间复杂度。<sup>[3](https://courses.cis.cornell.edu/courses/cs2110/2026fa/lectures/lec15/)</sup> 该实现维护两个指针：一个指向表头，即下一个将被移除的元素；一个指向表尾，即最后加入的元素。<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup>

**循环数组。** 朴素的数组实现面临一个问题：随着元素从队头出队，数组开头会浪费空间。每次出队后将所有剩余元素左移可以填补这些空间，但效率低下。标准的解决办法是循环缓冲区：把位置 0 视为跟在数组最后一个位置之后，这样当队尾索引到达数组末尾时便绕回开头，重新利用起始处释放的空间。索引递增使用模运算，例如 `first = (first + 1) % MaxQueue`，并用一个元素计数来区分满和空。<sup>[1](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)</sup>

**为何选择很重要。** 可增长数组在一端很快：在末尾追加和弹出是摊还 O(1)，但在前端插入或移除是 O(n)，因为后面每个元素都必须移动。链表恰好相反：只要持有指向某一端的引用，在任一端操作都是 O(1)，但按索引访问是 O(n)。这就是为什么可增长数组适合做栈而不适合做队列，而链表适合做队列。<sup>[7](https://www.cs.dartmouth.edu/~devin/courses/cs10/02_data_structures/03_runtime_analysis/)</sup>

## 用途

**图搜索。** [广度优先搜索](https://www.edgepedia.org/zh/breadth-first-search)由其前沿数据结构定义：当前沿是队列时，搜索先出队最早发现的顶点，对搜索树进行广度优先遍历。<sup>[8](https://www.usna.edu/Users/cs/wcbrown/courses/S18SI335/lec/l14/lec.html)</sup> 队列按顶点被发现的顺序交还顶点，因此同一距离的整圈顶点会在下一圈进入之前全部处理完毕。<sup>[9](https://www.classes.cs.uchicago.edu/current/14300-1/lectures/lec14_notes/)</sup>

**操作系统。** 调度器将可运行的进程和线程保存在队列中。在多队列调度中，可运行线程按优先级或时间片长度分开，每个队列按轮转方式调度，调度器先选择高优先级队列，再选择低优先级队列。<sup>[10](https://os.cs.luc.edu/scheduling.html)</sup>

**分布式系统。** 消息队列将软件组件解耦。亚马逊 SQS 是一种托管队列服务，支持标准队列和 FIFO 队列：标准队列至少投递一次，仅尽力保证顺序；FIFO 队列设计为恰好一次处理，并在每个消息组内保持顺序；死信队列处理消费者未能成功处理的消息。<sup>[6](https://aws.amazon.com/documentation-overview/sqs/)</sup><sup> • </sup><sup>[11](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-best-practices.html)</sup>

## 相关结构

双端队列（deque）推广了队列，允许在两端进行插入和删除；C++ 的 `std::queue` 实际上是一个容器适配器，其默认底层容器就是 `std::deque`，而 Java 类库通过单一的 `Deque` 接口同时建模栈和队列。<sup>[4](https://en.cppreference.com/cpp/container/queue)</sup><sup> • </sup><sup>[3](https://courses.cis.cornell.edu/courses/cs2110/2026fa/lectures/lec15/)</sup> 优先队列偏离了 FIFO，它按值而非到达顺序取出条目：Python 的 `queue.PriorityQueue` 保持条目有序，最先取出值最小的条目，通常以 `(priority_number, data)` 元组的形式存储。<sup>[5](https://docs.python.org/3.15/library/queue.html)</sup> Python 的 `queue` 模块实现了三种队列类型——FIFO、LIFO 和优先队列——它们使用锁来阻塞竞争的线程，而 `collections.deque` 则充当无界队列，提供无需加锁的快速原子 `append()` 和 `popleft()` 操作。<sup>[5](https://docs.python.org/3.15/library/queue.html)</sup>

## 历史

队列的起源在于对排队现象的数学研究——[排队论](https://www.edgepedia.org/zh/queueing-theory)，而非编程。阿格纳·克拉鲁普·厄朗（1878 年至 1929 年）是一位丹麦数学家、统计学家和工程师，任职于哥本哈根电话公司，他开创了交通工程和排队论领域，并于 1909 年发表了他的第一篇关于这些问题的论文《概率论与电话通话》。<sup>[12](https://mathshistory.st-andrews.ac.uk/Biographies/Erlang/)</sup> 排队论后来在第二次世界大战期间塑造了运筹学，源自这一传统的[利特尔法则](https://www.edgepedia.org/zh/littles-law)对运营管理和计算机体系结构产生了重大影响。<sup>[13](https://www.informs.org/Explore/History-of-O.R.-Excellence/O.R.-Methodologies/Queueing-Models)</sup> 到 20 世纪 70 年代，队列已成为编程语言中的标准抽象数据类型，并出现了链表、数组和循环缓冲区等实现方式。<sup>[14](https://www.cs.pomona.edu/classes/cs62/archive/sp2026/history/stack%26queue/)</sup>

## References

1. [CSC 161: Queue ADT, Grinnell College](https://weinman.cs.grinnell.edu/courses/CSC161/2015S/modules/pointers-stacks-queues/readings/queues.shtml)
2. [Paul E. Black, "queue", Dictionary of Algorithms and Data Structures, NIST](https://xlinux.nist.gov/dads/HTML/queue.html)
3. [CS 2110 Lecture 15: Stacks and Queues, Cornell University](https://courses.cis.cornell.edu/courses/cs2110/2026fa/lectures/lec15/)
4. [std::queue, cppreference.com](https://en.cppreference.com/cpp/container/queue)
5. [queue, A synchronized queue class, Python 3 documentation](https://docs.python.org/3.15/library/queue.html)
6. [Amazon Simple Queue Service Documentation, AWS](https://aws.amazon.com/documentation-overview/sqs/)
7. [Analysis of data structures, Dartmouth CS 10](https://www.cs.dartmouth.edu/~devin/courses/cs10/02_data_structures/03_runtime_analysis/)
8. [SI335: Graph Algorithms, Search, United States Naval Academy](https://www.usna.edu/Users/cs/wcbrown/courses/S18SI335/lec/l14/lec.html)
9. [Lecture 14: Trees and Graphs, Systems Programming I, University of Chicago](https://www.classes.cs.uchicago.edu/current/14300-1/lectures/lec14_notes/)
10. [Process/Thread Scheduling, Introduction to Operating Systems](https://os.cs.luc.edu/scheduling.html)
11. [Amazon SQS best practices, AWS](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-best-practices.html)
12. [Agner Erlang (1878 to 1929), MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk/Biographies/Erlang/)
13. [Queueing Models, INFORMS History of O.R. Excellence](https://www.informs.org/Explore/History-of-O.R.-Excellence/O.R.-Methodologies/Queueing-Models)
14. [History of Stacks and Queues, CS62, Pomona College](https://www.cs.pomona.edu/classes/cs62/archive/sp2026/history/stack%26queue/)
15. [3.11. The Queue Abstract Data Type, Problem Solving with Algorithms and Data Structures](https://cs.berea.edu/cppds/BasicDS/TheQueueAbstractDataType.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › Stacks, queues, and deques

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/queue-abstract-data-type

English original: https://www.edgepedia.org/queue-abstract-data-type
