python两个队列生成一个栈
class TwoQueueOneStack(object):
def __init__(self):
self.queue1 = []
self.queue2 = []
def push(self,item):
self.queue1.append(item)
def pop(self):
if len(self.queue1)==0:
return None
while len(self.queue1)!=1:
self.queue2.append(self.queue1.pop(0))
self.queue2,self.queue1 = self.queue1,self.queue2
return self.queue2.pop(0)
stack = TwoQueueOneStack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
print(stack.pop())
print(stack.pop())
输出:5
输出:4