2023-02-28 20:03:53 +08:00
|
|
|
|
# 队列
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2024-04-03 03:52:17 +08:00
|
|
|
|
<u>队列(queue)</u>是一种遵循先入先出规则的线性数据结构。顾名思义,队列模拟了排队现象,即新来的人不断加入队列尾部,而位于队列头部的人逐个离开。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
如下图所示,我们将队列头部称为“队首”,尾部称为“队尾”,将把元素加入队尾的操作称为“入队”,删除队首元素的操作称为“出队”。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![队列的先入先出规则](queue.assets/queue_operations.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 队列常用操作
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
队列的常见操作如下表所示。需要注意的是,不同编程语言的方法名称可能会有所不同。我们在此采用与栈相同的方法命名。
|
2023-08-20 23:28:30 +08:00
|
|
|
|
|
2023-08-22 13:51:25 +08:00
|
|
|
|
<p align="center"> 表 <id> 队列操作效率 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
| 方法名 | 描述 | 时间复杂度 |
|
|
|
|
|
| -------- | ---------------------------- | ---------- |
|
|
|
|
|
| `push()` | 元素入队,即将元素添加至队尾 | $O(1)$ |
|
|
|
|
|
| `pop()` | 队首元素出队 | $O(1)$ |
|
|
|
|
|
| `peek()` | 访问队首元素 | $O(1)$ |
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
我们可以直接使用编程语言中现成的队列类:
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Python"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```python title="queue.py"
|
2023-12-02 06:21:34 +08:00
|
|
|
|
from collections import deque
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 初始化队列
|
2023-12-02 06:21:34 +08:00
|
|
|
|
# 在 Python 中,我们一般将双向队列类 deque 当作队列使用
|
|
|
|
|
# 虽然 queue.Queue() 是纯正的队列类,但不太好用,因此不推荐
|
|
|
|
|
que: deque[int] = deque()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 元素入队
|
|
|
|
|
que.append(1)
|
|
|
|
|
que.append(3)
|
|
|
|
|
que.append(2)
|
|
|
|
|
que.append(5)
|
|
|
|
|
que.append(4)
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 访问队首元素
|
2024-03-14 20:01:16 +08:00
|
|
|
|
front: int = que[0]
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 元素出队
|
|
|
|
|
pop: int = que.popleft()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 获取队列的长度
|
|
|
|
|
size: int = len(que)
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
# 判断队列是否为空
|
|
|
|
|
is_empty: bool = len(que) == 0
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="queue.cpp"
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
queue<int> queue;
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.push(1);
|
|
|
|
|
queue.push(3);
|
|
|
|
|
queue.push(2);
|
|
|
|
|
queue.push(5);
|
|
|
|
|
queue.push(4);
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int front = queue.front();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
queue.pop();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
int size = queue.size();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
bool empty = queue.empty();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Java"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
```java title="queue.java"
|
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
Queue<Integer> queue = new LinkedList<>();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.offer(1);
|
|
|
|
|
queue.offer(3);
|
|
|
|
|
queue.offer(2);
|
|
|
|
|
queue.offer(5);
|
|
|
|
|
queue.offer(4);
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int peek = queue.peek();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
int pop = queue.poll();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
int size = queue.size();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
boolean isEmpty = queue.isEmpty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="queue.cs"
|
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
Queue<int> queue = new();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.Enqueue(1);
|
|
|
|
|
queue.Enqueue(3);
|
|
|
|
|
queue.Enqueue(2);
|
|
|
|
|
queue.Enqueue(5);
|
|
|
|
|
queue.Enqueue(4);
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int peek = queue.Peek();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
int pop = queue.Dequeue();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
int size = queue.Count;
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
bool isEmpty = queue.Count == 0;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-15 23:30:53 +08:00
|
|
|
|
```go title="queue_test.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
// 在 Go 中,将 list 作为队列来使用
|
|
|
|
|
queue := list.New()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.PushBack(1)
|
|
|
|
|
queue.PushBack(3)
|
|
|
|
|
queue.PushBack(2)
|
|
|
|
|
queue.PushBack(5)
|
|
|
|
|
queue.PushBack(4)
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
peek := queue.Front()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
|
pop := queue.Front()
|
|
|
|
|
queue.Remove(pop)
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
size := queue.Len()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
isEmpty := queue.Len() == 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="queue.swift"
|
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
// Swift 没有内置的队列类,可以把 Array 当作队列来使用
|
|
|
|
|
var queue: [Int] = []
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.append(1)
|
|
|
|
|
queue.append(3)
|
|
|
|
|
queue.append(2)
|
|
|
|
|
queue.append(5)
|
|
|
|
|
queue.append(4)
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
let peek = queue.first!
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
// 由于是数组,因此 removeFirst 的复杂度为 O(n)
|
|
|
|
|
let pool = queue.removeFirst()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
let size = queue.count
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
let isEmpty = queue.isEmpty
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="queue.js"
|
2022-12-05 22:07:41 +08:00
|
|
|
|
/* 初始化队列 */
|
2022-12-20 21:33:14 +08:00
|
|
|
|
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
2022-12-05 22:06:31 +08:00
|
|
|
|
const queue = [];
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.push(1);
|
|
|
|
|
queue.push(3);
|
|
|
|
|
queue.push(2);
|
|
|
|
|
queue.push(5);
|
|
|
|
|
queue.push(4);
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
const peek = queue[0];
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 元素出队 */
|
2022-12-20 21:33:14 +08:00
|
|
|
|
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
2023-03-13 21:58:21 +08:00
|
|
|
|
const pop = queue.shift();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
const size = queue.length;
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
const empty = queue.length === 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="queue.ts"
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 初始化队列 */
|
2024-04-07 18:58:56 +08:00
|
|
|
|
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
2022-12-05 22:06:31 +08:00
|
|
|
|
const queue: number[] = [];
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.push(1);
|
|
|
|
|
queue.push(3);
|
|
|
|
|
queue.push(2);
|
|
|
|
|
queue.push(5);
|
|
|
|
|
queue.push(4);
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
const peek = queue[0];
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 元素出队 */
|
2022-12-20 21:33:14 +08:00
|
|
|
|
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
2023-03-13 21:58:21 +08:00
|
|
|
|
const pop = queue.shift();
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
const size = queue.length;
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
const empty = queue.length === 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="queue.dart"
|
2023-06-02 14:56:29 +08:00
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
// 在 Dart 中,队列类 Qeque 是双向队列,也可作为队列使用
|
|
|
|
|
Queue<int> queue = Queue();
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.add(1);
|
|
|
|
|
queue.add(3);
|
|
|
|
|
queue.add(2);
|
|
|
|
|
queue.add(5);
|
|
|
|
|
queue.add(4);
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int peek = queue.first;
|
2023-06-02 02:40:26 +08:00
|
|
|
|
|
2023-06-02 14:56:29 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
int pop = queue.removeFirst();
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
int size = queue.length;
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
bool isEmpty = queue.isEmpty;
|
2023-06-02 02:40:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="queue.rs"
|
2023-09-17 00:39:57 +08:00
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
// 在 Rust 中使用双向队列作为普通队列来使用
|
|
|
|
|
let mut deque: VecDeque<u32> = VecDeque::new();
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-09-17 00:39:57 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.push_back(1);
|
|
|
|
|
deque.push_back(3);
|
|
|
|
|
deque.push_back(2);
|
|
|
|
|
deque.push_back(5);
|
|
|
|
|
deque.push_back(4);
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
if let Some(front) = deque.front() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
|
if let Some(pop) = deque.pop_front() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
let size = deque.len();
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
let is_empty = deque.is_empty();
|
2023-07-26 11:00:53 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="queue.c"
|
|
|
|
|
// C 未提供内置队列
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 03:15:36 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="queue.kt"
|
2024-03-30 13:16:39 +08:00
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
val queue = LinkedList<Int>()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2024-03-30 13:16:39 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.offer(1)
|
|
|
|
|
queue.offer(3)
|
|
|
|
|
queue.offer(2)
|
|
|
|
|
queue.offer(5)
|
|
|
|
|
queue.offer(4)
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2024-03-30 13:16:39 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
val peek = queue.peek()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2024-03-30 13:16:39 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
val pop = queue.poll()
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2024-03-30 13:16:39 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
val size = queue.size
|
2024-04-07 18:58:56 +08:00
|
|
|
|
|
2024-03-30 13:16:39 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
val isEmpty = queue.isEmpty()
|
2024-03-25 03:15:36 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-31 03:57:11 +08:00
|
|
|
|
=== "Ruby"
|
|
|
|
|
|
|
|
|
|
```ruby title="queue.rb"
|
2024-04-07 18:58:56 +08:00
|
|
|
|
# 初始化队列
|
|
|
|
|
# Ruby 内置的队列(Thread::Queue) 没有 peek 和遍历方法,可以把 Array 当作队列来使用
|
|
|
|
|
queue = []
|
|
|
|
|
|
|
|
|
|
# 元素入队
|
|
|
|
|
queue.push(1)
|
|
|
|
|
queue.push(3)
|
|
|
|
|
queue.push(2)
|
|
|
|
|
queue.push(5)
|
|
|
|
|
queue.push(4)
|
2024-03-31 03:57:11 +08:00
|
|
|
|
|
2024-04-07 18:58:56 +08:00
|
|
|
|
# 访问队列元素
|
|
|
|
|
peek = queue.first
|
|
|
|
|
|
|
|
|
|
# 元素出队
|
|
|
|
|
# 清注意,由于是数组,Array#shift 方法时间复杂度为 O(n)
|
|
|
|
|
pop = queue.shift
|
|
|
|
|
|
|
|
|
|
# 获取队列的长度
|
|
|
|
|
size = queue.length
|
|
|
|
|
|
|
|
|
|
# 判断队列是否为空
|
|
|
|
|
is_empty = queue.empty?
|
2024-03-31 03:57:11 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:19:08 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="queue.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 04:04:01 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:09:35 +08:00
|
|
|
|
https://pythontutor.com/render.html#code=from%20collections%20import%20deque%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%98%9F%E5%88%97%0A%20%20%20%20%23%20%E5%9C%A8%20Python%20%E4%B8%AD%EF%BC%8C%E6%88%91%E4%BB%AC%E4%B8%80%E8%88%AC%E5%B0%86%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E7%B1%BB%20deque%20%E7%9C%8B%E4%BD%9C%E9%98%9F%E5%88%97%E4%BD%BF%E7%94%A8%0A%20%20%20%20%23%20%E8%99%BD%E7%84%B6%20queue.Queue%28%29%20%E6%98%AF%E7%BA%AF%E6%AD%A3%E7%9A%84%E9%98%9F%E5%88%97%E7%B1%BB%EF%BC%8C%E4%BD%86%E4%B8%8D%E5%A4%AA%E5%A5%BD%E7%94%A8%0A%20%20%20%20que%20%3D%20deque%28%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E9%98%9F%0A%20%20%20%20que.append%281%29%0A%20%20%20%20que.append%283%29%0A%20%20%20%20que.append%282%29%0A%20%20%20%20que.append%285%29%0A%20%20%20%20que.append%284%29%0A%20%20%20%20print%28%22%E9%98%9F%E5%88%97%20que%20%3D%22,%20que%29%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%0A%20%20%20%20front%20%3D%20que%5B0%5D%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%20front%20%3D%22,%20front%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20pop%20%3D%20que.popleft%28%29%0A%20%20%20%20print%28%22%E5%87%BA%E9%98%9F%E5%85%83%E7%B4%A0%20pop%20%3D%22,%20pop%29%0A%20%20%20%20print%28%22%E5%87%BA%E9%98%9F%E5%90%8E%20que%20%3D%22,%20que%29%0A%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E9%98%9F%E5%88%97%E7%9A%84%E9%95%BF%E5%BA%A6%0A%20%20%20%20size%20%3D%20len%28que%29%0A%20%20%20%20print%28%22%E9%98%9F%E5%88%97%E9%95%BF%E5%BA%A6%20size%20%3D%22,%20size%29%0A%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20len%28que%29%20%3D%3D%200%0A%20%20%20%20print%28%22%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%20%3D%22,%20is_empty%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
2024-01-07 04:04:01 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 队列实现
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
为了实现队列,我们需要一种数据结构,可以在一端添加元素,并在另一端删除元素,链表和数组都符合要求。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
### 基于链表的实现
|
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
如下图所示,我们可以将链表的“头节点”和“尾节点”分别视为“队首”和“队尾”,规定队尾仅可添加节点,队首仅可删除节点。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-19 02:04:21 +08:00
|
|
|
|
=== "LinkedListQueue"
|
2024-02-14 18:37:18 +08:00
|
|
|
|
![基于链表实现队列的入队出队操作](queue.assets/linkedlist_queue_step1.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
|
|
|
|
=== "push()"
|
2024-02-14 18:37:18 +08:00
|
|
|
|
![linkedlist_queue_push](queue.assets/linkedlist_queue_step2_push.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "pop()"
|
2024-02-14 18:37:18 +08:00
|
|
|
|
![linkedlist_queue_pop](queue.assets/linkedlist_queue_step3_pop.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
以下是用链表实现队列的代码:
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-10-17 01:06:00 +08:00
|
|
|
|
```src
|
|
|
|
|
[file]{linkedlist_queue}-[class]{linked_list_queue}-[func]{}
|
|
|
|
|
```
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
### 基于数组的实现
|
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
在数组中删除首元素的时间复杂度为 $O(n)$ ,这会导致出队操作效率较低。然而,我们可以采用以下巧妙方法来避免这个问题。
|
2023-02-16 03:03:02 +08:00
|
|
|
|
|
2023-08-31 02:32:20 +08:00
|
|
|
|
我们可以使用一个变量 `front` 指向队首元素的索引,并维护一个变量 `size` 用于记录队列长度。定义 `rear = front + size` ,这个公式计算出的 `rear` 指向队尾元素之后的下一个位置。
|
2023-02-16 03:03:02 +08:00
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
基于此设计,**数组中包含元素的有效区间为 `[front, rear - 1]`**,各种操作的实现方法如下图所示。
|
2023-02-16 03:03:02 +08:00
|
|
|
|
|
2023-08-31 02:32:20 +08:00
|
|
|
|
- 入队操作:将输入元素赋值给 `rear` 索引处,并将 `size` 增加 1 。
|
|
|
|
|
- 出队操作:只需将 `front` 增加 1 ,并将 `size` 减少 1 。
|
2023-02-16 03:03:02 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
可以看到,入队和出队操作都只需进行一次操作,时间复杂度均为 $O(1)$ 。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-19 02:04:21 +08:00
|
|
|
|
=== "ArrayQueue"
|
2024-02-14 18:37:18 +08:00
|
|
|
|
![基于数组实现队列的入队出队操作](queue.assets/array_queue_step1.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
|
|
|
|
=== "push()"
|
2024-02-14 18:37:18 +08:00
|
|
|
|
![array_queue_push](queue.assets/array_queue_step2_push.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "pop()"
|
2024-02-14 18:37:18 +08:00
|
|
|
|
![array_queue_pop](queue.assets/array_queue_step3_pop.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
你可能会发现一个问题:在不断进行入队和出队的过程中,`front` 和 `rear` 都在向右移动,**当它们到达数组尾部时就无法继续移动了**。为了解决此问题,我们可以将数组视为首尾相接的“环形数组”。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
对于环形数组,我们需要让 `front` 或 `rear` 在越过数组尾部时,直接回到数组头部继续遍历。这种周期性规律可以通过“取余操作”来实现,代码如下所示:
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-10-17 01:06:00 +08:00
|
|
|
|
```src
|
|
|
|
|
[file]{array_queue}-[class]{array_queue}-[func]{}
|
|
|
|
|
```
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
以上实现的队列仍然具有局限性:其长度不可变。然而,这个问题不难解决,我们可以将数组替换为动态数组,从而引入扩容机制。有兴趣的读者可以尝试自行实现。
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
两种实现的对比结论与栈一致,在此不再赘述。
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 队列典型应用
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
- **淘宝订单**。购物者下单后,订单将加入队列中,系统随后会根据顺序处理队列中的订单。在双十一期间,短时间内会产生海量订单,高并发成为工程师们需要重点攻克的问题。
|
|
|
|
|
- **各类待办事项**。任何需要实现“先来后到”功能的场景,例如打印机的任务队列、餐厅的出餐队列等,队列在这些场景中可以有效地维护处理顺序。
|