栈的python代码

This commit is contained in:
pengchzn
2022-11-29 21:42:59 +08:00
parent b47f54886e
commit f8d44be73d
3 changed files with 6 additions and 21 deletions

View File

@ -1,7 +1,7 @@
'''
File: array_stack.py
Created Time: 2022-11-25
Author: Krahets (krahets@163.com)
Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com)
'''
import os.path as osp
@ -11,43 +11,35 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
""" 基于数组实现的栈 """
class ArrayStack:
def __init__(self):
self._stack = []
self._size = 0
""" 获取栈的长度 """
def size(self):
return self._size
""" 判断栈是否为空 """
def isEmpty(self):
return self._stack == []
""" 入栈 """
def push(self, item):
self._stack.append(item)
self._size += 1
""" 出栈 """
def pop(self):
pop = self._stack.pop()
self._size -= 1
return pop
""" 访问栈顶元素 """
def peek(self):
return self._stack[-1]
""" 访问索引 index 处元素 """
def get(self, index):
return self._stack[index]