2023-02-28 20:03:53 +08:00
|
|
|
|
# 队列
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-08-20 23:28:30 +08:00
|
|
|
|
「队列 queue」是一种遵循先入先出规则的线性数据结构。顾名思义,队列模拟了排队现象,即新来的人不断加入队列的尾部,而位于队列头部的人逐个离开。
|
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-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-19 19:22:08 +08:00
|
|
|
|
<p align="center"> 表:队列操作效率 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-02 01:43:01 +08:00
|
|
|
|
| 方法名 | 描述 | 时间复杂度 |
|
|
|
|
|
| --------- | -------------------------- | -------- |
|
|
|
|
|
| push() | 元素入队,即将元素添加至队尾 | $O(1)$ |
|
2023-03-13 21:58:21 +08:00
|
|
|
|
| pop() | 队首元素出队 | $O(1)$ |
|
|
|
|
|
| peek() | 访问队首元素 | $O(1)$ |
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
我们可以直接使用编程语言中现成的队列类。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="queue.java"
|
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
Queue<Integer> queue = new LinkedList<>();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.offer(1);
|
|
|
|
|
queue.offer(3);
|
|
|
|
|
queue.offer(2);
|
|
|
|
|
queue.offer(5);
|
|
|
|
|
queue.offer(4);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int peek = queue.peek();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
|
int pop = queue.poll();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
int size = queue.size();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
boolean isEmpty = queue.isEmpty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="queue.cpp"
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
queue<int> queue;
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.push(1);
|
|
|
|
|
queue.push(3);
|
|
|
|
|
queue.push(2);
|
|
|
|
|
queue.push(5);
|
|
|
|
|
queue.push(4);
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int front = queue.front();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
queue.pop();
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
int size = queue.size();
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
bool empty = queue.empty();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="queue.py"
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 初始化队列
|
2022-12-19 21:38:31 +08:00
|
|
|
|
# 在 Python 中,我们一般将双向队列类 deque 看作队列使用
|
2022-12-02 00:09:34 +08:00
|
|
|
|
# 虽然 queue.Queue() 是纯正的队列类,但不太好用,因此不建议
|
2023-03-12 18:49:52 +08:00
|
|
|
|
que: Deque[int] = collections.deque()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 元素入队
|
2022-12-02 00:09:34 +08:00
|
|
|
|
que.append(1)
|
|
|
|
|
que.append(3)
|
|
|
|
|
que.append(2)
|
|
|
|
|
que.append(5)
|
|
|
|
|
que.append(4)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 访问队首元素
|
2023-03-12 18:49:52 +08:00
|
|
|
|
front: int = que[0];
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 元素出队
|
2023-03-12 18:49:52 +08:00
|
|
|
|
pop: int = que.popleft()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 获取队列的长度
|
2023-03-12 18:49:52 +08:00
|
|
|
|
size: int = len(que)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 判断队列是否为空
|
2023-03-12 18:49:52 +08:00
|
|
|
|
is_empty: bool = len(que) == 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()
|
2023-01-07 20:34:32 +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)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
peek := queue.Front()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
|
pop := queue.Front()
|
|
|
|
|
queue.Remove(pop)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
size := queue.Len()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
isEmpty := queue.Len() == 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
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 = [];
|
2023-01-07 20:34:32 +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);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
const peek = queue[0];
|
2023-01-07 20:34:32 +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();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
const size = queue.length;
|
2023-01-07 20:34:32 +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
|
|
|
|
/* 初始化队列 */
|
2022-12-05 22:56:38 +08:00
|
|
|
|
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
2022-12-05 22:06:31 +08:00
|
|
|
|
const queue: number[] = [];
|
2023-01-07 20:34:32 +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);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
const peek = queue[0];
|
2023-01-07 20:34:32 +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();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
const size = queue.length;
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-05 22:06:31 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
const empty = queue.length === 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="queue.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
// C 未提供内置队列
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="queue.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
Queue<int> queue = new();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.Enqueue(1);
|
|
|
|
|
queue.Enqueue(3);
|
|
|
|
|
queue.Enqueue(2);
|
|
|
|
|
queue.Enqueue(5);
|
|
|
|
|
queue.Enqueue(4);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int peek = queue.Peek();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
|
int pop = queue.Dequeue();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 获取队列的长度 */
|
2023-07-24 19:50:00 +08:00
|
|
|
|
int size = queue.Count;
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
2023-07-24 19:50:00 +08:00
|
|
|
|
bool isEmpty = queue.Count == 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="queue.swift"
|
2023-01-11 23:48:10 +08:00
|
|
|
|
/* 初始化队列 */
|
|
|
|
|
// Swift 没有内置的队列类,可以把 Array 当作队列来使用
|
|
|
|
|
var queue: [Int] = []
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-01-11 23:48:10 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
queue.append(1)
|
|
|
|
|
queue.append(3)
|
|
|
|
|
queue.append(2)
|
|
|
|
|
queue.append(5)
|
|
|
|
|
queue.append(4)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-01-11 23:48:10 +08:00
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
let peek = queue.first!
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-01-11 23:48:10 +08:00
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
|
// 由于是数组,因此 removeFirst 的复杂度为 O(n)
|
2023-01-11 23:48:10 +08:00
|
|
|
|
let pool = queue.removeFirst()
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-01-11 23:48:10 +08:00
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
|
let size = queue.count
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-01-11 23:48:10 +08:00
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
|
let isEmpty = queue.isEmpty
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="queue.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
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-02-16 03:39:01 +08:00
|
|
|
|
## 队列实现
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +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"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![基于链表实现队列的入队出队操作](queue.assets/linkedlist_queue.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
|
|
|
|
=== "push()"
|
|
|
|
|
![linkedlist_queue_push](queue.assets/linkedlist_queue_push.png)
|
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "pop()"
|
|
|
|
|
![linkedlist_queue_pop](queue.assets/linkedlist_queue_pop.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2023-08-21 19:33:45 +08:00
|
|
|
|
以下是用链表实现队列的代码。
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linkedlist_queue.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linkedlist_queue.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linkedlist_queue.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linkedlist_queue.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{linkedListQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
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="linkedlist_queue.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
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="linkedlist_queue.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linkedlist_queue.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{linkedListQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linkedlist_queue.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linkedlist_queue.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linkedlist_queue.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="linkedlist_queue.dart"
|
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="linkedlist_queue.rs"
|
|
|
|
|
[class]{LinkedListQueue}-[func]{}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
### 基于数组的实现
|
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
由于数组删除首元素的时间复杂度为 $O(n)$ ,这会导致出队操作效率较低。然而,我们可以采用以下巧妙方法来避免这个问题。
|
2023-02-16 03:03:02 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
我们可以使用一个变量 `front` 指向队首元素的索引,并维护一个变量 `queSize` 用于记录队列长度。定义 `rear = front + queSize` ,这个公式计算出的 `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-21 19:33:45 +08:00
|
|
|
|
- 入队操作:将输入元素赋值给 `rear` 索引处,并将 `queSize` 增加 1 。
|
|
|
|
|
- 出队操作:只需将 `front` 增加 1 ,并将 `queSize` 减少 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"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![基于数组实现队列的入队出队操作](queue.assets/array_queue.png)
|
2023-01-19 02:04:21 +08:00
|
|
|
|
|
|
|
|
|
=== "push()"
|
|
|
|
|
![array_queue_push](queue.assets/array_queue_push.png)
|
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "pop()"
|
|
|
|
|
![array_queue_pop](queue.assets/array_queue_pop.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-08-20 23:28:30 +08:00
|
|
|
|
你可能会发现一个问题:在不断进行入队和出队的过程中,`front` 和 `rear` 都在向右移动,**当它们到达数组尾部时就无法继续移动了**。为解决此问题,我们可以将数组视为首尾相接的“环形数组”。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 23:59:22 +08:00
|
|
|
|
对于环形数组,我们需要让 `front` 或 `rear` 在越过数组尾部时,直接回到数组头部继续遍历。这种周期性规律可以通过“取余操作”来实现,代码如下所示。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array_queue.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array_queue.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array_queue.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array_queue.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{arrayQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
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="array_queue.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
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="array_queue.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array_queue.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{arrayQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array_queue.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array_queue.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="array_queue.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="array_queue.dart"
|
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="array_queue.rs"
|
|
|
|
|
[class]{ArrayQueue}-[func]{}
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-10 23:59:22 +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-04-10 23:59:22 +08:00
|
|
|
|
- **淘宝订单**。购物者下单后,订单将加入队列中,系统随后会根据顺序依次处理队列中的订单。在双十一期间,短时间内会产生海量订单,高并发成为工程师们需要重点攻克的问题。
|
|
|
|
|
- **各类待办事项**。任何需要实现“先来后到”功能的场景,例如打印机的任务队列、餐厅的出餐队列等。队列在这些场景中可以有效地维护处理顺序。
|