|
|
@ -222,3 +222,195 @@ comments: true
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
let isEmpty = deque.isEmpty
|
|
|
|
let isEmpty = deque.isEmpty
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 双向队列实现
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
双向队列需要一种可以在两端添加、两端删除的数据结构。与队列的实现方法类似,双向队列也可以使用双向链表和循环数组来实现。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 基于双向链表的实现
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
我们将双向链表的头结点和尾结点分别看作双向队列的队首和队尾,并且实现在两端都能添加与删除结点。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "LinkedListDeque"
|
|
|
|
|
|
|
|
![linkedlist_deque](deque.assets/linkedlist_deque.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "pushLast()"
|
|
|
|
|
|
|
|
![linkedlist_deque_push_last](deque.assets/linkedlist_deque_push_last.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "pushFirst()"
|
|
|
|
|
|
|
|
![linkedlist_deque_push_first](deque.assets/linkedlist_deque_push_first.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "pollLast()"
|
|
|
|
|
|
|
|
![linkedlist_deque_poll_last](deque.assets/linkedlist_deque_poll_last.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "pollFirst()"
|
|
|
|
|
|
|
|
![linkedlist_deque_poll_first](deque.assets/linkedlist_deque_poll_first.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
以下是使用双向链表实现双向队列的示例代码。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java title="linkedlist_deque.java"
|
|
|
|
|
|
|
|
/* 双向链表结点 */
|
|
|
|
|
|
|
|
class ListNode {
|
|
|
|
|
|
|
|
int val; // 结点值
|
|
|
|
|
|
|
|
ListNode next; // 后继结点引用(指针)
|
|
|
|
|
|
|
|
ListNode prev; // 前驱结点引用(指针)
|
|
|
|
|
|
|
|
ListNode(int val) {
|
|
|
|
|
|
|
|
this.val = val;
|
|
|
|
|
|
|
|
prev = next = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 基于双向链表实现的双向队列 */
|
|
|
|
|
|
|
|
class LinkedListDeque {
|
|
|
|
|
|
|
|
private ListNode front, rear; // 头结点 front ,尾结点 rear
|
|
|
|
|
|
|
|
private int size = 0; // 双向队列的长度
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public LinkedListDeque() {
|
|
|
|
|
|
|
|
front = rear = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取双向队列的长度 */
|
|
|
|
|
|
|
|
public int size() {
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断双向队列是否为空 */
|
|
|
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
|
|
|
return size() == 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队操作 */
|
|
|
|
|
|
|
|
private void offer(int num, boolean isFront) {
|
|
|
|
|
|
|
|
ListNode node = new ListNode(num);
|
|
|
|
|
|
|
|
// 若链表为空,则令 front, rear 都指向 node
|
|
|
|
|
|
|
|
if (isEmpty())
|
|
|
|
|
|
|
|
front = rear = node;
|
|
|
|
|
|
|
|
// 队首入队操作
|
|
|
|
|
|
|
|
else if (isFront) {
|
|
|
|
|
|
|
|
// 将 node 添加至链表头部
|
|
|
|
|
|
|
|
front.prev = node;
|
|
|
|
|
|
|
|
node.next = front;
|
|
|
|
|
|
|
|
front = node; // 更新头结点
|
|
|
|
|
|
|
|
// 队尾入队操作
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 将 node 添加至链表尾部
|
|
|
|
|
|
|
|
rear.next = node;
|
|
|
|
|
|
|
|
node.prev = rear;
|
|
|
|
|
|
|
|
rear = node; // 更新尾结点
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
size++; // 更新队列长度
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 队首入队 */
|
|
|
|
|
|
|
|
public void offerFirst(int num) {
|
|
|
|
|
|
|
|
offer(num, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 队尾入队 */
|
|
|
|
|
|
|
|
public void offerLast(int num) {
|
|
|
|
|
|
|
|
offer(num, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队操作 */
|
|
|
|
|
|
|
|
private Integer poll(boolean isFront) {
|
|
|
|
|
|
|
|
// 若队列为空,直接返回 null
|
|
|
|
|
|
|
|
if (isEmpty())
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
int val;
|
|
|
|
|
|
|
|
// 队首出队操作
|
|
|
|
|
|
|
|
if (isFront) {
|
|
|
|
|
|
|
|
val = front.val; // 暂存头结点值
|
|
|
|
|
|
|
|
// 删除头结点
|
|
|
|
|
|
|
|
ListNode fNext = front.next;
|
|
|
|
|
|
|
|
if (fNext != null) {
|
|
|
|
|
|
|
|
fNext.prev = null;
|
|
|
|
|
|
|
|
front.next = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
front = fNext; // 更新头结点
|
|
|
|
|
|
|
|
// 队尾出队操作
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
val = rear.val; // 暂存尾结点值
|
|
|
|
|
|
|
|
// 删除尾结点
|
|
|
|
|
|
|
|
ListNode rPrev = rear.prev;
|
|
|
|
|
|
|
|
if (rPrev != null) {
|
|
|
|
|
|
|
|
rPrev.next = null;
|
|
|
|
|
|
|
|
rear.prev = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rear = rPrev; // 更新尾结点
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
size--; // 更新队列长度
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 队首出队 */
|
|
|
|
|
|
|
|
public Integer pollFirst() {
|
|
|
|
|
|
|
|
return poll(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 队尾出队 */
|
|
|
|
|
|
|
|
public Integer pollLast() {
|
|
|
|
|
|
|
|
return poll(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
|
|
|
|
public Integer peekFirst() {
|
|
|
|
|
|
|
|
return isEmpty() ? null : front.val;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队尾元素 */
|
|
|
|
|
|
|
|
public Integer peekLast() {
|
|
|
|
|
|
|
|
return isEmpty() ? null : rear.val;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="linkedlist_deque.cpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python title="linkedlist_deque.py"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```go title="linkedlist_deque.go"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```js title="linkedlist_deque.js"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="linkedlist_deque.ts"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```c title="linkedlist_deque.c"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```csharp title="linkedlist_deque.cs"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="linkedlist_deque.swift"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|