09-用两个栈实现队列

题目1:用两个栈实现一个队列

class Stack2Queue(object):
    def __init__(self):
        self.stack1 = []
        self.stack2 = []
    def push(self,var):
        self.stack1.append(var)
    def pop(self):
        if len(self.stack2)>0:
            return self.stack2.pop()
        else:
            while len(self.stack1)>0:
                self.stack2.append(self.stack1.pop())
            return self.stack2.pop()
    def empty(self):
        if len(self.stack2)==0 and len(self.stack1)==0:
            return True
        else:
            return False

注:

一个栈用来模拟入队,另一个栈模拟出队。如果栈2为空,从栈1将数据移到栈2。栈1和栈2全为空表示队列为空。

原文地址:https://www.cnblogs.com/kingshine007/p/11342433.html