题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
n = int(input()) # 输入操作次数 stack = [] # 栈 for _ in range(n): op = input().split() # 分割操作字符串 if op[0] == "push": stack.append(int(op[1])) # 入栈操作 elif op[0] == "pop": if not stack: print("error") else: print(stack.pop()) # 出栈操作 elif op[0] == "top": if not stack: print("error") else: print(stack[-1]) # 输出栈顶元素