2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-02-28 20:06:23 +08:00
|
|
|
|
# 5.1. 栈
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
「栈 Stack」是一种遵循先入后出(First In, Last Out)原则的线性数据结构。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
我们可以将栈类比为桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。我们将盘子替换为各种类型的元素(如整数、字符、对象等),就得到了栈数据结构。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
在栈中,我们把堆叠元素的顶部称为「栈顶」,底部称为「栈底」。将把元素添加到栈顶的操作叫做「入栈」,而删除栈顶元素的操作叫做「出栈」。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![栈的先入后出规则](stack.assets/stack_operations.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 栈的先入后出规则 </p>
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 5.1.1. 栈常用操作
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
栈的常用操作如下表所示,具体的方法名需要根据所使用的编程语言来确定。在此,我们以常见的 `push()` , `pop()` , `peek()` 命名为例。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 | 时间复杂度 |
|
|
|
|
|
| --------- | ---------------------- | ---------- |
|
|
|
|
|
| push() | 元素入栈(添加至栈顶) | $O(1)$ |
|
|
|
|
|
| pop() | 栈顶元素出栈 | $O(1)$ |
|
|
|
|
|
| peek() | 访问栈顶元素 | $O(1)$ |
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
通常情况下,我们可以直接使用编程语言内置的栈类。然而,某些语言可能没有专门提供栈类,这时我们可以将该语言的「数组」或「链表」视作栈来使用,并通过“脑补”来忽略与栈无关的操作。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="stack.java"
|
|
|
|
|
/* 初始化栈 */
|
2023-03-12 04:13:23 +08:00
|
|
|
|
Stack<Integer> stack = new Stack<>();
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 元素入栈 */
|
2023-03-12 04:13:23 +08:00
|
|
|
|
stack.push(1);
|
|
|
|
|
stack.push(3);
|
|
|
|
|
stack.push(2);
|
|
|
|
|
stack.push(5);
|
|
|
|
|
stack.push(4);
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
2023-03-12 04:13:23 +08:00
|
|
|
|
int peek = stack.peek();
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 元素出栈 */
|
2023-03-12 04:13:23 +08:00
|
|
|
|
int pop = stack.pop();
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size = stack.size();
|
2023-03-12 04:13:23 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
boolean isEmpty = stack.isEmpty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="stack.cpp"
|
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
stack<int> stack;
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.push(1);
|
|
|
|
|
stack.push(3);
|
|
|
|
|
stack.push(2);
|
|
|
|
|
stack.push(5);
|
|
|
|
|
stack.push(4);
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int top = stack.top();
|
|
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
2023-03-12 04:13:23 +08:00
|
|
|
|
stack.pop(); // 无返回值
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size = stack.size();
|
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
bool empty = stack.empty();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="stack.py"
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 初始化栈
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# Python 没有内置的栈类,可以把 List 当作栈来使用
|
2023-03-12 18:46:03 +08:00
|
|
|
|
stack: List[int] = []
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 元素入栈
|
2023-02-08 15:20:18 +08:00
|
|
|
|
stack.append(1)
|
|
|
|
|
stack.append(3)
|
|
|
|
|
stack.append(2)
|
|
|
|
|
stack.append(5)
|
|
|
|
|
stack.append(4)
|
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 访问栈顶元素
|
2023-03-12 18:46:03 +08:00
|
|
|
|
peek: int = stack[-1]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 元素出栈
|
2023-03-12 18:46:03 +08:00
|
|
|
|
pop: int = stack.pop()
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 获取栈的长度
|
2023-03-12 18:46:03 +08:00
|
|
|
|
size: int = len(stack)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 05:30:47 +08:00
|
|
|
|
# 判断是否为空
|
2023-03-12 18:46:03 +08:00
|
|
|
|
is_empty: bool = len(stack) == 0
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="stack_test.go"
|
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// 在 Go 中,推荐将 Slice 当作栈来使用
|
|
|
|
|
var stack []int
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack = append(stack, 1)
|
|
|
|
|
stack = append(stack, 3)
|
|
|
|
|
stack = append(stack, 2)
|
|
|
|
|
stack = append(stack, 5)
|
|
|
|
|
stack = append(stack, 4)
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
peek := stack[len(stack)-1]
|
|
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
pop := stack[len(stack)-1]
|
|
|
|
|
stack = stack[:len(stack)-1]
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
size := len(stack)
|
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
isEmpty := len(stack) == 0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="stack.js"
|
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
|
|
|
|
|
const stack = [];
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.push(1);
|
|
|
|
|
stack.push(3);
|
|
|
|
|
stack.push(2);
|
|
|
|
|
stack.push(5);
|
|
|
|
|
stack.push(4);
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
const peek = stack[stack.length-1];
|
|
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
const pop = stack.pop();
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
const size = stack.length;
|
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
const is_empty = stack.length === 0;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="stack.ts"
|
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// Typescript 没有内置的栈类,可以把 Array 当作栈来使用
|
|
|
|
|
const stack: number[] = [];
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.push(1);
|
|
|
|
|
stack.push(3);
|
|
|
|
|
stack.push(2);
|
|
|
|
|
stack.push(5);
|
|
|
|
|
stack.push(4);
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
const peek = stack[stack.length - 1];
|
|
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
const pop = stack.pop();
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
const size = stack.length;
|
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
const is_empty = stack.length === 0;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="stack.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
// C 未提供内置栈
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="stack.cs"
|
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
Stack<int> stack = new ();
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.Push(1);
|
|
|
|
|
stack.Push(3);
|
|
|
|
|
stack.Push(2);
|
|
|
|
|
stack.Push(5);
|
|
|
|
|
stack.Push(4);
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int peek = stack.Peek();
|
|
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
int pop = stack.Pop();
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
2023-07-25 16:42:55 +08:00
|
|
|
|
int size = stack.Count;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
2023-07-25 16:42:55 +08:00
|
|
|
|
bool isEmpty = stack.Count == 0;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="stack.swift"
|
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// Swift 没有内置的栈类,可以把 Array 当作栈来使用
|
|
|
|
|
var stack: [Int] = []
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.append(1)
|
|
|
|
|
stack.append(3)
|
|
|
|
|
stack.append(2)
|
|
|
|
|
stack.append(5)
|
|
|
|
|
stack.append(4)
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
let peek = stack.last!
|
|
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
let pop = stack.removeLast()
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
let size = stack.count
|
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
let isEmpty = stack.isEmpty
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="stack.zig"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="stack.dart"
|
2023-06-02 15:07:33 +08:00
|
|
|
|
/* 初始化栈 */
|
|
|
|
|
// Dart 没有内置的栈类,可以把 List 当作栈来使用
|
|
|
|
|
List<int> stack = [];
|
|
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
|
stack.add(1);
|
|
|
|
|
stack.add(3);
|
|
|
|
|
stack.add(2);
|
|
|
|
|
stack.add(5);
|
|
|
|
|
stack.add(4);
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int peek = stack.last;
|
2023-06-02 02:38:24 +08:00
|
|
|
|
|
2023-06-02 15:07:33 +08:00
|
|
|
|
/* 元素出栈 */
|
|
|
|
|
int pop = stack.removeLast();
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size = stack.length;
|
|
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
|
bool isEmpty = stack.isEmpty;
|
2023-06-02 02:38:24 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 5.1.2. 栈的实现
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
为了深入了解栈的运行机制,我们来尝试自己实现一个栈类。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
栈遵循先入后出的原则,因此我们只能在栈顶添加或删除元素。然而,数组和链表都可以在任意位置添加和删除元素,**因此栈可以被视为一种受限制的数组或链表**。换句话说,我们可以“屏蔽”数组或链表的部分无关操作,使其对外表现的逻辑符合栈的特性。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
### 基于链表的实现
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
使用链表来实现栈时,我们可以将链表的头节点视为栈顶,尾节点视为栈底。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
对于入栈操作,我们只需将元素插入链表头部,这种节点插入方法被称为“头插法”。而对于出栈操作,只需将头节点从链表中删除即可。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "LinkedListStack"
|
2023-02-26 19:22:52 +08:00
|
|
|
|
![基于链表实现栈的入栈出栈操作](stack.assets/linkedlist_stack.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "push()"
|
|
|
|
|
![linkedlist_stack_push](stack.assets/linkedlist_stack_push.png)
|
|
|
|
|
|
|
|
|
|
=== "pop()"
|
|
|
|
|
![linkedlist_stack_pop](stack.assets/linkedlist_stack_pop.png)
|
|
|
|
|
|
|
|
|
|
以下是基于链表实现栈的示例代码。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linkedlist_stack.java"
|
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
private ListNode stackPeek; // 将头节点作为栈顶
|
|
|
|
|
private int stkSize = 0; // 栈的长度
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
public LinkedListStack() {
|
|
|
|
|
stackPeek = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
public int size() {
|
|
|
|
|
return stkSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
public boolean 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)
|
2023-04-23 14:58:03 +08:00
|
|
|
|
throw new IndexOutOfBoundsException();
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return stackPeek.val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
|
|
|
|
public int[] toArray() {
|
|
|
|
|
ListNode node = stackPeek;
|
|
|
|
|
int[] res = new int[size()];
|
|
|
|
|
for (int i = res.length - 1; i >= 0; i--) {
|
|
|
|
|
res[i] = node.val;
|
|
|
|
|
node = node.next;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linkedlist_stack.cpp"
|
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
private:
|
|
|
|
|
ListNode *stackTop; // 将头节点作为栈顶
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int stkSize; // 栈的长度
|
|
|
|
|
|
2023-04-14 04:01:38 +08:00
|
|
|
|
public:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
LinkedListStack() {
|
|
|
|
|
stackTop = nullptr;
|
|
|
|
|
stkSize = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~LinkedListStack() {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 遍历链表删除节点,释放内存
|
2023-02-08 15:20:18 +08:00
|
|
|
|
freeMemoryLinkedList(stackTop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size() {
|
|
|
|
|
return stkSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool empty() {
|
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(int num) {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
ListNode *node = new ListNode(num);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node->next = stackTop;
|
|
|
|
|
stackTop = node;
|
|
|
|
|
stkSize++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
void pop() {
|
|
|
|
|
int num = top();
|
|
|
|
|
ListNode *tmp = stackTop;
|
|
|
|
|
stackTop = stackTop->next;
|
|
|
|
|
// 释放内存
|
|
|
|
|
delete tmp;
|
|
|
|
|
stkSize--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int top() {
|
|
|
|
|
if (size() == 0)
|
|
|
|
|
throw out_of_range("栈为空");
|
|
|
|
|
return stackTop->val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
|
|
|
|
vector<int> toVector() {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
ListNode *node = stackTop;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
vector<int> res(size());
|
|
|
|
|
for (int i = res.size() - 1; i >= 0; i--) {
|
|
|
|
|
res[i] = node->val;
|
|
|
|
|
node = node->next;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linkedlist_stack.py"
|
|
|
|
|
class LinkedListStack:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""基于链表实现的栈"""
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
def __init__(self):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""构造方法"""
|
2023-03-23 18:56:56 +08:00
|
|
|
|
self.__peek: ListNode | None = None
|
2023-03-12 18:46:03 +08:00
|
|
|
|
self.__size: int = 0
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def size(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""获取栈的长度"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return self.__size
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def is_empty(self) -> bool:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""判断栈是否为空"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return not self.__peek
|
|
|
|
|
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def push(self, val: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""入栈"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node = ListNode(val)
|
|
|
|
|
node.next = self.__peek
|
|
|
|
|
self.__peek = node
|
|
|
|
|
self.__size += 1
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def pop(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""出栈"""
|
2023-03-12 18:46:03 +08:00
|
|
|
|
num: int = self.peek()
|
2023-02-08 15:20:18 +08:00
|
|
|
|
self.__peek = self.__peek.next
|
|
|
|
|
self.__size -= 1
|
|
|
|
|
return num
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def peek(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""访问栈顶元素"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 判空处理
|
2023-04-09 05:12:22 +08:00
|
|
|
|
if not self.__peek:
|
|
|
|
|
return None
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return self.__peek.val
|
|
|
|
|
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def to_list(self) -> list[int]:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""转化为列表用于打印"""
|
2023-05-22 22:01:18 +08:00
|
|
|
|
arr = []
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node = self.__peek
|
|
|
|
|
while node:
|
|
|
|
|
arr.append(node.val)
|
|
|
|
|
node = node.next
|
|
|
|
|
arr.reverse()
|
|
|
|
|
return arr
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linkedlist_stack.go"
|
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
type linkedListStack struct {
|
|
|
|
|
// 使用内置包 list 来实现栈
|
|
|
|
|
data *list.List
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 初始化栈 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func newLinkedListStack() *linkedListStack {
|
|
|
|
|
return &linkedListStack{
|
|
|
|
|
data: list.New(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *linkedListStack) push(value int) {
|
|
|
|
|
s.data.PushBack(value)
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *linkedListStack) pop() any {
|
|
|
|
|
if s.isEmpty() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
e := s.data.Back()
|
|
|
|
|
s.data.Remove(e)
|
|
|
|
|
return e.Value
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *linkedListStack) peek() any {
|
|
|
|
|
if s.isEmpty() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
e := s.data.Back()
|
|
|
|
|
return e.Value
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *linkedListStack) size() int {
|
|
|
|
|
return s.data.Len()
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *linkedListStack) isEmpty() bool {
|
|
|
|
|
return s.data.Len() == 0
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 获取 List 用于打印 */
|
|
|
|
|
func (s *linkedListStack) toList() *list.List {
|
|
|
|
|
return s.data
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="linkedlist_stack.js"
|
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
#stackPeek; // 将头节点作为栈顶
|
|
|
|
|
#stkSize = 0; // 栈的长度
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
this.#stackPeek = null;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size() {
|
|
|
|
|
return this.#stkSize;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
isEmpty() {
|
|
|
|
|
return this.size == 0;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num) {
|
|
|
|
|
const node = new ListNode(num);
|
|
|
|
|
node.next = this.#stackPeek;
|
|
|
|
|
this.#stackPeek = node;
|
|
|
|
|
this.#stkSize++;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop() {
|
|
|
|
|
const num = this.peek();
|
|
|
|
|
this.#stackPeek = this.#stackPeek.next;
|
|
|
|
|
this.#stkSize--;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
peek() {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (!this.#stackPeek) throw new Error('栈为空');
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return this.#stackPeek.val;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="linkedlist_stack.ts"
|
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
private stackPeek: ListNode | null; // 将头节点作为栈顶
|
2023-03-03 02:46:12 +08:00
|
|
|
|
private stkSize: number = 0; // 栈的长度
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
constructor() {
|
|
|
|
|
this.stackPeek = null;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size(): number {
|
|
|
|
|
return this.stkSize;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
isEmpty(): boolean {
|
|
|
|
|
return this.size == 0;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num: number): void {
|
|
|
|
|
const node = new ListNode(num);
|
|
|
|
|
node.next = this.stackPeek;
|
|
|
|
|
this.stackPeek = node;
|
|
|
|
|
this.stkSize++;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop(): number {
|
|
|
|
|
const num = this.peek();
|
2023-03-03 02:46:12 +08:00
|
|
|
|
if (!this.stackPeek) throw new Error('栈为空');
|
2023-02-08 15:20:18 +08:00
|
|
|
|
this.stackPeek = this.stackPeek.next;
|
|
|
|
|
this.stkSize--;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
peek(): number {
|
2023-03-03 02:46:12 +08:00
|
|
|
|
if (!this.stackPeek) throw new Error('栈为空');
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return this.stackPeek.val;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 将链表转化为 Array 并返回 */
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linkedlist_stack.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
struct linkedListStack {
|
|
|
|
|
ListNode *top; // 将头节点作为栈顶
|
|
|
|
|
int size; // 栈的长度
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-22 01:35:51 +08:00
|
|
|
|
typedef struct linkedListStack linkedListStack;
|
|
|
|
|
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 构造函数 */
|
|
|
|
|
linkedListStack *newLinkedListStack() {
|
|
|
|
|
linkedListStack *s = malloc(sizeof(linkedListStack));
|
|
|
|
|
s->top = NULL;
|
|
|
|
|
s->size = 0;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 析构函数 */
|
|
|
|
|
void delLinkedListStack(linkedListStack *s) {
|
|
|
|
|
while (s->top) {
|
|
|
|
|
ListNode *n = s->top->next;
|
|
|
|
|
free(s->top);
|
|
|
|
|
s->top = n;
|
|
|
|
|
}
|
|
|
|
|
free(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size(linkedListStack *s) {
|
|
|
|
|
assert(s);
|
|
|
|
|
return s->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool isEmpty(linkedListStack *s) {
|
|
|
|
|
assert(s);
|
|
|
|
|
return size(s) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int peek(linkedListStack *s) {
|
|
|
|
|
assert(s);
|
|
|
|
|
assert(size(s) != 0);
|
|
|
|
|
return s->top->val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(linkedListStack *s, int num) {
|
|
|
|
|
assert(s);
|
|
|
|
|
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
|
|
|
|
|
node->next = s->top; // 更新新加节点指针域
|
|
|
|
|
node->val = num; // 更新新加节点数据域
|
|
|
|
|
s->top = node; // 更新栈顶
|
|
|
|
|
s->size++; // 更新栈大小
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
int pop(linkedListStack *s) {
|
|
|
|
|
if (s->size == 0) {
|
|
|
|
|
printf("stack is empty.\n");
|
|
|
|
|
return INT_MAX;
|
|
|
|
|
}
|
|
|
|
|
assert(s);
|
|
|
|
|
int val = peek(s);
|
|
|
|
|
ListNode *tmp = s->top;
|
|
|
|
|
s->top = s->top->next;
|
|
|
|
|
// 释放内存
|
|
|
|
|
free(tmp);
|
|
|
|
|
s->size--;
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linkedlist_stack.cs"
|
|
|
|
|
/* 基于链表实现的栈 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
class LinkedListStack {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
private ListNode? stackPeek; // 将头节点作为栈顶
|
2023-02-08 15:20:18 +08:00
|
|
|
|
private int stkSize = 0; // 栈的长度
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public LinkedListStack() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
stackPeek = null;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int size() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return stkSize;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public bool isEmpty() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public void push(int num) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
ListNode node = new ListNode(num);
|
|
|
|
|
node.next = stackPeek;
|
|
|
|
|
stackPeek = node;
|
|
|
|
|
stkSize++;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int pop() {
|
2023-02-08 22:16:25 +08:00
|
|
|
|
if (stackPeek == null)
|
|
|
|
|
throw new Exception();
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int num = peek();
|
2023-02-08 22:16:25 +08:00
|
|
|
|
stackPeek = stackPeek.next;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
stkSize--;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int peek() {
|
2023-02-08 22:16:25 +08:00
|
|
|
|
if (size() == 0 || stackPeek == null)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
throw new Exception();
|
|
|
|
|
return stackPeek.val;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int[] toArray() {
|
2023-02-08 22:16:25 +08:00
|
|
|
|
if (stackPeek == null)
|
|
|
|
|
return Array.Empty<int>();
|
|
|
|
|
|
|
|
|
|
ListNode node = stackPeek;
|
|
|
|
|
int[] res = new int[size()];
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = res.Length - 1; i >= 0; i--) {
|
2023-02-08 22:16:25 +08:00
|
|
|
|
res[i] = node.val;
|
|
|
|
|
node = node.next;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linkedlist_stack.swift"
|
|
|
|
|
/* 基于链表实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
private var _peek: ListNode? // 将头节点作为栈顶
|
2023-02-08 15:20:18 +08:00
|
|
|
|
private var _size = 0 // 栈的长度
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
init() {}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
func size() -> Int {
|
|
|
|
|
_size
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
func isEmpty() -> Bool {
|
|
|
|
|
size() == 0
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
func push(num: Int) {
|
|
|
|
|
let node = ListNode(x: num)
|
|
|
|
|
node.next = _peek
|
|
|
|
|
_peek = node
|
|
|
|
|
_size += 1
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
@discardableResult
|
|
|
|
|
func pop() -> Int {
|
|
|
|
|
let num = peek()
|
|
|
|
|
_peek = _peek?.next
|
|
|
|
|
_size -= 1
|
|
|
|
|
return num
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
func peek() -> Int {
|
|
|
|
|
if isEmpty() {
|
|
|
|
|
fatalError("栈为空")
|
|
|
|
|
}
|
|
|
|
|
return _peek!.val
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
|
|
|
|
/* 将 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-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linkedlist_stack.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 基于链表实现的栈
|
|
|
|
|
fn LinkedListStack(comptime T: type) type {
|
|
|
|
|
return struct {
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
stack_top: ?*inc.ListNode(T) = null, // 将头节点作为栈顶
|
2023-03-13 22:40:57 +08:00
|
|
|
|
stk_size: usize = 0, // 栈的长度
|
2023-02-09 22:55:29 +08:00
|
|
|
|
mem_arena: ?std.heap.ArenaAllocator = null,
|
2023-06-25 21:11:35 +08:00
|
|
|
|
mem_allocator: std.mem.Allocator = undefined, // 内存分配器
|
2023-02-09 22:55:29 +08:00
|
|
|
|
|
2023-06-25 21:11:35 +08:00
|
|
|
|
// 构造函数(分配内存+初始化栈)
|
2023-02-09 22:55:29 +08:00
|
|
|
|
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
|
|
|
|
if (self.mem_arena == null) {
|
|
|
|
|
self.mem_arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
|
self.mem_allocator = self.mem_arena.?.allocator();
|
|
|
|
|
}
|
2023-03-13 22:40:57 +08:00
|
|
|
|
self.stack_top = null;
|
|
|
|
|
self.stk_size = 0;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 21:11:35 +08:00
|
|
|
|
// 析构函数(释放内存)
|
2023-02-09 22:55:29 +08:00
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
|
if (self.mem_arena == null) return;
|
|
|
|
|
self.mem_arena.?.deinit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取栈的长度
|
|
|
|
|
pub fn size(self: *Self) usize {
|
2023-03-13 22:40:57 +08:00
|
|
|
|
return self.stk_size;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 判断栈是否为空
|
|
|
|
|
pub fn isEmpty(self: *Self) bool {
|
|
|
|
|
return self.size() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 访问栈顶元素
|
2023-03-13 22:40:57 +08:00
|
|
|
|
pub fn peek(self: *Self) T {
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (self.size() == 0) @panic("栈为空");
|
2023-03-13 22:40:57 +08:00
|
|
|
|
return self.stack_top.?.val;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 入栈
|
|
|
|
|
pub fn push(self: *Self, num: T) !void {
|
|
|
|
|
var node = try self.mem_allocator.create(inc.ListNode(T));
|
|
|
|
|
node.init(num);
|
2023-03-13 22:40:57 +08:00
|
|
|
|
node.next = self.stack_top;
|
|
|
|
|
self.stack_top = node;
|
|
|
|
|
self.stk_size += 1;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 出栈
|
|
|
|
|
pub fn pop(self: *Self) T {
|
2023-03-13 22:40:57 +08:00
|
|
|
|
var num = self.peek();
|
|
|
|
|
self.stack_top = self.stack_top.?.next;
|
|
|
|
|
self.stk_size -= 1;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将栈转换为数组
|
|
|
|
|
pub fn toArray(self: *Self) ![]T {
|
2023-03-13 22:40:57 +08:00
|
|
|
|
var node = self.stack_top;
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var res = try self.mem_allocator.alloc(T, self.size());
|
2023-06-25 21:11:35 +08:00
|
|
|
|
@memset(res, @as(T, 0));
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var i: usize = 0;
|
|
|
|
|
while (i < res.len) : (i += 1) {
|
|
|
|
|
res[res.len - i - 1] = node.?.val;
|
|
|
|
|
node = node.?.next;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="linkedlist_stack.dart"
|
|
|
|
|
/* 基于链表类实现的栈 */
|
|
|
|
|
class LinkedListStack {
|
|
|
|
|
ListNode? _stackPeek; // 将头节点作为栈顶
|
|
|
|
|
int _stkSize = 0; // 栈的长度
|
|
|
|
|
|
|
|
|
|
LinkedListStack() {
|
|
|
|
|
_stackPeek = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size() {
|
|
|
|
|
return _stkSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool isEmpty() {
|
|
|
|
|
return _stkSize == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(int num) {
|
|
|
|
|
final ListNode node = ListNode(num);
|
|
|
|
|
node.next = _stackPeek;
|
|
|
|
|
_stackPeek = node;
|
|
|
|
|
_stkSize++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
int pop() {
|
|
|
|
|
final int num = peek();
|
|
|
|
|
_stackPeek = _stackPeek!.next;
|
|
|
|
|
_stkSize--;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int peek() {
|
|
|
|
|
if (_stackPeek == null) {
|
|
|
|
|
throw Exception("栈为空");
|
|
|
|
|
}
|
|
|
|
|
return _stackPeek!.val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将链表转化为 List 并返回 */
|
|
|
|
|
List<int> toList() {
|
|
|
|
|
ListNode? node = _stackPeek;
|
|
|
|
|
List<int> list = [];
|
|
|
|
|
while (node != null) {
|
|
|
|
|
list.add(node.val);
|
|
|
|
|
node = node.next;
|
|
|
|
|
}
|
|
|
|
|
list = list.reversed.toList();
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
### 基于数组的实现
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
在基于「数组」实现栈时,我们可以将数组的尾部作为栈顶。在这样的设计下,入栈与出栈操作就分别对应在数组尾部添加元素与删除元素,时间复杂度都为 $O(1)$ 。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "ArrayStack"
|
2023-02-26 19:22:52 +08:00
|
|
|
|
![基于数组实现栈的入栈出栈操作](stack.assets/array_stack.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "push()"
|
|
|
|
|
![array_stack_push](stack.assets/array_stack_push.png)
|
|
|
|
|
|
|
|
|
|
=== "pop()"
|
|
|
|
|
![array_stack_pop](stack.assets/array_stack_pop.png)
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
由于入栈的元素可能会源源不断地增加,因此我们可以使用动态数组,这样就无需自行处理数组扩容问题。以下为示例代码。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array_stack.java"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
private ArrayList<Integer> stack;
|
2023-04-14 04:01:38 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
public ArrayStack() {
|
|
|
|
|
// 初始化列表(动态数组)
|
|
|
|
|
stack = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
public int size() {
|
|
|
|
|
return stack.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
public void push(int num) {
|
|
|
|
|
stack.add(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
public int pop() {
|
|
|
|
|
if (isEmpty())
|
2023-04-23 14:58:03 +08:00
|
|
|
|
throw new IndexOutOfBoundsException();
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return stack.remove(size() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
public int peek() {
|
|
|
|
|
if (isEmpty())
|
2023-04-23 14:58:03 +08:00
|
|
|
|
throw new IndexOutOfBoundsException();
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return stack.get(size() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
|
|
|
|
public Object[] toArray() {
|
|
|
|
|
return stack.toArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array_stack.cpp"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
private:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
vector<int> stack;
|
2023-04-14 04:01:38 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size() {
|
|
|
|
|
return stack.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool empty() {
|
|
|
|
|
return stack.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(int num) {
|
|
|
|
|
stack.push_back(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
void pop() {
|
|
|
|
|
int oldTop = top();
|
|
|
|
|
stack.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int top() {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (empty())
|
2023-02-08 15:20:18 +08:00
|
|
|
|
throw out_of_range("栈为空");
|
|
|
|
|
return stack.back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 返回 Vector */
|
|
|
|
|
vector<int> toVector() {
|
|
|
|
|
return stack;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array_stack.py"
|
|
|
|
|
class ArrayStack:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""基于数组实现的栈"""
|
|
|
|
|
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def __init__(self):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""构造方法"""
|
2023-03-23 18:56:56 +08:00
|
|
|
|
self.__stack: list[int] = []
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def size(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""获取栈的长度"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return len(self.__stack)
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def is_empty(self) -> bool:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""判断栈是否为空"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return self.__stack == []
|
|
|
|
|
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def push(self, item: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""入栈"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
self.__stack.append(item)
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def pop(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""出栈"""
|
2023-04-23 14:58:03 +08:00
|
|
|
|
if self.is_empty():
|
|
|
|
|
raise IndexError("栈为空")
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return self.__stack.pop()
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def peek(self) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""访问栈顶元素"""
|
2023-04-23 14:58:03 +08:00
|
|
|
|
if self.is_empty():
|
|
|
|
|
raise IndexError("栈为空")
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return self.__stack[-1]
|
2023-04-09 05:12:22 +08:00
|
|
|
|
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def to_list(self) -> list[int]:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""返回列表用于打印"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return self.__stack
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array_stack.go"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
type arrayStack struct {
|
|
|
|
|
data []int // 数据
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 初始化栈 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func newArrayStack() *arrayStack {
|
|
|
|
|
return &arrayStack{
|
|
|
|
|
// 设置栈的长度为 0,容量为 16
|
|
|
|
|
data: make([]int, 0, 16),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 栈的长度 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *arrayStack) size() int {
|
|
|
|
|
return len(s.data)
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 栈是否为空 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *arrayStack) isEmpty() bool {
|
|
|
|
|
return s.size() == 0
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *arrayStack) push(v int) {
|
|
|
|
|
// 切片会自动扩容
|
|
|
|
|
s.data = append(s.data, v)
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *arrayStack) pop() any {
|
|
|
|
|
val := s.peek()
|
|
|
|
|
s.data = s.data[:len(s.data)-1]
|
|
|
|
|
return val
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 获取栈顶元素 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (s *arrayStack) peek() any {
|
|
|
|
|
if s.isEmpty() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
val := s.data[len(s.data)-1]
|
|
|
|
|
return val
|
|
|
|
|
}
|
2023-02-09 04:43:12 +08:00
|
|
|
|
|
|
|
|
|
/* 获取 Slice 用于打印 */
|
|
|
|
|
func (s *arrayStack) toSlice() []int {
|
|
|
|
|
return s.data
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="array_stack.js"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
2023-03-03 02:46:12 +08:00
|
|
|
|
#stack;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
constructor() {
|
2023-03-03 02:46:12 +08:00
|
|
|
|
this.#stack = [];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-17 21:57:42 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size() {
|
2023-03-03 02:46:12 +08:00
|
|
|
|
return this.#stack.length;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
empty() {
|
2023-03-03 02:46:12 +08:00
|
|
|
|
return this.#stack.length === 0;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num) {
|
2023-03-03 02:46:12 +08:00
|
|
|
|
this.#stack.push(num);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop() {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (this.empty()) throw new Error('栈为空');
|
2023-03-03 02:46:12 +08:00
|
|
|
|
return this.#stack.pop();
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
top() {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (this.empty()) throw new Error('栈为空');
|
2023-03-03 02:46:12 +08:00
|
|
|
|
return this.#stack[this.#stack.length - 1];
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
|
|
|
|
/* 返回 Array */
|
|
|
|
|
toArray() {
|
2023-03-03 02:46:12 +08:00
|
|
|
|
return this.#stack;
|
2023-02-08 16:47:52 +08:00
|
|
|
|
}
|
2023-04-17 21:57:42 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array_stack.ts"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
private stack: number[];
|
|
|
|
|
constructor() {
|
|
|
|
|
this.stack = [];
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
get size(): number {
|
|
|
|
|
return this.stack.length;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
empty(): boolean {
|
|
|
|
|
return this.stack.length === 0;
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
push(num: number): void {
|
|
|
|
|
this.stack.push(num);
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
pop(): number | undefined {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (this.empty()) throw new Error('栈为空');
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return this.stack.pop();
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
top(): number | undefined {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (this.empty()) throw new Error('栈为空');
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return this.stack[this.stack.length - 1];
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
|
|
|
|
|
/* 返回 Array */
|
|
|
|
|
toArray() {
|
|
|
|
|
return this.stack;
|
|
|
|
|
}
|
2023-04-17 21:57:42 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array_stack.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
struct arrayStack {
|
|
|
|
|
int *data;
|
|
|
|
|
int size;
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-22 01:35:51 +08:00
|
|
|
|
typedef struct arrayStack arrayStack;
|
|
|
|
|
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 构造函数 */
|
|
|
|
|
arrayStack *newArrayStack() {
|
|
|
|
|
arrayStack *s = malloc(sizeof(arrayStack));
|
|
|
|
|
// 初始化一个大容量,避免扩容
|
|
|
|
|
s->data = malloc(sizeof(int) * MAX_SIZE);
|
|
|
|
|
s->size = 0;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size(arrayStack *s) {
|
|
|
|
|
return s->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool isEmpty(arrayStack *s) {
|
|
|
|
|
return s->size == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(arrayStack *s, int num) {
|
|
|
|
|
if (s->size == MAX_SIZE) {
|
|
|
|
|
printf("stack is full.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
s->data[s->size] = num;
|
|
|
|
|
s->size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int peek(arrayStack *s) {
|
|
|
|
|
if (s->size == 0) {
|
|
|
|
|
printf("stack is empty.\n");
|
|
|
|
|
return INT_MAX;
|
|
|
|
|
}
|
|
|
|
|
return s->data[s->size - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
int pop(arrayStack *s) {
|
|
|
|
|
if (s->size == 0) {
|
|
|
|
|
printf("stack is empty.\n");
|
|
|
|
|
return INT_MAX;
|
|
|
|
|
}
|
|
|
|
|
int val = peek(s);
|
|
|
|
|
s->size--;
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array_stack.cs"
|
|
|
|
|
/* 基于数组实现的栈 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
class ArrayStack {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
private List<int> stack;
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public ArrayStack() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 初始化列表(动态数组)
|
|
|
|
|
stack = new();
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int size() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return stack.Count();
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public bool isEmpty() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return size() == 0;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public void push(int num) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
stack.Add(num);
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int pop() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new Exception();
|
|
|
|
|
var val = peek();
|
|
|
|
|
stack.RemoveAt(size() - 1);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int peek() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (isEmpty())
|
|
|
|
|
throw new Exception();
|
|
|
|
|
return stack[size() - 1];
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
public int[] toArray() {
|
2023-02-08 22:16:25 +08:00
|
|
|
|
return stack.ToArray();
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array_stack.swift"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
private var stack: [Int]
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
init() {
|
|
|
|
|
// 初始化列表(动态数组)
|
|
|
|
|
stack = []
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
func size() -> Int {
|
|
|
|
|
stack.count
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
func isEmpty() -> Bool {
|
|
|
|
|
stack.isEmpty
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 入栈 */
|
|
|
|
|
func push(num: Int) {
|
|
|
|
|
stack.append(num)
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 出栈 */
|
|
|
|
|
@discardableResult
|
|
|
|
|
func pop() -> Int {
|
|
|
|
|
if isEmpty() {
|
|
|
|
|
fatalError("栈为空")
|
|
|
|
|
}
|
|
|
|
|
return stack.removeLast()
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
func peek() -> Int {
|
|
|
|
|
if isEmpty() {
|
|
|
|
|
fatalError("栈为空")
|
|
|
|
|
}
|
|
|
|
|
return stack.last!
|
|
|
|
|
}
|
2023-02-08 20:30:14 +08:00
|
|
|
|
|
|
|
|
|
/* 将 List 转化为 Array 并返回 */
|
|
|
|
|
func toArray() -> [Int] {
|
|
|
|
|
stack
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="array_stack.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 基于数组实现的栈
|
|
|
|
|
fn ArrayStack(comptime T: type) type {
|
|
|
|
|
return struct {
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
|
|
stack: ?std.ArrayList(T) = null,
|
|
|
|
|
|
2023-02-10 01:03:37 +08:00
|
|
|
|
// 构造方法(分配内存+初始化栈)
|
2023-02-09 22:55:29 +08:00
|
|
|
|
pub fn init(self: *Self, allocator: std.mem.Allocator) void {
|
|
|
|
|
if (self.stack == null) {
|
|
|
|
|
self.stack = std.ArrayList(T).init(allocator);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 01:03:37 +08:00
|
|
|
|
// 析构方法(释放内存)
|
2023-02-09 22:55:29 +08:00
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
|
if (self.stack == null) return;
|
|
|
|
|
self.stack.?.deinit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取栈的长度
|
|
|
|
|
pub fn size(self: *Self) usize {
|
|
|
|
|
return self.stack.?.items.len;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 判断栈是否为空
|
|
|
|
|
pub fn isEmpty(self: *Self) bool {
|
|
|
|
|
return self.size() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 访问栈顶元素
|
|
|
|
|
pub fn peek(self: *Self) T {
|
|
|
|
|
if (self.isEmpty()) @panic("栈为空");
|
|
|
|
|
return self.stack.?.items[self.size() - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 入栈
|
|
|
|
|
pub fn push(self: *Self, num: T) !void {
|
|
|
|
|
try self.stack.?.append(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 出栈
|
|
|
|
|
pub fn pop(self: *Self) T {
|
|
|
|
|
var num = self.stack.?.pop();
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回 ArrayList
|
|
|
|
|
pub fn toList(self: *Self) std.ArrayList(T) {
|
|
|
|
|
return self.stack.?;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="array_stack.dart"
|
|
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
|
class ArrayStack {
|
|
|
|
|
late List<int> _stack;
|
|
|
|
|
ArrayStack() {
|
|
|
|
|
_stack = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
|
int size() {
|
|
|
|
|
return _stack.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断栈是否为空 */
|
|
|
|
|
bool isEmpty() {
|
|
|
|
|
return _stack.isEmpty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 入栈 */
|
|
|
|
|
void push(int num) {
|
|
|
|
|
_stack.add(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 出栈 */
|
|
|
|
|
int pop() {
|
|
|
|
|
if (isEmpty()) {
|
|
|
|
|
throw Exception("栈为空");
|
|
|
|
|
}
|
|
|
|
|
return _stack.removeLast();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
|
int peek() {
|
|
|
|
|
if (isEmpty()) {
|
|
|
|
|
throw Exception("栈为空");
|
|
|
|
|
}
|
|
|
|
|
return _stack.last;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 将栈转化为 Array 并返回 */
|
|
|
|
|
List<int> toArray() => _stack;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 5.1.3. 两种实现对比
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
### 支持操作
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
两种实现都支持栈定义中的各项操作。数组实现额外支持随机访问,但这已超出了栈的定义范畴,因此一般不会用到。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
### 时间效率
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
在基于数组的实现中,入栈和出栈操作都是在预先分配好的连续内存中进行,具有很好的缓存本地性,因此效率较高。然而,如果入栈时超出数组容量,会触发扩容机制,导致该次入栈操作的时间复杂度变为 $O(n)$ 。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
在链表实现中,链表的扩容非常灵活,不存在上述数组扩容时效率降低的问题。但是,入栈操作需要初始化节点对象并修改指针,因此效率相对较低。不过,如果入栈元素本身就是节点对象,那么可以省去初始化步骤,从而提高效率。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
综上所述,当入栈与出栈操作的元素是基本数据类型(如 `int` , `double` )时,我们可以得出以下结论:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-07-26 08:58:52 +08:00
|
|
|
|
- 基于数组实现的栈在触发扩容时效率会降低,但由于扩容是低频操作,因此平均效率更高。
|
|
|
|
|
- 基于链表实现的栈可以提供更加稳定的效率表现。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
### 空间效率
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
在初始化列表时,系统会为列表分配“初始容量”,该容量可能超过实际需求。并且,扩容机制通常是按照特定倍率(例如 2 倍)进行扩容,扩容后的容量也可能超出实际需求。因此,**基于数组实现的栈可能造成一定的空间浪费**。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
然而,由于链表节点需要额外存储指针,**因此链表节点占用的空间相对较大**。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
综上,我们不能简单地确定哪种实现更加节省内存,需要针对具体情况进行分析。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 5.1.4. 栈典型应用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
- **浏览器中的后退与前进、软件中的撤销与反撤销**。每当我们打开新的网页,浏览器就会将上一个网页执行入栈,这样我们就可以通过「后退」操作回到上一页面。后退操作实际上是在执行出栈。如果要同时支持后退和前进,那么需要两个栈来配合实现。
|
|
|
|
|
- **程序内存管理**。每次调用函数时,系统都会在栈顶添加一个栈帧,用于记录函数的上下文信息。在递归函数中,向下递推阶段会不断执行入栈操作,而向上回溯阶段则会执行出栈操作。
|