使用链表结构模拟一个栈
stack =[]
# 定义入栈方法
def push():
stack.append(int(input('请输入一个数')))
# 定义出栈方法
def pop():
if len(stack) == 0:
print("栈已经空了")
else:
print('已经出栈' % (stack.pop()))
# 浏览栈
def view():
print(stack)
# 定义菜单命令
CMDS={
'u' : push,
'o' : pop,
'v' : view
}
def menu():
menu_str="""
输入u入栈
输入o出栈
输入v浏览
输入q退出
请输入:
"""
while True:
while True:
choice = input(menu_str)
if choice not in 'uovq':
print('输入无效')
else:
break
if choice == 'q':
break
print(choice)
CMDS[choice]()
menu()
再简单的代码看过以后最好都自己实现一遍
网友评论