mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 02:43:41 +08:00
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:
@@ -1,6 +1,6 @@
|
||||
# 栈
|
||||
|
||||
「栈 stack」是一种遵循先入后出的逻辑的线性数据结构。
|
||||
「栈 stack」是一种遵循先入后出逻辑的线性数据结构。
|
||||
|
||||
我们可以将栈类比为桌面上的一摞盘子,如果想取出底部的盘子,则需要先将上面的盘子依次移走。我们将盘子替换为各种类型的元素(如整数、字符、对象等),就得到了栈这种数据结构。
|
||||
|
||||
@@ -8,17 +8,17 @@
|
||||
|
||||

|
||||
|
||||
## 栈常用操作
|
||||
## 栈的常用操作
|
||||
|
||||
栈的常用操作如下表所示,具体的方法名需要根据所使用的编程语言来确定。在此,我们以常见的 `push()`、`pop()`、`peek()` 命名为例。
|
||||
|
||||
<p align="center"> 表 <id> 栈的操作效率 </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] = []
|
||||
|
||||
# 元素入栈
|
||||
|
||||
Reference in New Issue
Block a user