2022-11-22 17:47:26 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# 栈
|
|
|
|
|
|
|
|
|
|
「栈 Stack」是一种遵循「先入后出 first in, last out」数据操作规则的线性数据结构。我们可以将栈类比为放在桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。
|
|
|
|
|
|
2023-01-02 21:52:40 +08:00
|
|
|
|
“盘子”是一种形象比喻,我们将盘子替换为任意一种元素(例如整数、字符、对象等),就得到了栈数据结构。
|
|
|
|
|
|
|
|
|
|
我们将这一摞元素的顶部称为「栈顶」,将底部称为「栈底」,将把元素添加到栈顶的操作称为「入栈」,将删除栈顶元素的操作称为「出栈」。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
![stack_operations](stack.assets/stack_operations.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 栈的先入后出特性 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
## 栈常用操作
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
栈的常用操作见下表(方法命名以 Java 为例)。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Table. 栈的常用操作 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
2023-01-09 02:17:40 +08:00
|
|
|
|
| 方法 | 描述 | 时间复杂度 |
|
|
|
|
|
| --------- | ---------------------- | ---------- |
|
|
|
|
|
| push() | 元素入栈(添加至栈顶) | $O(1)$ |
|
|
|
|
|
| pop() | 栈顶元素出栈 | $O(1)$ |
|
|
|
|
|
| peek() | 访问栈顶元素 | $O(1)$ |
|
|
|
|
|
| size() | 获取栈的长度 | $O(1)$ |
|
|
|
|
|
| isEmpty() | 判断栈是否为空 | $O(1)$ |
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
我们可以直接使用编程语言实现好的栈类。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="stack.java"
|
|
|
|
|
/* 初始化栈 */
|
2022-11-30 02:27:26 +08:00
|
|
|
|
// 在 Java 中,推荐将 LinkedList 当作栈来使用
|
|
|
|
|
LinkedList<Integer> stack = new LinkedList<>();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 元素入栈 */
|
2022-11-30 02:27:26 +08:00
|
|
|
|
stack.addLast(1);
|
|
|
|
|
stack.addLast(3);
|
|
|
|
|
stack.addLast(2);
|
|
|
|
|
stack.addLast(5);
|
|
|
|
|
stack.addLast(4);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
2022-11-30 02:27:26 +08:00
|
|
|
|
int peek = stack.peekLast();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 元素出栈 */
|
2022-11-30 02:27:26 +08:00
|
|
|
|
int pop = stack.removeLast();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size = stack.size();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
boolean isEmpty = stack.isEmpty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="stack.cpp"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
stack<int> stack;
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.push(1);
|
|
|
|
|
stack.push(3);
|
|
|
|
|
stack.push(2);
|
|
|
|
|
stack.push(5);
|
|
|
|
|
stack.push(4);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int top = stack.top();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
stack.pop();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size = stack.size();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
bool empty = stack.empty();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="stack.py"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 初始化栈 """
|
2022-11-30 03:46:53 +08:00
|
|
|
|
# Python 没有内置的栈类,可以把 List 当作栈来使用
|
2022-11-30 02:27:26 +08:00
|
|
|
|
stack = []
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 元素入栈 """
|
|
|
|
|
stack.append(1)
|
|
|
|
|
stack.append(3)
|
|
|
|
|
stack.append(2)
|
|
|
|
|
stack.append(5)
|
|
|
|
|
stack.append(4)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 访问栈顶元素 """
|
|
|
|
|
peek = stack[-1]
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 元素出栈 """
|
|
|
|
|
pop = stack.pop()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 获取栈的长度 """
|
|
|
|
|
size = len(stack)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 判断是否为空 """
|
|
|
|
|
is_empty = len(stack) == 0
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
2022-12-15 23:30:53 +08:00
|
|
|
|
```go title="stack_test.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// 在 Go 中,推荐将 Slice 当作栈来使用
|
|
|
|
|
var stack []int
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack = append(stack, 1)
|
|
|
|
|
stack = append(stack, 3)
|
|
|
|
|
stack = append(stack, 2)
|
|
|
|
|
stack = append(stack, 5)
|
|
|
|
|
stack = append(stack, 4)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
peek := stack[len(stack)-1]
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
pop := stack[len(stack)-1]
|
|
|
|
|
stack = stack[:len(stack)-1]
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
size := len(stack)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
isEmpty := len(stack) == 0
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="stack.js"
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
|
|
|
|
|
const stack = [];
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.push(1);
|
|
|
|
|
stack.push(3);
|
|
|
|
|
stack.push(2);
|
|
|
|
|
stack.push(5);
|
|
|
|
|
stack.push(4);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
2022-12-04 10:22:18 +08:00
|
|
|
|
const peek = stack[stack.length-1];
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 元素出栈 */
|
2022-12-04 10:22:18 +08:00
|
|
|
|
const pop = stack.pop();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 获取栈的长度 */
|
2022-12-04 10:22:18 +08:00
|
|
|
|
const size = stack.length;
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 判断是否为空 */
|
2022-12-04 10:22:18 +08:00
|
|
|
|
const is_empty = stack.length === 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="stack.ts"
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// Typescript 没有内置的栈类,可以把 Array 当作栈来使用
|
2022-12-04 10:17:18 +08:00
|
|
|
|
const stack: number[] = [];
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.push(1);
|
|
|
|
|
stack.push(3);
|
|
|
|
|
stack.push(2);
|
|
|
|
|
stack.push(5);
|
|
|
|
|
stack.push(4);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
2022-12-04 10:17:18 +08:00
|
|
|
|
const peek = stack[stack.length - 1];
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 元素出栈 */
|
2022-12-04 10:17:18 +08:00
|
|
|
|
const pop = stack.pop();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 获取栈的长度 */
|
2022-12-04 10:17:18 +08:00
|
|
|
|
const size = stack.length;
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 23:00:13 +08:00
|
|
|
|
/* 判断是否为空 */
|
2022-12-04 10:17:18 +08:00
|
|
|
|
const is_empty = stack.length === 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="stack.c"
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="stack.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
Stack<int> stack = new ();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.Push(1);
|
|
|
|
|
stack.Push(3);
|
|
|
|
|
stack.Push(2);
|
|
|
|
|
stack.Push(5);
|
|
|
|
|
stack.Push(4);
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int peek = stack.Peek();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
int pop = stack.Pop();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size = stack.Count();
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
bool isEmpty = stack.Count()==0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="stack.swift"
|
2023-01-09 23:02:22 +08:00
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// Swift 没有内置的栈类,可以把 Array 当作栈来使用
|
|
|
|
|
var stack: [Int] = []
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.append(1)
|
|
|
|
|
stack.append(3)
|
|
|
|
|
stack.append(2)
|
|
|
|
|
stack.append(5)
|
|
|
|
|
stack.append(4)
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-01-09 23:02:22 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
let peek = stack.last!
|
|
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
let pop = stack.removeLast()
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
let size = stack.count
|
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
let isEmpty = stack.isEmpty
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
## 栈的实现
|
|
|
|
|
|
|
|
|
|
为了更加清晰地了解栈的运行机制,接下来我们来自己动手实现一个栈类。
|
|
|
|
|
|
2022-12-21 01:37:58 +08:00
|
|
|
|
栈规定元素是先入后出的,因此我们只能在栈顶添加或删除元素。然而,数组或链表都可以在任意位置添加删除元素,因此 **栈可被看作是一种受约束的数组或链表**。换言之,我们可以“屏蔽”数组或链表的部分无关操作,使之对外的表现逻辑符合栈的规定即可。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
### 基于链表的实现
|
|
|
|
|
|
2023-01-05 01:39:22 +08:00
|
|
|
|
使用「链表」实现栈时,将链表的头结点看作栈顶,尾结点看作栈底。
|
|
|
|
|
|
|
|
|
|
对于入栈操作,将元素插入到链表头部即可,这种结点添加方式被称为“头插法”。而对于出栈操作,则将头结点从链表中删除即可。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
受益于链表的离散存储方式,栈的扩容更加灵活,删除元素的内存也会被系统自动回收;缺点是无法像数组一样高效地随机访问,并且由于链表结点需存储指针,导致单个元素占用空间更大。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linkedlist_stack.java"
|
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
private ListNode stackPeek; // 将头结点作为栈顶
|
2022-11-30 03:46:53 +08:00
|
|
|
|
private int stkSize = 0; // 栈的长度
|
2022-11-22 17:47:26 +08:00
|
|
|
|
public LinkedListStack() {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
stackPeek = null;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
public int size() {
|
2022-11-30 03:46:53 +08:00
|
|
|
|
return stkSize;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
public void push(int num) {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
ListNode node = new ListNode(num);
|
|
|
|
|
node.next = stackPeek;
|
|
|
|
|
stackPeek = node;
|
2022-11-30 03:46:53 +08:00
|
|
|
|
stkSize++;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
public int pop() {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
int num = peek();
|
|
|
|
|
stackPeek = stackPeek.next;
|
2022-11-30 03:46:53 +08:00
|
|
|
|
stkSize--;
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return num;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
public int peek() {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
if (size() == 0)
|
2022-12-20 14:13:21 +08:00
|
|
|
|
throw new EmptyStackException();
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return stackPeek.val;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linkedlist_stack.cpp"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
|
|
|
|
private:
|
|
|
|
|
ListNode* stackTop; // 将头结点作为栈顶
|
2022-11-30 03:46:53 +08:00
|
|
|
|
int stkSize; // 栈的长度
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
public:
|
|
|
|
|
LinkedListStack() {
|
|
|
|
|
stackTop = nullptr;
|
2022-11-30 03:46:53 +08:00
|
|
|
|
stkSize = 0;
|
2022-11-30 02:27:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size() {
|
2022-11-30 03:46:53 +08:00
|
|
|
|
return stkSize;
|
2022-11-30 02:27:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool empty() {
|
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(int num) {
|
|
|
|
|
ListNode* node = new ListNode(num);
|
|
|
|
|
node->next = stackTop;
|
|
|
|
|
stackTop = node;
|
2022-11-30 03:46:53 +08:00
|
|
|
|
stkSize++;
|
2022-11-30 02:27:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
2023-01-05 01:59:31 +08:00
|
|
|
|
void pop() {
|
2022-11-30 03:46:53 +08:00
|
|
|
|
int num = top();
|
2023-01-02 19:53:55 +08:00
|
|
|
|
ListNode *tmp = stackTop;
|
2022-11-30 02:27:26 +08:00
|
|
|
|
stackTop = stackTop->next;
|
2023-01-02 19:53:55 +08:00
|
|
|
|
// 释放内存
|
|
|
|
|
delete tmp;
|
2022-11-30 03:46:53 +08:00
|
|
|
|
stkSize--;
|
2022-11-30 02:27:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int top() {
|
|
|
|
|
if (size() == 0)
|
|
|
|
|
throw out_of_range("栈为空");
|
|
|
|
|
return stackTop->val;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linkedlist_stack.py"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 基于链表实现的栈 """
|
|
|
|
|
class LinkedListStack:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.__peek = None
|
|
|
|
|
self.__size = 0
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 获取栈的长度 """
|
|
|
|
|
def size(self):
|
|
|
|
|
return self.__size
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 判断栈是否为空 """
|
|
|
|
|
def is_empty(self):
|
|
|
|
|
return not self.__peek
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 入栈 """
|
|
|
|
|
def push(self, val):
|
|
|
|
|
node = ListNode(val)
|
|
|
|
|
node.next = self.__peek
|
|
|
|
|
self.__peek = node
|
|
|
|
|
self.__size += 1
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 出栈 """
|
|
|
|
|
def pop(self):
|
2022-11-30 03:46:53 +08:00
|
|
|
|
num = self.peek()
|
2022-11-30 02:27:26 +08:00
|
|
|
|
self.__peek = self.__peek.next
|
|
|
|
|
self.__size -= 1
|
2022-11-30 03:46:53 +08:00
|
|
|
|
return num
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 访问栈顶元素 """
|
|
|
|
|
def peek(self):
|
|
|
|
|
# 判空处理
|
|
|
|
|
if not self.__peek: return None
|
|
|
|
|
return self.__peek.val
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linkedlist_stack.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 基于链表实现的栈 */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
type linkedListStack struct {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
// 使用内置包 list 来实现栈
|
|
|
|
|
data *list.List
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// newLinkedListStack 初始化链表
|
|
|
|
|
func newLinkedListStack() *linkedListStack {
|
|
|
|
|
return &linkedListStack{
|
2022-12-03 20:25:24 +08:00
|
|
|
|
data: list.New(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// push 入栈
|
|
|
|
|
func (s *linkedListStack) push(value int) {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
s.data.PushBack(value)
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// pop 出栈
|
|
|
|
|
func (s *linkedListStack) pop() any {
|
|
|
|
|
if s.isEmpty() {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
e := s.data.Back()
|
|
|
|
|
s.data.Remove(e)
|
|
|
|
|
return e.Value
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// peek 访问栈顶元素
|
|
|
|
|
func (s *linkedListStack) peek() any {
|
|
|
|
|
if s.isEmpty() {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
e := s.data.Back()
|
|
|
|
|
return e.Value
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// size 获取栈的长度
|
|
|
|
|
func (s *linkedListStack) size() int {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
return s.data.Len()
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// isEmpty 判断栈是否为空
|
|
|
|
|
func (s *linkedListStack) isEmpty() bool {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
return s.data.Len() == 0
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="linkedlist_stack.js"
|
2022-12-24 09:01:22 +08:00
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
|
|
|
|
#stackPeek; // 将头结点作为栈顶
|
|
|
|
|
#stkSize = 0; // 栈的长度
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-24 09:01:22 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
this.#stackPeek = null;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-24 09:01:22 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size() {
|
|
|
|
|
return this.#stkSize;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-24 09:01:22 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
isEmpty() {
|
|
|
|
|
return this.size == 0;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-24 09:01:22 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num) {
|
|
|
|
|
const node = new ListNode(num);
|
|
|
|
|
node.next = this.#stackPeek;
|
|
|
|
|
this.#stackPeek = node;
|
|
|
|
|
this.#stkSize++;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-24 09:01:22 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop() {
|
|
|
|
|
const num = this.peek();
|
|
|
|
|
if (!this.#stackPeek) {
|
|
|
|
|
throw new Error("栈为空!");
|
|
|
|
|
}
|
|
|
|
|
this.#stackPeek = this.#stackPeek.next;
|
|
|
|
|
this.#stkSize--;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-24 09:01:22 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
peek() {
|
|
|
|
|
if (!this.#stackPeek) {
|
|
|
|
|
throw new Error("栈为空!");
|
|
|
|
|
}
|
|
|
|
|
return this.#stackPeek.val;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-24 09:01:22 +08:00
|
|
|
|
/* 将链表转化为 Array 并返回 */
|
|
|
|
|
toArray() {
|
|
|
|
|
let node = this.#stackPeek;
|
|
|
|
|
const res = new Array(this.size);
|
|
|
|
|
for (let i = res.length - 1; i >= 0; i--) {
|
|
|
|
|
res[i] = node.val;
|
|
|
|
|
node = node.next;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linkedlist_stack.ts"
|
2022-12-23 12:08:15 +08:00
|
|
|
|
/* 基于链表实现的栈 */
|
2022-12-22 09:29:55 +08:00
|
|
|
|
class LinkedListStack {
|
|
|
|
|
private stackPeek: ListNode | null; // 将头结点作为栈顶
|
|
|
|
|
private stkSize: number = 0; // 栈的长度
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-22 09:29:55 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
this.stackPeek = null;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-22 09:29:55 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size(): number {
|
|
|
|
|
return this.stkSize;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-22 09:29:55 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
isEmpty(): boolean {
|
|
|
|
|
return this.size == 0;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-22 09:29:55 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num: number): void {
|
|
|
|
|
const node = new ListNode(num);
|
|
|
|
|
node.next = this.stackPeek;
|
|
|
|
|
this.stackPeek = node;
|
|
|
|
|
this.stkSize++;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-22 09:29:55 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop(): number {
|
|
|
|
|
const num = this.peek();
|
|
|
|
|
if (!this.stackPeek) {
|
|
|
|
|
throw new Error("栈为空!");
|
|
|
|
|
}
|
|
|
|
|
this.stackPeek = this.stackPeek.next;
|
|
|
|
|
this.stkSize--;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-22 09:29:55 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
peek(): number {
|
|
|
|
|
if (!this.stackPeek) {
|
|
|
|
|
throw new Error("栈为空!");
|
|
|
|
|
}
|
|
|
|
|
return this.stackPeek.val;
|
|
|
|
|
}
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-23 12:08:15 +08:00
|
|
|
|
/* 将链表转化为 Array 并返回 */
|
2022-12-22 09:29:55 +08:00
|
|
|
|
toArray(): number[] {
|
|
|
|
|
let node = this.stackPeek;
|
|
|
|
|
const res = new Array<number>(this.size);
|
|
|
|
|
for (let i = res.length - 1; i >= 0; i--) {
|
|
|
|
|
res[i] = node!.val;
|
|
|
|
|
node = node!.next;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linkedlist_stack.c"
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linkedlist_stack.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack
|
|
|
|
|
{
|
|
|
|
|
private ListNode stackPeek; // 将头结点作为栈顶
|
|
|
|
|
private int stkSize = 0; // 栈的长度
|
|
|
|
|
public LinkedListStack()
|
|
|
|
|
{
|
|
|
|
|
stackPeek = null;
|
|
|
|
|
}
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
public int size()
|
|
|
|
|
{
|
|
|
|
|
return stkSize;
|
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
public bool isEmpty()
|
|
|
|
|
{
|
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
public void push(int num)
|
|
|
|
|
{
|
|
|
|
|
ListNode node = new ListNode(num);
|
|
|
|
|
node.next = stackPeek;
|
|
|
|
|
stackPeek = node;
|
|
|
|
|
stkSize++;
|
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
public int pop()
|
|
|
|
|
{
|
|
|
|
|
int num = peek();
|
|
|
|
|
stackPeek = stackPeek?.next;
|
|
|
|
|
stkSize--;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
public int peek()
|
|
|
|
|
{
|
|
|
|
|
if (size() == 0)
|
|
|
|
|
throw new Exception();
|
|
|
|
|
return stackPeek.val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linkedlist_stack.swift"
|
2023-01-09 23:02:22 +08:00
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
|
|
|
|
private var _peek: ListNode? // 将头结点作为栈顶
|
|
|
|
|
private var _size = 0 // 栈的长度
|
|
|
|
|
|
|
|
|
|
init() {}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
func size() -> Int {
|
|
|
|
|
_size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
func isEmpty() -> Bool {
|
|
|
|
|
_size == 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
func push(num: Int) {
|
|
|
|
|
let node = ListNode(x: num)
|
|
|
|
|
node.next = _peek
|
|
|
|
|
_peek = node
|
|
|
|
|
_size += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
func pop() -> Int {
|
|
|
|
|
let num = peek()
|
|
|
|
|
_peek = _peek?.next
|
|
|
|
|
_size -= 1
|
|
|
|
|
return num
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-01-09 23:02:22 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
func peek() -> Int {
|
|
|
|
|
if _size == 0 {
|
|
|
|
|
fatalError("栈为空")
|
|
|
|
|
}
|
|
|
|
|
return _peek!.val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
|
|
|
|
func toArray() -> [Int] {
|
|
|
|
|
var node = _peek
|
|
|
|
|
var res = Array(repeating: 0, count: _size)
|
|
|
|
|
for i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {
|
|
|
|
|
res[i] = node!.val
|
|
|
|
|
node = node?.next
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
### 基于数组的实现
|
|
|
|
|
|
2023-01-05 01:39:22 +08:00
|
|
|
|
使用「数组」实现栈时,将数组的尾部当作栈顶,这样可以保证入栈与出栈操作的时间复杂度都为 $O(1)$ 。准确地说,由于入栈的元素可能是源源不断的,我们需要使用可以动态扩容的「列表」。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
基于数组实现的栈,优点是支持随机访问,缺点是会造成一定的空间浪费,因为列表的容量始终 $\geq$ 元素数量。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array_stack.java"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
private ArrayList<Integer> stack;
|
2022-11-22 17:47:26 +08:00
|
|
|
|
public ArrayStack() {
|
|
|
|
|
// 初始化列表(动态数组)
|
2022-11-30 02:27:26 +08:00
|
|
|
|
stack = new ArrayList<>();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
public int size() {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return stack.size();
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
public void push(int num) {
|
2022-11-30 02:27:26 +08:00
|
|
|
|
stack.add(num);
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
public int pop() {
|
2022-12-20 14:13:21 +08:00
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new EmptyStackException();
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return stack.remove(size() - 1);
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
public int peek() {
|
2022-12-20 14:13:21 +08:00
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new EmptyStackException();
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return stack.get(size() - 1);
|
2022-11-22 17:47:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array_stack.cpp"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
private:
|
|
|
|
|
vector<int> stack;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size() {
|
|
|
|
|
return stack.size();
|
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool empty() {
|
|
|
|
|
return stack.empty();
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(int num) {
|
|
|
|
|
stack.push_back(num);
|
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
2023-01-05 01:59:31 +08:00
|
|
|
|
void pop() {
|
2022-12-20 14:13:21 +08:00
|
|
|
|
int oldTop = top();
|
2022-11-30 02:27:26 +08:00
|
|
|
|
stack.pop_back();
|
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int top() {
|
2022-12-20 14:13:21 +08:00
|
|
|
|
if(empty())
|
|
|
|
|
throw out_of_range("栈为空");
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return stack.back();
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array_stack.py"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 基于数组实现的栈 """
|
|
|
|
|
class ArrayStack:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.__stack = []
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 获取栈的长度 """
|
|
|
|
|
def size(self):
|
|
|
|
|
return len(self.__stack)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 判断栈是否为空 """
|
|
|
|
|
def is_empty(self):
|
|
|
|
|
return self.__stack == []
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 入栈 """
|
|
|
|
|
def push(self, item):
|
|
|
|
|
self.__stack.append(item)
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 出栈 """
|
|
|
|
|
def pop(self):
|
2022-12-20 14:13:21 +08:00
|
|
|
|
assert not self.is_empty(), "栈为空"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return self.__stack.pop()
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-11-30 02:27:26 +08:00
|
|
|
|
""" 访问栈顶元素 """
|
|
|
|
|
def peek(self):
|
2022-12-20 14:13:21 +08:00
|
|
|
|
assert not self.is_empty(), "栈为空"
|
2022-11-30 02:27:26 +08:00
|
|
|
|
return self.__stack[-1]
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array_stack.go"
|
2022-12-03 20:25:24 +08:00
|
|
|
|
/* 基于数组实现的栈 */
|
2023-01-08 20:29:13 +08:00
|
|
|
|
type arrayStack struct {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
data []int // 数据
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
func newArrayStack() *arrayStack {
|
|
|
|
|
return &arrayStack{
|
2022-12-03 20:25:24 +08:00
|
|
|
|
// 设置栈的长度为 0,容量为 16
|
|
|
|
|
data: make([]int, 0, 16),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// size 栈的长度
|
|
|
|
|
func (s *arrayStack) size() int {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
return len(s.data)
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// isEmpty 栈是否为空
|
|
|
|
|
func (s *arrayStack) isEmpty() bool {
|
|
|
|
|
return s.size() == 0
|
2022-12-03 20:25:24 +08:00
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// push 入栈
|
|
|
|
|
func (s *arrayStack) push(v int) {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
// 切片会自动扩容
|
|
|
|
|
s.data = append(s.data, v)
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// pop 出栈
|
|
|
|
|
func (s *arrayStack) pop() any {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
// 弹出栈前,先判断是否为空
|
2023-01-08 20:29:13 +08:00
|
|
|
|
if s.isEmpty() {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
val := s.peek()
|
2022-12-03 20:25:24 +08:00
|
|
|
|
s.data = s.data[:len(s.data)-1]
|
|
|
|
|
return val
|
|
|
|
|
}
|
2023-01-08 20:29:13 +08:00
|
|
|
|
|
|
|
|
|
// peek 获取栈顶元素
|
|
|
|
|
func (s *arrayStack) peek() any {
|
|
|
|
|
if s.isEmpty() {
|
2022-12-03 20:25:24 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
val := s.data[len(s.data)-1]
|
|
|
|
|
return val
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```js title="array_stack.js"
|
2022-12-10 00:00:07 +08:00
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
stack;
|
|
|
|
|
constructor() {
|
|
|
|
|
this.stack = [];
|
|
|
|
|
}
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size() {
|
|
|
|
|
return this.stack.length;
|
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
empty() {
|
|
|
|
|
return this.stack.length === 0;
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num) {
|
|
|
|
|
this.stack.push(num);
|
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop() {
|
2022-12-20 21:33:14 +08:00
|
|
|
|
if (this.empty())
|
|
|
|
|
throw new Error("栈为空");
|
2022-12-10 00:00:07 +08:00
|
|
|
|
return this.stack.pop();
|
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
top() {
|
2022-12-20 21:33:14 +08:00
|
|
|
|
if (this.empty())
|
|
|
|
|
throw new Error("栈为空");
|
2022-12-10 00:00:07 +08:00
|
|
|
|
return this.stack[this.stack.length - 1];
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array_stack.ts"
|
2022-12-10 00:00:07 +08:00
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
private stack: number[];
|
|
|
|
|
constructor() {
|
|
|
|
|
this.stack = [];
|
|
|
|
|
}
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size(): number {
|
|
|
|
|
return this.stack.length;
|
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
empty(): boolean {
|
|
|
|
|
return this.stack.length === 0;
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num: number): void {
|
|
|
|
|
this.stack.push(num);
|
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop(): number | undefined {
|
2022-12-20 21:33:14 +08:00
|
|
|
|
if (this.empty())
|
|
|
|
|
throw new Error('栈为空');
|
2022-12-10 00:00:07 +08:00
|
|
|
|
return this.stack.pop();
|
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
top(): number | undefined {
|
2022-12-20 21:33:14 +08:00
|
|
|
|
if (this.empty())
|
|
|
|
|
throw new Error('栈为空');
|
2022-12-10 00:00:07 +08:00
|
|
|
|
return this.stack[this.stack.length - 1];
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array_stack.c"
|
2023-01-07 20:34:32 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array_stack.cs"
|
2022-12-23 15:42:02 +08:00
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack
|
|
|
|
|
{
|
|
|
|
|
private List<int> stack;
|
|
|
|
|
public ArrayStack()
|
|
|
|
|
{
|
|
|
|
|
// 初始化列表(动态数组)
|
|
|
|
|
stack = new();
|
|
|
|
|
}
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
public int size()
|
|
|
|
|
{
|
|
|
|
|
return stack.Count();
|
|
|
|
|
}
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
public bool isEmpty()
|
|
|
|
|
{
|
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
public void push(int num)
|
|
|
|
|
{
|
|
|
|
|
stack.Add(num);
|
|
|
|
|
}
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
public int pop()
|
|
|
|
|
{
|
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new Exception();
|
|
|
|
|
var val = peek();
|
|
|
|
|
stack.RemoveAt(size() - 1);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
public int peek()
|
|
|
|
|
{
|
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new Exception();
|
|
|
|
|
return stack[size() - 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array_stack.swift"
|
2023-01-09 23:02:22 +08:00
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
private var stack: [Int]
|
|
|
|
|
|
|
|
|
|
init() {
|
|
|
|
|
// 初始化列表(动态数组)
|
|
|
|
|
stack = []
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
2023-01-09 23:02:22 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
func size() -> Int {
|
|
|
|
|
stack.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
func isEmpty() -> Bool {
|
|
|
|
|
stack.isEmpty
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
func push(num: Int) {
|
|
|
|
|
stack.append(num)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
func pop() -> Int {
|
|
|
|
|
if stack.isEmpty {
|
|
|
|
|
fatalError("栈为空")
|
|
|
|
|
}
|
|
|
|
|
return stack.removeLast()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
func peek() -> Int {
|
|
|
|
|
if stack.isEmpty {
|
|
|
|
|
fatalError("栈为空")
|
|
|
|
|
}
|
|
|
|
|
return stack.last!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
|
|
|
|
func toArray() -> [Int] {
|
|
|
|
|
stack
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
!!! tip
|
|
|
|
|
|
2023-01-06 03:13:06 +08:00
|
|
|
|
某些语言并未专门提供栈类,但我们可以直接把该语言的「数组」或「链表」看作栈来使用,并通过“脑补”来屏蔽无关操作,而无需像上述代码去特意包装一层。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
## 栈典型应用
|
|
|
|
|
|
2023-01-09 11:15:26 +08:00
|
|
|
|
- **浏览器中的后退与前进、软件中的撤销与反撤销**。每当我们打开新的网页,浏览器就将上一个网页执行入栈,这样我们就可以通过「后退」操作来回到上一页面,后退操作实际上是在执行出栈。如果要同时支持后退和前进,那么则需要两个栈来配合实现。
|
2023-01-07 20:34:32 +08:00
|
|
|
|
- **程序内存管理**。每当调用函数时,系统就会在栈顶添加一个栈帧,用来记录函数的上下文信息。在递归函数中,向下递推会不断执行入栈,向上回溯阶段时出栈。
|