Files
hello-algo/docs/chapter_stack_and_queue/queue.md
T
Yudong JinandClaude Opus 5.5 3d53548b80 Move inline doc snippets and Tutor URLs into codes/snippets (#2032)
Book pages keep placeholders only; all code now lives under each edition's codes/.

- The 32 inline 13-language tab groups (17 pages) and the 18 PythonTutor URLs written
  directly in `??? pythontutor` blocks move, per edition, to
  `codes/snippets/<chapter>/<page>.md`. Pages refer to them with a `snippet` fence that
  holds the entry ID and with `@tutor <ID>`; the workspace build restores the exact
  previous Markdown, so the generated docs and site are unchanged.
- `codes/code_links.json`, the `src` mapping shared by every edition, moves here from
  the workspace repository unchanged, so a new `src` reference needs only a source PR.

Snippet code is kept byte for byte, including one Rust line with trailing spaces in
space_complexity. zh-hant is regenerated by OpenCC.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-15 23:38:00 +08:00

92 lines
4.2 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 队列
<u>队列(queue)</u>是一种遵循先入先出规则的线性数据结构。顾名思义,队列模拟了排队现象,即新来的人不断加入队列尾部,而位于队列头部的人逐个离开。
如下图所示,我们将队列头部称为“队首”,尾部称为“队尾”,将把元素加入队尾的操作称为“入队”,删除队首元素的操作称为“出队”。
![队列的先入先出规则](queue.assets/queue_operations.svg)
## 队列常用操作
队列的常见操作如下表所示。需要注意的是,不同编程语言的方法名称可能会有所不同。我们在此采用与栈相同的方法命名。
<p align="center"> 表 <id> &nbsp; 队列操作效率 </p>
| 方法名 | 描述 | 时间复杂度 |
| -------- | ---------------------------- | ---------- |
| `push()` | 元素入队,即将元素添加至队尾 | $O(1)$ |
| `pop()` | 队首元素出队 | $O(1)$ |
| `peek()` | 访问队首元素 | $O(1)$ |
我们可以直接使用编程语言中现成的队列类:
```snippet
queue_operations
```
??? pythontutor "可视化运行"
@tutor queue_operations_tutor
## 队列实现
为了实现队列,我们需要一种数据结构,可以在一端添加元素,并在另一端删除元素,链表和数组都符合要求。
### 基于链表的实现
如下图所示,我们可以将链表的“头节点”和“尾节点”分别视为“队首”和“队尾”,规定队尾仅可添加节点,队首仅可删除节点。
=== "<1>"
![基于链表实现队列的入队出队操作](queue.assets/linkedlist_queue_step1.svg)
=== "<2>"
![linkedlist_queue_push](queue.assets/linkedlist_queue_step2_push.svg)
=== "<3>"
![linkedlist_queue_pop](queue.assets/linkedlist_queue_step3_pop.svg)
以下是用链表实现队列的代码:
```src
[file]{linkedlist_queue}-[class]{linked_list_queue}-[func]{}
```
### 基于数组的实现
在数组中删除首元素的时间复杂度为 $O(n)$ ,这会导致出队操作效率较低。然而,我们可以采用以下巧妙方法来避免这个问题。
我们可以使用一个变量 `front` 指向队首元素的索引,并维护一个变量 `size` 用于记录队列长度。定义 `rear = front + size` ,这个公式计算出的 `rear` 指向队尾元素之后的下一个位置。
基于此设计,**数组中包含元素的有效区间为 `[front, rear - 1]`**,各种操作的实现方法如下图所示。
- 入队操作:将输入元素赋值给 `rear` 索引处,并将 `size` 增加 1 。
- 出队操作:只需将 `front` 增加 1 ,并将 `size` 减少 1 。
可以看到,入队和出队操作都只需进行一次操作,时间复杂度均为 $O(1)$ 。
=== "<1>"
![基于数组实现队列的入队出队操作](queue.assets/array_queue_step1.svg)
=== "<2>"
![array_queue_push](queue.assets/array_queue_step2_push.svg)
=== "<3>"
![array_queue_pop](queue.assets/array_queue_step3_pop.svg)
你可能会发现一个问题:在不断进行入队和出队的过程中,`front` 和 `rear` 都在向右移动,**当它们到达数组尾部时就无法继续移动了**。为了解决此问题,我们可以将数组视为首尾相接的“环形数组”。
对于环形数组,我们需要让 `front` 或 `rear` 在越过数组尾部时,直接回到数组头部继续遍历。这种周期性规律可以通过“取余操作”来实现,代码如下所示:
```src
[file]{array_queue}-[class]{array_queue}-[func]{}
```
以上实现的队列仍然具有局限性:其长度不可变。然而,这个问题不难解决,我们可以将数组替换为动态数组,从而引入扩容机制。有兴趣的读者可以尝试自行实现。
两种实现的对比结论与栈一致,在此不再赘述。
## 队列典型应用
- **淘宝订单**。购物者下单后,订单将加入队列中,系统随后会根据顺序处理队列中的订单。在双十一期间,短时间内会产生海量订单,高并发成为工程师们需要重点攻克的问题。
- **各类待办事项**。任何需要实现“先来后到”功能的场景,例如打印机的任务队列、餐厅的出餐队列等,队列在这些场景中可以有效地维护处理顺序。