feat: Revised the book (#978)

* Sync recent changes to the revised Word.

* Revised the preface chapter

* Revised the introduction chapter

* Revised the computation complexity chapter

* Revised the chapter data structure

* Revised the chapter array and linked list

* Revised the chapter stack and queue

* Revised the chapter hashing

* Revised the chapter tree

* Revised the chapter heap

* Revised the chapter graph

* Revised the chapter searching

* Reivised the sorting chapter

* Revised the divide and conquer chapter

* Revised the chapter backtacking

* Revised the DP chapter

* Revised the greedy chapter

* Revised the appendix chapter

* Revised the preface chapter doubly

* Revised the figures
This commit is contained in:
Yudong Jin
2023-12-02 06:21:34 +08:00
committed by GitHub
parent b824d149cb
commit e720aa2d24
404 changed files with 1537 additions and 1558 deletions

View File

@@ -1,6 +1,6 @@
# 双向队列
在队列中,我们仅能在头部删除或在尾部添加元素。如下图所示,「双向队列 double-ended queue」提供了更高的灵活性允许在头部和尾部执行元素的添加或删除操作。
在队列中,我们仅能删除头部元素或在尾部添加元素。如下图所示,「双向队列 double-ended queue」提供了更高的灵活性允许在头部和尾部执行元素的添加或删除操作。
![双向队列的操作](deque.assets/deque_operations.png)
@@ -19,13 +19,15 @@
| peekFirst() | 访问队首元素 | $O(1)$ |
| peekLast() | 访问队尾元素 | $O(1)$ |
同样地,我们可以直接使用编程语言中已实现的双向队列类
同样地,我们可以直接使用编程语言中已实现的双向队列类
=== "Python"
```python title="deque.py"
from collections import deque
# 初始化双向队列
deque: deque[int] = collections.deque()
deque: deque[int] = deque()
# 元素入队
deque.append(2) # 添加至队尾
@@ -369,7 +371,7 @@
=== "popFirst()"
![linkedlist_deque_pop_first](deque.assets/linkedlist_deque_pop_first.png)
实现代码如下所示
实现代码如下所示
```src
[file]{linkedlist_deque}-[class]{linked_list_deque}-[func]{}
@@ -394,7 +396,7 @@
=== "popFirst()"
![array_deque_pop_first](deque.assets/array_deque_pop_first.png)
在队列的实现基础上,仅需增加“队首入队”和“队尾出队”的方法
在队列的实现基础上,仅需增加“队首入队”和“队尾出队”的方法
```src
[file]{array_deque}-[class]{array_deque}-[func]{}
@@ -404,4 +406,4 @@
双向队列兼具栈与队列的逻辑,**因此它可以实现这两者的所有应用场景,同时提供更高的自由度**。
我们知道,软件的“撤销”功能通常使用栈来实现:系统将每次更改操作 `push` 到栈中,然后通过 `pop` 实现撤销。然而,考虑到系统资源的限制,软件通常会限制撤销的步数(例如仅允许保存 $50$ 步)。当栈的长度超过 $50$ 时,软件需要在栈底(队首)执行删除操作。**但栈无法实现该功能,此时就需要使用双向队列来替代栈**。请注意,“撤销”的核心逻辑仍然遵循栈的先入后出原则,只是双向队列能够更加灵活地实现一些额外逻辑。
我们知道,软件的“撤销”功能通常使用栈来实现:系统将每次更改操作 `push` 到栈中,然后通过 `pop` 实现撤销。然而,考虑到系统资源的限制,软件通常会限制撤销的步数(例如仅允许保存 $50$ 步)。当栈的长度超过 $50$ 时,软件需要在栈底(队首)执行删除操作。**但栈无法实现该功能,此时就需要使用双向队列来替代栈**。请注意,“撤销”的核心逻辑仍然遵循栈的先入后出原则,只是双向队列能够更加灵活地实现一些额外逻辑。