2022-11-29 03:19:08 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
# 双向队列
|
|
|
|
|
|
|
|
|
|
对于队列,我们只能在头部删除或在尾部添加元素,而「双向队列 Deque」更加灵活,在其头部和尾部都能执行元素添加或删除操作。
|
|
|
|
|
|
|
|
|
|
![deque_operations](deque.assets/deque_operations.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 双向队列的操作 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
## 双向队列常用操作
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
双向队列的常用操作见下表(方法命名以 Java 为例)。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Table. 双向队列的常用操作 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2023-01-09 02:17:40 +08:00
|
|
|
|
| 方法 | 描述 | 时间复杂度 |
|
|
|
|
|
| ------------ | ---------------- | ---------- |
|
|
|
|
|
| offerFirst() | 将元素添加至队首 | $O(1)$ |
|
|
|
|
|
| offerLast() | 将元素添加至队尾 | $O(1)$ |
|
|
|
|
|
| pollFirst() | 删除队首元素 | $O(1)$ |
|
|
|
|
|
| pollLast() | 删除队尾元素 | $O(1)$ |
|
|
|
|
|
| peekFirst() | 访问队首元素 | $O(1)$ |
|
|
|
|
|
| peekLast() | 访问队尾元素 | $O(1)$ |
|
|
|
|
|
| size() | 获取队列的长度 | $O(1)$ |
|
|
|
|
|
| isEmpty() | 判断队列是否为空 | $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
|
|
|
|
/* 元素出队 */
|
2022-11-30 03:46:53 +08:00
|
|
|
|
int pollFirst = deque.pollFirst(); // 队首元素出队
|
|
|
|
|
int pollLast = 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"
|
2022-12-02 00:09:34 +08:00
|
|
|
|
""" 初始化双向队列 """
|
|
|
|
|
duque = deque()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-02 00:09:34 +08:00
|
|
|
|
""" 元素入队 """
|
|
|
|
|
duque.append(2) # 添加至队尾
|
|
|
|
|
duque.append(5)
|
|
|
|
|
duque.append(4)
|
|
|
|
|
duque.appendleft(3) # 添加至队首
|
|
|
|
|
duque.appendleft(1)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
""" 访问元素 """
|
2022-12-02 00:09:34 +08:00
|
|
|
|
front = duque[0] # 队首元素
|
|
|
|
|
rear = duque[-1] # 队尾元素
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-02 00:09:34 +08:00
|
|
|
|
""" 元素出队 """
|
|
|
|
|
pop_front = duque.popleft() # 队首元素出队
|
|
|
|
|
pop_rear = duque.pop() # 队尾元素出队
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-02 00:09:34 +08:00
|
|
|
|
""" 获取双向队列的长度 """
|
|
|
|
|
size = len(duque)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-02 00:09:34 +08:00
|
|
|
|
""" 判断双向队列是否为空 """
|
|
|
|
|
is_empty = len(duque) == 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"
|
|
|
|
|
|
|
|
|
|
```js title="deque.js"
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="deque.ts"
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
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-09 02:17:40 +08:00
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|