Files
hello-algo/ja/codes/csharp/chapter_stack_and_queue/stack.cs
Yudong Jin d7b2277d2b Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
2026-03-30 07:30:15 +08:00

41 lines
1.3 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);
}
}