判断栈是否为空
class Stack():
def __init__(self):
self.stack=[]
def is_empty(self):
return len(self.stack)==0
取出栈顶元素
def ReturnTop(self):
if not self.is_empty():
return self.stack[len(self.stack-1)]
出栈
def Pop(self):
if len(self.stack)>0:
return self.stack.pop()
else:
print("栈已经为空")
return None
压栈
def Push(self,date):
self.stack.append(date)
遍历栈
def Travel(self):
if self.is_empty():
print("栈为空,无法遍历")
else:
for i in range(len(self.stack)):
print(self.stack[i],end=" ")
求栈的长度
def Length(self):
return len(self.stack)