python实现栈
# 先进后出 砌墙的砖头 class Stack(): def __init__(self, size): self.size = size self.stack = [] self.top = 0 # 栈顶指针 # 入栈之前检查栈是否已满 def push(self, x): if self.isfull(): raise Exception("stack is full") else: self.stack.insert(self.top, x) self.top = self.top + 1 # 出栈之前检查栈是否为空 def pop(self): if self.isempty(): raise Exception("stack is empty") else: self.top = self.top - 1 return self.stack.pop() def isfull(self): return self.top == self.size def isempty(self): return self.top == 0 def showStack(self): return self.stack