用两个栈实现队列

class Solution  
{  
public:  
    void push(int node) {  
        stack1.push(node);  
    }  
  
    int pop() {  
        int res = 0;  
        if(!stack2.empty())  
        {  
            res = stack2.top();  
            stack2.pop();  
            return res;  
        }  
          
        while(!stack1.empty())  
         {  
            int tmp = stack1.top();  
            stack1.pop();  
            stack2.push(tmp);  
        }  
        res = stack2.top();  
        stack2.pop();  
        return res;  
          
    }  
  
private:  
    stack<int> stack1;                //进入栈  
    stack<int> stack2;                //辅助栈  
};  

 

posted on 2017-03-01 01:23  123_123  阅读(82)  评论(0编辑  收藏  举报