美文网首页
Python写队列和栈

Python写队列和栈

作者: 晨颜 | 来源:发表于2023-02-27 20:27 被阅读0次

    x # 数据结构1. 队列:先进来的先出去,先进先出2. 栈:先进来的后出来,先进后出'''使用列表的内置方法实现以上两种数据结构'''

     #队
    my_friends = ['tony', 'jason', 'tom', 4, 5, 6, 7, 88888]
    cmd="w"
    while cmd != '0' :
        print("输入指令:(1进入,2出,0为退出)")
        cmd = input().strip()
        if cmd== '1':
            print("输入添加内容")
            my_friends.append(input())
            print(my_friends)
        elif cmd== '2':
            aa = my_friends.pop(0)
            print(aa)
            print(my_friends)
        #print("成功,再来")
        elif cmd == '0':
            break
        else:
            print("错误,重来")
      
    #栈
    my_friends = ['tony', 'jason', 'tom', 4, 5, 6, 7, 88888]
    cmd="w"
    while cmd != '0' :
        print("输入指令:(1进入,2出,0为退出)")
        cmd = input().strip()
        if cmd== '1':
            print("输入添加内容")
            my_friends.append(input())
            print(my_friends)
        elif cmd== '2':
            aa = my_friends.pop()
            print(aa)
            print(my_friends)
        #print("成功,再来")
        elif cmd == '0':
            break
        else:
            print("错误,重来")
    

    相关文章

      网友评论

          本文标题:Python写队列和栈

          本文链接:https://www.haomeiwen.com/subject/ikkkldtx.html