mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Reverts #2034, #2035, #2036 and #2037. The work will return in a single
reviewed PR that is completed before it merges. The tree equals 8c96e2f30
(#2033).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
/**
|
|
* File: stack.cs
|
|
* Created Time: 2022-12-23
|
|
* Author: haptear ([email protected])
|
|
*/
|
|
|
|
namespace hello_algo.chapter_stack_and_queue;
|
|
|
|
public class stack {
|
|
[Test]
|
|
public void Test() {
|
|
/* 初始化栈 */
|
|
Stack<int> stack = new();
|
|
|
|
/* 元素入栈 */
|
|
stack.Push(1);
|
|
stack.Push(3);
|
|
stack.Push(2);
|
|
stack.Push(5);
|
|
stack.Push(4);
|
|
// 请注意,stack.ToArray() 得到的是倒序序列,即索引 0 为栈顶
|
|
Console.WriteLine("栈 stack = " + string.Join(",", stack));
|
|
|
|
/* 访问栈顶元素 */
|
|
int peek = stack.Peek();
|
|
Console.WriteLine("栈顶元素 peek = " + peek);
|
|
|
|
/* 元素出栈 */
|
|
int pop = stack.Pop();
|
|
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack));
|
|
|
|
/* 获取栈的长度 */
|
|
int size = stack.Count;
|
|
Console.WriteLine("栈的长度 size = " + size);
|
|
|
|
/* 判断是否为空 */
|
|
bool isEmpty = stack.Count == 0;
|
|
Console.WriteLine("栈是否为空 = " + isEmpty);
|
|
}
|
|
}
|