hello-algo/zh-hant/codes/csharp/chapter_stack_and_queue/stack.cs

41 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: stack.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
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);
}
}