2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
File: array_stack.py
|
2022-11-29 21:42:59 +08:00
|
|
|
Created Time: 2022-11-29
|
|
|
|
Author: Peng Chen (pengchzn@gmail.com)
|
2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2022-11-29 13:58:23 +08:00
|
|
|
class ArrayStack:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""基于数组实现的栈"""
|
|
|
|
|
2023-07-24 22:34:05 +08:00
|
|
|
def __init__(self):
|
2023-04-09 05:05:35 +08:00
|
|
|
"""构造方法"""
|
2023-10-14 21:54:47 +08:00
|
|
|
self._stack: list[int] = []
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def size(self) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""获取栈的长度"""
|
2023-10-14 21:54:47 +08:00
|
|
|
return len(self._stack)
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def is_empty(self) -> bool:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""判断栈是否为空"""
|
2024-04-22 02:26:32 +08:00
|
|
|
return self.size() == 0
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-07-24 22:34:05 +08:00
|
|
|
def push(self, item: int):
|
2023-04-09 05:05:35 +08:00
|
|
|
"""入栈"""
|
2023-10-14 21:54:47 +08:00
|
|
|
self._stack.append(item)
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def pop(self) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""出栈"""
|
2023-04-23 03:41:39 +08:00
|
|
|
if self.is_empty():
|
|
|
|
raise IndexError("栈为空")
|
2023-10-14 21:54:47 +08:00
|
|
|
return self._stack.pop()
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def peek(self) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""访问栈顶元素"""
|
2023-04-23 03:41:39 +08:00
|
|
|
if self.is_empty():
|
|
|
|
raise IndexError("栈为空")
|
2023-10-14 21:54:47 +08:00
|
|
|
return self._stack[-1]
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def to_list(self) -> list[int]:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""返回列表用于打印"""
|
2023-10-14 21:54:47 +08:00
|
|
|
return self._stack
|
2022-11-29 13:58:23 +08:00
|
|
|
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
"""Driver Code"""
|
2022-11-29 13:58:23 +08:00
|
|
|
if __name__ == "__main__":
|
2023-04-09 05:05:35 +08:00
|
|
|
# 初始化栈
|
2022-11-29 13:58:23 +08:00
|
|
|
stack = ArrayStack()
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
# 元素入栈
|
2022-11-29 13:58:23 +08:00
|
|
|
stack.push(1)
|
|
|
|
stack.push(3)
|
|
|
|
stack.push(2)
|
|
|
|
stack.push(5)
|
|
|
|
stack.push(4)
|
2022-11-30 02:27:26 +08:00
|
|
|
print("栈 stack =", stack.to_list())
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
# 访问栈顶元素
|
2023-03-12 18:49:52 +08:00
|
|
|
peek: int = stack.peek()
|
2022-11-29 23:35:51 +08:00
|
|
|
print("栈顶元素 peek =", peek)
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
# 元素出栈
|
2023-03-12 18:49:52 +08:00
|
|
|
pop: int = stack.pop()
|
2022-11-29 23:35:51 +08:00
|
|
|
print("出栈元素 pop =", pop)
|
2022-11-30 02:27:26 +08:00
|
|
|
print("出栈后 stack =", stack.to_list())
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
# 获取栈的长度
|
2023-03-12 18:49:52 +08:00
|
|
|
size: int = stack.size()
|
2022-11-29 23:35:51 +08:00
|
|
|
print("栈的长度 size =", size)
|
2022-11-29 13:58:23 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
# 判断是否为空
|
2023-03-12 18:49:52 +08:00
|
|
|
is_empty: bool = stack.is_empty()
|
2022-12-01 06:50:25 +08:00
|
|
|
print("栈是否为空 =", is_empty)
|