[剑指OFFER] 用两个栈实现队列

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
 
用stack导两次就变成顺序了,
来了数据都放在stack1中,输出时,stack2中有数据直接输出,没有数据,从stack1中导入数据到stack2 中
 
class Solution
{
    public:
        void push(int node) {
            stack1.push(node);
        }
 
        int pop() {
            if(stack2.empty())
            {
                while(!stack1.empty())
                {
                    int tmp = stack1.top();
                    stack1.pop();
                    stack2.push(tmp);
                }
            }
 
            if(!stack2.empty())
            {
                int tmp = stack2.top();
                stack2.pop();
                return tmp;
            }
            else
                return INT_MAX;//error
 
        }
 
    private:
        stack<int> stack1;
        stack<int> stack2;
};
原文地址:https://www.cnblogs.com/diegodu/p/4571636.html