题解 | #【模板】栈#

【模板】栈

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

import sys
n = int(input())
acts = []
stack = []
for i in range(n):
    acts.append(input().split())
for act in acts:
    if act[0]=='push':
        stack.append(int(act[1]))
    elif act[0]=='pop':
        if len(stack)!=0:
            print(stack.pop())
        else:
            print('error')
    elif act[0]=='top':
        if len(stack)!=0:
            print(stack[-1])
        else:
            print('error')

        
        

全部评论

相关推荐

不愿透露姓名的神秘牛友
05-25 18:29
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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