2023-02-28 20:03:53 +08:00
|
|
|
|
# 双向队列
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
对于队列,我们只能在头部删除或在尾部添加元素,而「双向队列 Deque」更加灵活,在其头部和尾部都能执行元素添加或删除操作。
|
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![双向队列的操作](deque.assets/deque_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-03-13 21:58:21 +08:00
|
|
|
|
双向队列的常用操作见下表,方法名需根据语言来确定。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
| 方法名 | 描述 | 时间复杂度 |
|
|
|
|
|
| ----------- | -------------- | ---------- |
|
|
|
|
|
| pushFirst() | 将元素添加至队首 | $O(1)$ |
|
|
|
|
|
| pushLast() | 将元素添加至队尾 | $O(1)$ |
|
|
|
|
|
| popFirst() | 删除队首元素 | $O(1)$ |
|
|
|
|
|
| popLast() | 删除队尾元素 | $O(1)$ |
|
|
|
|
|
| peekFirst() | 访问队首元素 | $O(1)$ |
|
|
|
|
|
| peekLast() | 访问队尾元素 | $O(1)$ |
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
相同地,我们可以直接使用编程语言实现好的双向队列类。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="deque.java"
|
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
Deque<Integer> deque = new LinkedList<>();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 元素入队 */
|
2022-11-30 03:46:53 +08:00
|
|
|
|
deque.offerLast(2); // 添加至队尾
|
2022-11-22 17:47:26 +08:00
|
|
|
|
deque.offerLast(5);
|
|
|
|
|
deque.offerLast(4);
|
2022-11-30 03:46:53 +08:00
|
|
|
|
deque.offerFirst(3); // 添加至队首
|
2022-11-22 17:47:26 +08:00
|
|
|
|
deque.offerFirst(1);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 访问元素 */
|
2022-11-30 03:46:53 +08:00
|
|
|
|
int peekFirst = deque.peekFirst(); // 队首元素
|
|
|
|
|
int peekLast = deque.peekLast(); // 队尾元素
|
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 popFirst = deque.pollFirst(); // 队首元素出队
|
|
|
|
|
int popLast = deque.pollLast(); // 队尾元素出队
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 获取双向队列的长度 */
|
2022-11-22 17:47:26 +08:00
|
|
|
|
int size = deque.size();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 判断双向队列是否为空 */
|
2022-11-22 17:47:26 +08:00
|
|
|
|
boolean isEmpty = deque.isEmpty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="deque.cpp"
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
deque<int> deque;
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.push_back(2); // 添加至队尾
|
|
|
|
|
deque.push_back(5);
|
|
|
|
|
deque.push_back(4);
|
|
|
|
|
deque.push_front(3); // 添加至队首
|
|
|
|
|
deque.push_front(1);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 访问元素 */
|
2022-11-30 03:46:53 +08:00
|
|
|
|
int front = deque.front(); // 队首元素
|
|
|
|
|
int back = deque.back(); // 队尾元素
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
deque.pop_front(); // 队首元素出队
|
|
|
|
|
deque.pop_back(); // 队尾元素出队
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
int size = deque.size();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
bool empty = deque.empty();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="deque.py"
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 初始化双向队列
|
2023-03-12 18:49:52 +08:00
|
|
|
|
deque: Deque[int] = collections.deque()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
# 元素入队
|
2023-03-12 02:45:10 +08:00
|
|
|
|
deque.append(2) # 添加至队尾
|
|
|
|
|
deque.append(5)
|
|
|
|
|
deque.append(4)
|
|
|
|
|
deque.appendleft(3) # 添加至队首
|
|
|
|
|
deque.appendleft(1)
|
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 = deque[0] # 队首元素
|
|
|
|
|
rear: int = deque[-1] # 队尾元素
|
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_front: int = deque.popleft() # 队首元素出队
|
|
|
|
|
pop_rear: int = deque.pop() # 队尾元素出队
|
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(deque)
|
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(deque) == 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="deque_test.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
// 在 Go 中,将 list 作为双向队列使用
|
|
|
|
|
deque := list.New()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.PushBack(2) // 添加至队尾
|
|
|
|
|
deque.PushBack(5)
|
|
|
|
|
deque.PushBack(4)
|
|
|
|
|
deque.PushFront(3) // 添加至队首
|
|
|
|
|
deque.PushFront(1)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 访问元素 */
|
|
|
|
|
front := deque.Front() // 队首元素
|
|
|
|
|
rear := deque.Back() // 队尾元素
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
deque.Remove(front) // 队首元素出队
|
|
|
|
|
deque.Remove(rear) // 队尾元素出队
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
size := deque.Len()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
isEmpty := deque.Len() == 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="deque.js"
|
2023-02-06 01:31:09 +08:00
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
// JavaScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
|
|
|
|
|
const deque = [];
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.push(2);
|
|
|
|
|
deque.push(5);
|
|
|
|
|
deque.push(4);
|
|
|
|
|
// 请注意,由于是数组,unshift() 方法的时间复杂度为 O(n)
|
|
|
|
|
deque.unshift(3);
|
|
|
|
|
deque.unshift(1);
|
|
|
|
|
console.log("双向队列 deque = ", deque);
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
const peekFirst = deque[0];
|
|
|
|
|
console.log("队首元素 peekFirst = " + peekFirst);
|
|
|
|
|
const peekLast = deque[deque.length - 1];
|
|
|
|
|
console.log("队尾元素 peekLast = " + peekLast);
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
|
// 请注意,由于是数组,shift() 方法的时间复杂度为 O(n)
|
|
|
|
|
const popFront = deque.shift();
|
|
|
|
|
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
|
|
|
|
const popBack = deque.pop();
|
|
|
|
|
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = " + deque);
|
|
|
|
|
|
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
const size = deque.length;
|
|
|
|
|
console.log("双向队列长度 size = " + size);
|
|
|
|
|
|
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
const isEmpty = size === 0;
|
|
|
|
|
console.log("双向队列是否为空 = " + isEmpty);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="deque.ts"
|
2023-02-06 01:31:09 +08:00
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
// TypeScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
|
|
|
|
|
const deque: number[] = [];
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.push(2);
|
|
|
|
|
deque.push(5);
|
|
|
|
|
deque.push(4);
|
|
|
|
|
// 请注意,由于是数组,unshift() 方法的时间复杂度为 O(n)
|
|
|
|
|
deque.unshift(3);
|
|
|
|
|
deque.unshift(1);
|
|
|
|
|
console.log("双向队列 deque = ", deque);
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
const peekFirst: number = deque[0];
|
|
|
|
|
console.log("队首元素 peekFirst = " + peekFirst);
|
|
|
|
|
const peekLast: number = deque[deque.length - 1];
|
|
|
|
|
console.log("队尾元素 peekLast = " + peekLast);
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
|
// 请注意,由于是数组,shift() 方法的时间复杂度为 O(n)
|
|
|
|
|
const popFront: number = deque.shift() as number;
|
|
|
|
|
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
|
|
|
|
const popBack: number = deque.pop() as number;
|
|
|
|
|
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = " + deque);
|
|
|
|
|
|
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
const size: number = deque.length;
|
|
|
|
|
console.log("双向队列长度 size = " + size);
|
|
|
|
|
|
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
const isEmpty: boolean = size === 0;
|
|
|
|
|
console.log("双向队列是否为空 = " + isEmpty);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="deque.c"
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="deque.cs"
|
2022-12-30 14:28:39 +08:00
|
|
|
|
/* 初始化双向队列 */
|
2022-12-30 16:11:47 +08:00
|
|
|
|
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
|
2022-12-30 14:28:39 +08:00
|
|
|
|
LinkedList<int> deque = new LinkedList<int>();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-30 14:28:39 +08:00
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.AddLast(2); // 添加至队尾
|
|
|
|
|
deque.AddLast(5);
|
|
|
|
|
deque.AddLast(4);
|
|
|
|
|
deque.AddFirst(3); // 添加至队首
|
|
|
|
|
deque.AddFirst(1);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-30 14:28:39 +08:00
|
|
|
|
/* 访问元素 */
|
|
|
|
|
int peekFirst = deque.First.Value; // 队首元素
|
|
|
|
|
int peekLast = deque.Last.Value; // 队尾元素
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-30 14:28:39 +08:00
|
|
|
|
/* 元素出队 */
|
|
|
|
|
deque.RemoveFirst(); // 队首元素出队
|
|
|
|
|
deque.RemoveLast(); // 队尾元素出队
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-30 14:28:39 +08:00
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
int size = deque.Count;
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-30 14:28:39 +08:00
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
bool isEmpty = deque.Count == 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="deque.swift"
|
2023-01-14 01:06:00 +08:00
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
// Swift 没有内置的双向队列类,可以把 Array 当作双向队列来使用
|
|
|
|
|
var deque: [Int] = []
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.append(2) // 添加至队尾
|
|
|
|
|
deque.append(5)
|
|
|
|
|
deque.append(4)
|
|
|
|
|
deque.insert(3, at: 0) // 添加至队首
|
|
|
|
|
deque.insert(1, at: 0)
|
|
|
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
|
let peekFirst = deque.first! // 队首元素
|
|
|
|
|
let peekLast = deque.last! // 队尾元素
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
|
// 使用 Array 模拟时 popFirst 的复杂度为 O(n)
|
|
|
|
|
let popFirst = deque.removeFirst() // 队首元素出队
|
|
|
|
|
let popLast = deque.removeLast() // 队尾元素出队
|
2023-01-14 01:06:00 +08:00
|
|
|
|
|
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
let size = deque.count
|
|
|
|
|
|
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
let isEmpty = deque.isEmpty
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="deque.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 双向队列实现 *
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-02-16 02:17:15 +08:00
|
|
|
|
与队列类似,双向队列同样可以使用链表或数组来实现。
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
|
|
|
|
### 基于双向链表的实现
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
回忆上节内容,由于可以方便地删除链表头节点(对应出队操作),以及在链表尾节点后添加新节点(对应入队操作),因此我们使用普通单向链表来实现队列。
|
2023-02-16 02:17:15 +08:00
|
|
|
|
|
|
|
|
|
而双向队列的头部和尾部都可以执行入队与出队操作,换言之,双向队列的操作是“首尾对称”的,也需要实现另一个对称方向的操作。因此,双向队列需要使用「双向链表」来实现。
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
我们将双向链表的头节点和尾节点分别看作双向队列的队首和队尾,并且实现在两端都能添加与删除节点。
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
|
|
|
|
=== "LinkedListDeque"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![基于链表实现双向队列的入队出队操作](deque.assets/linkedlist_deque.png)
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
|
|
|
|
=== "pushLast()"
|
|
|
|
|
![linkedlist_deque_push_last](deque.assets/linkedlist_deque_push_last.png)
|
|
|
|
|
|
|
|
|
|
=== "pushFirst()"
|
|
|
|
|
![linkedlist_deque_push_first](deque.assets/linkedlist_deque_push_first.png)
|
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "popLast()"
|
|
|
|
|
![linkedlist_deque_pop_last](deque.assets/linkedlist_deque_pop_last.png)
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "popFirst()"
|
|
|
|
|
![linkedlist_deque_pop_first](deque.assets/linkedlist_deque_pop_first.png)
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-02-16 02:17:15 +08:00
|
|
|
|
以下是具体实现代码。
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linkedlist_deque.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linkedlist_deque.cpp"
|
2023-03-02 18:57:33 +08:00
|
|
|
|
[class]{DoublyListNode}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linkedlist_deque.py"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linkedlist_deque.go"
|
2023-03-02 02:58:34 +08:00
|
|
|
|
[class]{linkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="linkedlist_deque.js"
|
2023-02-09 03:20:27 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linkedlist_deque.ts"
|
2023-02-09 03:20:27 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
|
|
|
|
|
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linkedlist_deque.c"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linkedlist_deque.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linkedlist_deque.swift"
|
2023-02-23 23:03:33 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
|
2023-02-23 23:03:33 +08:00
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-01-29 13:42:42 +08:00
|
|
|
|
```
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linkedlist_deque.zig"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ListNode}-[func]{}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{LinkedListDeque}-[func]{}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
2023-02-16 02:17:15 +08:00
|
|
|
|
|
|
|
|
|
### 基于数组的实现
|
|
|
|
|
|
|
|
|
|
与基于数组实现队列类似,我们也可以使用环形数组来实现双向队列。在实现队列的基础上,增加实现“队首入队”和“队尾出队”方法即可。
|
|
|
|
|
|
|
|
|
|
=== "ArrayDeque"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![基于数组实现双向队列的入队出队操作](deque.assets/array_deque.png)
|
2023-02-16 02:17:15 +08:00
|
|
|
|
|
|
|
|
|
=== "pushLast()"
|
|
|
|
|
![array_deque_push_last](deque.assets/array_deque_push_last.png)
|
|
|
|
|
|
|
|
|
|
=== "pushFirst()"
|
|
|
|
|
![array_deque_push_first](deque.assets/array_deque_push_first.png)
|
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "popLast()"
|
|
|
|
|
![array_deque_pop_last](deque.assets/array_deque_pop_last.png)
|
2023-02-16 02:17:15 +08:00
|
|
|
|
|
2023-03-13 21:58:21 +08:00
|
|
|
|
=== "popFirst()"
|
|
|
|
|
![array_deque_pop_first](deque.assets/array_deque_pop_first.png)
|
2023-02-16 02:17:15 +08:00
|
|
|
|
|
|
|
|
|
以下是具体实现代码。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array_deque.java"
|
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array_deque.cpp"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array_deque.py"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array_deque.go"
|
2023-03-16 00:05:46 +08:00
|
|
|
|
[class]{arrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-03-03 21:54:04 +08:00
|
|
|
|
```javascript title="array_deque.js"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array_deque.ts"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array_deque.c"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array_deque.cs"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array_deque.swift"
|
2023-02-23 23:03:33 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="array_deque.zig"
|
2023-03-01 03:51:30 +08:00
|
|
|
|
[class]{ArrayDeque}-[func]{}
|
2023-02-16 02:17:15 +08:00
|
|
|
|
```
|
2023-03-12 02:45:10 +08:00
|
|
|
|
|
|
|
|
|
## 双向队列应用
|
|
|
|
|
|
|
|
|
|
双向队列同时表现出栈与队列的逻辑,**因此可以实现两者的所有应用,并且提供更高的自由度**。
|
|
|
|
|
|
|
|
|
|
我们知道,软件的“撤销”功能需要使用栈来实现;系统把每一次更改操作 `push` 到栈中,然后通过 `pop` 实现撤销。然而,考虑到系统资源有限,软件一般会限制撤销的步数(例如仅允许保存 $50$ 步),那么当栈的长度 $> 50$ 时,软件就需要在栈底(即队首)执行删除,**但栈无法实现,此时就需要使用双向队列来替代栈**。注意,“撤销”的核心逻辑仍然是栈的先入后出,只是双向队列可以更加灵活地实现。
|