Update the book based on the revised second edition (#1014)

* Revised the book

* Update the book with the second revised edition

* Revise base on the manuscript of the first edition
This commit is contained in:
Yudong Jin
2023-12-28 18:06:09 +08:00
committed by GitHub
parent 19dde675df
commit f68bbb0d59
261 changed files with 643 additions and 647 deletions

View File

@@ -1,6 +1,6 @@
# 栈
「栈 stack」是一种遵循先入后出逻辑的线性数据结构。
「栈 stack」是一种遵循先入后出逻辑的线性数据结构。
我们可以将栈类比为桌面上的一摞盘子,如果想取出底部的盘子,则需要先将上面的盘子依次移走。我们将盘子替换为各种类型的元素(如整数、字符、对象等),就得到了栈这种数据结构。
@@ -8,17 +8,17 @@
![栈的先入后出规则](stack.assets/stack_operations.png)
## 栈常用操作
## 栈常用操作
栈的常用操作如下表所示,具体的方法名需要根据所使用的编程语言来确定。在此,我们以常见的 `push()``pop()``peek()` 命名为例。
<p align="center"> 表 <id> &nbsp; 栈的操作效率 </p>
| 方法 | 描述 | 时间复杂度 |
| ------ | ---------------------- | ---------- |
| push() | 元素入栈(添加至栈顶) | $O(1)$ |
| pop() | 栈顶元素出栈 | $O(1)$ |
| peek() | 访问栈顶元素 | $O(1)$ |
| 方法 | 描述 | 时间复杂度 |
| -------- | ---------------------- | ---------- |
| `push()` | 元素入栈(添加至栈顶) | $O(1)$ |
| `pop()` | 栈顶元素出栈 | $O(1)$ |
| `peek()` | 访问栈顶元素 | $O(1)$ |
通常情况下,我们可以直接使用编程语言内置的栈类。然而,某些语言可能没有专门提供栈类,这时我们可以将该语言的“数组”或“链表”当作栈来使用,并在程序逻辑上忽略与栈无关的操作。
@@ -26,7 +26,7 @@
```python title="stack.py"
# 初始化栈
# Python 没有内置的栈类,可以把 List 当作栈来使用
# Python 没有内置的栈类,可以把 list 当作栈来使用
stack: list[int] = []
# 元素入栈