用两个栈实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类型。
# -*-coding:utf-8 -*-
class Solution:
def __init__(self):
self.acceptStack = []
self.outputStack = []
def push(self, node):
self.acceptStack.append(node)
def pop(self):
if not self.outputStack:
while self.acceptStack:
self.outputStack.append(self.acceptStack.pop())
if self.outputStack:
return self.outputStack.pop()
else:
return None
if __name__ == '__main__':
s = Solution()
s.push(1)
s.push(2)
s.push(3)
s.push(4)
s.push(5)
print(s.pop())
print(s.pop())
s.push(6)
s.push(7)
print(s.pop())
print(s.pop())
print(s.pop())
s.push(8)
s.push(9)
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())
s.push(10)
s.push(11)
print(s.pop())
print(s.pop())
运行结果为:
1
2
3
4
5
6
7
8
9
None
10
11