代码示例
class Stack():def __init__(self, size):self.size = sizeself.stack = []self.top = -1def push(self, ele): # 入栈之前检查栈是否已满if self.isfull():raise Exception("out of range")else:self.stack.append(ele)self.top = self.top + 1def pop(self): # 出栈之前检查栈是否为空if self.isempty():raise Exception("stack is empty")else:self.top = self.top - 1return self.stack.pop()def isfull(self):return self.top + 1 == self.sizedef isempty(self):return self.top == -1