题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
class Stack: def __init__(self) -> None: self.items = [] def push(self,item): self.items.append(item) def pop(self): return self.items.pop() def top(self): return self.items[len(self.items)-1] if __name__ == '__main__': n = int(input()) stack = Stack() for i in range(n): input_str = input().split() if input_str[0] == 'push': stack.push(int(input_str[1])) if input_str[0] == 'pop': if not stack.items: print('error') else: print(stack.pop()) if input_str[0] == 'top': if not stack.items: print('error') else: print(stack.top())#数据结构和算法##栈#