题目描述
栈的工作原理:
入栈:append
出栈: pop
栈的长度 len
栈是否为空 len ===0
代码示例
stack=[]
info="""
********栈操作******
1.入栈
2.出栈
3.栈顶元素
4.栈的长度
5.栈是否为空
"""
while True:
print(info)
choice=input("please input your choice:")
if choice=='1':
item=input('请输入入栈元素:')
stack.append(item)
print('%s入栈成功!'%item)
elif choice=='2':
if not stack:
print('栈为空,不能出栈!')
else:
item=stack.pop()
print('%s出栈成功!'%item)
elif choice=='3':
if len(stack)==0:
print('栈为空!')
else:
print('栈顶元素为:%s'%stack[-1])
elif choice=='4':
print('栈的长度为%s'%len(stack))
elif choice=='5':
if len(stack)==0:
print('栈为空!')
else:
print('栈不为空!')
elif choice=='q':
print('logout')
break
else:
print('Error:check your input!')
运行结果
9ibG9nLmNzZG4ubmV0L3FxXzQyMDA2MzU4,size_16,color_FFFFFF,t_70)