feat: Deque implemented using linked list (#307)

* deque: code

* deque: md

* deque: 修改代码注释

* deque: 修改md文档中代码示例

* deque: 优化注释

* deque: 优化md代码注释

* deque: 添加草图

* deque: rename file

* deque: 优化代码注释

* deque: 更改md中代码示例

* deque: 优化代码,链表的入队和出队依赖size来判断

* deque: 更新md中代码示例

* deque: 更改readme代码示例,并移除了没有使用的代码

* Update deque.md

* Update the figures and the content.

* Add code blocks.

---------

Co-authored-by: FangYuan <374072213@qq.com>
pull/310/head
Yudong Jin 2 years ago committed by GitHub
parent e308657ae9
commit 29b096d90d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

@ -222,3 +222,195 @@ comments: true
/* 判断双向队列是否为空 */
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"
```

Loading…
Cancel
Save