python列表模拟栈
class Stack():
def __init__(self,len):
self.stack =[]
self.len = len
def __repr__(self):#方便查看对象属性
return str(self.stack)# 需要返回字符串
def push(self,element):#压栈
self.stack.append(element)
def pop(self):#出栈
if self.stack:
self.stack.pop()
else:
return LookupError("栈已经空了")
def is_empty(self):#判断是否为空
return bool(self.stack)
def full(self):# 判断栈是否满了
if len(self.stack) == self.len :
print("栈满了")
else:
print("栈没满")
def find(self,element): #查找元素位置
return self.stack.index(element)
def peek(self):#返回栈顶元素
if self.is_empty():
return self.stack[-1]
else:
print("栈是空的")
s = Stack(4)
print(s.is_empty())
s.push(4)
s.push(6)
s.push('a')
s.full()
print(s.peek())
print(s.find('a'))
# s.pop()
print(s)
人生苦短,我用python