mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
954c45864b
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
72 lines
1.8 KiB
Python
72 lines
1.8 KiB
Python
"""
|
|
File: array_stack.py
|
|
Created Time: 2022-11-29
|
|
Author: Peng Chen (pengchzn@gmail.com)
|
|
"""
|
|
|
|
|
|
class ArrayStack:
|
|
"""配列ベースのスタッククラス"""
|
|
|
|
def __init__(self):
|
|
"""コンストラクタ"""
|
|
self._stack: list[int] = []
|
|
|
|
def size(self) -> int:
|
|
"""スタックの長さを取得"""
|
|
return len(self._stack)
|
|
|
|
def is_empty(self) -> bool:
|
|
"""スタックが空かどうかを判定"""
|
|
return self.size() == 0
|
|
|
|
def push(self, item: int):
|
|
"""プッシュ"""
|
|
self._stack.append(item)
|
|
|
|
def pop(self) -> int:
|
|
"""ポップ"""
|
|
if self.is_empty():
|
|
raise IndexError("Stack is empty")
|
|
return self._stack.pop()
|
|
|
|
def peek(self) -> int:
|
|
"""スタックトップ要素にアクセス"""
|
|
if self.is_empty():
|
|
raise IndexError("Stack is empty")
|
|
return self._stack[-1]
|
|
|
|
def to_list(self) -> list[int]:
|
|
"""出力用の配列を返す"""
|
|
return self._stack
|
|
|
|
|
|
"""Driver Code"""
|
|
if __name__ == "__main__":
|
|
# スタックを初期化
|
|
stack = ArrayStack()
|
|
|
|
# 要素をプッシュ
|
|
stack.push(1)
|
|
stack.push(3)
|
|
stack.push(2)
|
|
stack.push(5)
|
|
stack.push(4)
|
|
print("スタック stack =", stack.to_list())
|
|
|
|
# スタックトップ要素にアクセス
|
|
peek: int = stack.peek()
|
|
print("スタックトップ要素 peek =", peek)
|
|
|
|
# 要素をポップ
|
|
pop: int = stack.pop()
|
|
print("ポップされた要素 pop =", pop)
|
|
print("ポップ後のスタック =", stack.to_list())
|
|
|
|
# スタックの長さを取得
|
|
size: int = stack.size()
|
|
print("スタックの長さ size =", size)
|
|
|
|
# 空かどうかを判定
|
|
is_empty: bool = stack.is_empty()
|
|
print("スタックが空かどうか =", is_empty) |