题解 | 【模板】栈的操作

【模板】栈的操作

https://www.nowcoder.com/practice/cdf02ea916454957b575585634e5773a

def main():
    n=int(input())
    stack=[]
    for _ in range(n):
        line=input().split()
        command=line[0]

        if command=="push":
            x=int(line[1])
            stack.append(x)
        elif command=="pop":
            if not stack:#if stack==[]
                print("Empty")
            else:
                stack.pop()
        elif command=="query":
            if not stack:
                print("Empty")
            else:
                print(stack[-1])
        elif command=="size":
            print(len(stack))
    
main()

                 


全部评论

相关推荐

02-16 01:39
南昌大学 Java
重剑Ds:感觉不太可能 后端都减飞了 根本不缺人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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