Files
hello-algo/codes/csharp/chapter_stack_and_queue/stack.cs
T
Yudong JinandClaude Opus 5.5 849b6a95b9 Cite demo-source regions from doc snippets (pilot) (#2034)
Doc snippets for init_linked_list, init_binary_tree, stack_operations and
five list.md entries now reference marked regions of the demo programs
instead of copying code, so the book shows code that is compiled and run.

- Mark regions in the zh demo files and add the snippet comments that the
  demos lacked, reusing texts that already have reviewed translations.
- Sync ru by hand, regenerate en/ja/ko from the reviewed dictionaries and
  derive zh-hant with OpenCC.
- Convert 97 snippet blocks in six editions to [file]{..}-[region]{..}
  references; init_list and the Rust stack block stay literal until their
  comments have reviewed translations.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-16 03:36:04 +08:00

53 lines
1.4 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() {
// [region]{init_stack}
/* 初始化栈 */
Stack<int> stack = new();
// [endregion]{init_stack}
// [region]{push}
/* 元素入栈 */
stack.Push(1);
stack.Push(3);
stack.Push(2);
stack.Push(5);
stack.Push(4);
// 请注意,stack.ToArray() 得到的是倒序序列,即索引 0 为栈顶
// [endregion]{push}
Console.WriteLine("栈 stack = " + string.Join(",", stack));
// [region]{peek}
/* 访问栈顶元素 */
int peek = stack.Peek();
// [endregion]{peek}
Console.WriteLine("栈顶元素 peek = " + peek);
// [region]{pop}
/* 元素出栈 */
int pop = stack.Pop();
// [endregion]{pop}
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack));
// [region]{size}
/* 获取栈的长度 */
int size = stack.Count;
// [endregion]{size}
Console.WriteLine("栈的长度 size = " + size);
// [region]{is_empty}
/* 判断是否为空 */
bool isEmpty = stack.Count == 0;
// [endregion]{is_empty}
Console.WriteLine("栈是否为空 = " + isEmpty);
}
}