|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# 双向队列
|
|
|
|
|
|
|
|
|
|
对于队列,我们只能在头部删除或在尾部添加元素,而「双向队列 Deque」更加灵活,在其头部和尾部都能执行元素添加或删除操作。
|
|
|
|
|
|
|
|
|
|
![deque_operations](deque.assets/deque_operations.png)
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 双向队列的操作 </p>
|
|
|
|
|
|
|
|
|
|
## 双向队列常用操作
|
|
|
|
|
|
|
|
|
|
双向队列的常用操作见下表,方法名需根据编程语言设定来具体确定。
|
|
|
|
|
|
|
|
|
|
<p align="center"> Table. 双向队列的常用操作 </p>
|
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
| ------------ | ---------------- |
|
|
|
|
|
| offerFirst() | 将元素添加至队首 |
|
|
|
|
|
| offerLast() | 将元素添加至队尾 |
|
|
|
|
|
| pollFirst() | 删除队首元素 |
|
|
|
|
|
| pollLast() | 删除队尾元素 |
|
|
|
|
|
| peekFirst() | 访问队首元素 |
|
|
|
|
|
| peekLast() | 访问队尾元素 |
|
|
|
|
|
| size() | 获取队列的长度 |
|
|
|
|
|
| isEmpty() | 判断队列是否为空 |
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
相同地,我们可以直接使用编程语言实现好的双向队列类。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="deque.java"
|
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
Deque<Integer> deque = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.offerLast(2); // 添加至队尾
|
|
|
|
|
deque.offerLast(5);
|
|
|
|
|
deque.offerLast(4);
|
|
|
|
|
deque.offerFirst(3); // 添加至队首
|
|
|
|
|
deque.offerFirst(1);
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int peekFirst = deque.peekFirst(); // 队首元素
|
|
|
|
|
int peekLast = deque.peekLast(); // 队尾元素
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
|
int pollFirst = deque.pollFirst(); // 队首元素出队
|
|
|
|
|
int pollLast = deque.pollLast(); // 队尾元素出队
|
|
|
|
|
|
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
int size = deque.size();
|
|
|
|
|
|
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
boolean isEmpty = deque.isEmpty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="deque.cpp"
|
|
|
|
|
/* 初始化双向队列 */
|
|
|
|
|
deque<int> deque;
|
|
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
|
deque.push_back(2); // 添加至队尾
|
|
|
|
|
deque.push_back(5);
|
|
|
|
|
deque.push_back(4);
|
|
|
|
|
deque.push_front(3); // 添加至队首
|
|
|
|
|
deque.push_front(1);
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
int front = deque.front(); // 队首元素
|
|
|
|
|
int back = deque.back(); // 队尾元素
|
|
|
|
|
|
|
|
|
|
/* 元素出队 */
|
|
|
|
|
deque.pop_front(); // 队首元素出队
|
|
|
|
|
deque.pop_back(); // 队尾元素出队
|
|
|
|
|
|
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
int size = deque.size();
|
|
|
|
|
|
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
bool empty = deque.empty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="deque.py"
|
|
|
|
|
""" 初始化双向队列 """
|
|
|
|
|
duque = deque()
|
|
|
|
|
|
|
|
|
|
""" 元素入队 """
|
|
|
|
|
duque.append(2) # 添加至队尾
|
|
|
|
|
duque.append(5)
|
|
|
|
|
duque.append(4)
|
|
|
|
|
duque.appendleft(3) # 添加至队首
|
|
|
|
|
duque.appendleft(1)
|
|
|
|
|
|
|
|
|
|
""" 访问队首元素 """
|
|
|
|
|
front = duque[0] # 队首元素
|
|
|
|
|
rear = duque[-1] # 队尾元素
|
|
|
|
|
|
|
|
|
|
""" 元素出队 """
|
|
|
|
|
pop_front = duque.popleft() # 队首元素出队
|
|
|
|
|
pop_rear = duque.pop() # 队尾元素出队
|
|
|
|
|
|
|
|
|
|
""" 获取双向队列的长度 """
|
|
|
|
|
size = len(duque)
|
|
|
|
|
|
|
|
|
|
""" 判断双向队列是否为空 """
|
|
|
|
|
is_empty = len(duque) == 0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="deque.go"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="deque.js"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="deque.ts"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="deque.c"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="deque.cs"
|
|
|
|
|
|
|
|
|
|
```
|