2022-12-23 18:28:17 +08:00
|
|
|
/**
|
|
|
|
* File: array_stack.cs
|
|
|
|
* Created Time: 2022-12-23
|
|
|
|
* Author: haptear (haptear@hotmail.com)
|
|
|
|
*/
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
namespace hello_algo.chapter_stack_and_queue;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 基于数组实现的栈 */
|
2023-04-23 03:03:12 +08:00
|
|
|
class ArrayStack {
|
2023-10-08 01:33:46 +08:00
|
|
|
private readonly List<int> stack;
|
2023-04-23 03:03:12 +08:00
|
|
|
public ArrayStack() {
|
2023-02-08 22:18:02 +08:00
|
|
|
// 初始化列表(动态数组)
|
|
|
|
stack = new();
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 获取栈的长度 */
|
2023-10-08 01:33:46 +08:00
|
|
|
public int Size() {
|
|
|
|
return stack.Count;
|
2023-02-08 22:18:02 +08:00
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 判断栈是否为空 */
|
2023-10-08 01:33:46 +08:00
|
|
|
public bool IsEmpty() {
|
|
|
|
return Size() == 0;
|
2023-02-08 22:18:02 +08:00
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 入栈 */
|
2023-10-08 01:33:46 +08:00
|
|
|
public void Push(int num) {
|
2023-02-08 22:18:02 +08:00
|
|
|
stack.Add(num);
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 出栈 */
|
2023-10-08 01:33:46 +08:00
|
|
|
public int Pop() {
|
|
|
|
if (IsEmpty())
|
2023-02-08 22:18:02 +08:00
|
|
|
throw new Exception();
|
2023-10-08 01:33:46 +08:00
|
|
|
var val = Peek();
|
|
|
|
stack.RemoveAt(Size() - 1);
|
2023-02-08 22:18:02 +08:00
|
|
|
return val;
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 访问栈顶元素 */
|
2023-10-08 01:33:46 +08:00
|
|
|
public int Peek() {
|
|
|
|
if (IsEmpty())
|
2023-02-08 22:18:02 +08:00
|
|
|
throw new Exception();
|
2023-10-08 01:33:46 +08:00
|
|
|
return stack[Size() - 1];
|
2023-02-08 22:18:02 +08:00
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 将 List 转化为 Array 并返回 */
|
2023-10-08 01:33:46 +08:00
|
|
|
public int[] ToArray() {
|
2023-02-08 22:18:02 +08:00
|
|
|
return stack.ToArray();
|
2022-12-23 15:42:02 +08:00
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-04-23 03:03:12 +08:00
|
|
|
public class array_stack {
|
2023-02-08 22:18:02 +08:00
|
|
|
[Test]
|
2023-04-23 03:03:12 +08:00
|
|
|
public void Test() {
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 初始化栈 */
|
2023-10-08 01:33:46 +08:00
|
|
|
ArrayStack stack = new();
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 元素入栈 */
|
2023-10-08 01:33:46 +08:00
|
|
|
stack.Push(1);
|
|
|
|
stack.Push(3);
|
|
|
|
stack.Push(2);
|
|
|
|
stack.Push(5);
|
|
|
|
stack.Push(4);
|
|
|
|
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 访问栈顶元素 */
|
2023-10-08 01:33:46 +08:00
|
|
|
int peek = stack.Peek();
|
2023-02-08 22:18:02 +08:00
|
|
|
Console.WriteLine("栈顶元素 peek = " + peek);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 元素出栈 */
|
2023-10-08 01:33:46 +08:00
|
|
|
int pop = stack.Pop();
|
|
|
|
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray()));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 获取栈的长度 */
|
2023-10-08 01:33:46 +08:00
|
|
|
int size = stack.Size();
|
2023-02-08 22:18:02 +08:00
|
|
|
Console.WriteLine("栈的长度 size = " + size);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
/* 判断是否为空 */
|
2023-10-08 01:33:46 +08:00
|
|
|
bool isEmpty = stack.IsEmpty();
|
2023-02-08 22:18:02 +08:00
|
|
|
Console.WriteLine("栈是否为空 = " + isEmpty);
|
2022-12-23 15:42:02 +08:00
|
|
|
}
|
|
|
|
}
|