Files
hello-algo/ja/codes/python/chapter_stack_and_queue/stack.py
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

37 lines
989 B
Python

"""
File: stack.py
Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com)
"""
"""Driver Code"""
if __name__ == "__main__":
# スタックを初期化する
# Python には組み込みのスタッククラスがないため、list をスタックとして使える
stack: list[int] = []
# 要素をプッシュ
stack.append(1)
stack.append(3)
stack.append(2)
stack.append(5)
stack.append(4)
print("スタック stack =", stack)
# スタックトップの要素にアクセス
peek: int = stack[-1]
print("スタックトップ要素 peek =", peek)
# 要素をポップ
pop: int = stack.pop()
print("ポップした要素 pop =", pop)
print("ポップ後 stack =", stack)
# スタックの長さを取得
size: int = len(stack)
print("スタックの長さ size =", size)
# 空かどうかを判定
is_empty: bool = len(stack) == 0
print("スタックが空かどうか =", is_empty)