python--两个栈实现一个队列

class TwoStackOneQueue(object):
    def __init__(self):
        self.stack1 = []
        self.stack2 = []

    def push(self,item):
        self.stack1.append(item)
    def pop(self):
        if self.stack2:
            return self.stack2.pop()
        else:
            if self.stack1:
                while self.stack1:
                    self.stack2.append(self.stack1.pop())
                return self.stack2.pop()
            else:
                return None
a = TwoStackOneQueue()
a.push(1)
a.push(2)
a.push(3)
a.push(4)
print(a.pop())

 

你可能感兴趣的:(面试题,python)