题解 | #【模板】栈#

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

import sys


def push(stack: list, x):
    stack.append(x)
    return stack


def pop(stack: list):
    if(stack):
        print(stack.pop())
    else:
        print("error")
    return stack


def top(stack: list):
    if(stack):
        print(stack[-1])
    else:
        print("error") 
    return stack

if __name__ == "__main__":
    stack = []
    for line in sys.stdin:
        if line.startswith("push "):
            stack = push(stack, int(line.replace("push ", "")))
        elif line.startswith("pop"):
            stack = pop(stack)
        elif line.startswith("top"):
            stack = top(stack)

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务