python没有像C++那样的struct 来实现栈 和队列,但是可以用deque来实现栈和队列,当然,栈也可以用最简单的list来实现。
实现 Stacks
- 用lists实现Stacks
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
print(stack) #output : [3, 4, 5, 6, 7]
stack.pop() #output : 7
print(stack) #output : [3, 4, 5, 6]
- 用deque