题解 | List -> Stack
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf?tpId=308&tqId=2111163&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D308
Python的list既可以作为stack,也可以作为queue
# Total number of operations n = int(input()) # List can be used as a stack stack = [] while n: # [operation [number]] field = input().split() # push == append if field[0] == 'push': stack.append(field[1]) # peek == last item elif field[0] == 'top': print(stack[-1]) if stack else print('error') # pop == pop else: print(stack.pop()) if stack else print('error') # Next oeration n -= 1