定义一个栈类(Stack),用于模拟一种具有后进先出(LIFO)特性的数据结构
class Stack:
count=0
def __init__(self,start=[]):
self.a=[]
for i in start:
self.push(i)
def isEmpty(self):
if Stack.count==0:
return True
else:
return False
def push(self,msg):
if not self.a:
print("该栈为空!")
else:
self.msg=msg
Stack.count +=1
self.a.append(self.msg)
def pop(self):
if not self.a:
print("该栈为空!")
else:
del self.a[-1]
Stack.count -=1
def top(self):
if not self.a:
print("该栈为空!")
else:
return print(self.a[-1])
def bottom(self):
if not self.a:
print("该栈为空!")
else:
return print(self.a[0])