Files
hello-algo/ja/codes/python/chapter_stack_and_queue/stack.py
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

37 lines
1014 B
Python

"""
File: stack.py
Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com)
"""
"""実行用コード"""
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)