www

导航

两个栈实现队列

class MyQueue
{
public:
    void push(int key) 
    {
        stack1.push(key);
    }

    int pop() 
    {
        if(stack2.empty())
        {
            while(!stack1.empty())
            {
                int temp=stack1.top();
                stack1.pop();
                stack2.push(temp);
            }
        }
        if(stack2.empty())
            throw exception;
        int result=stack2.top();
        stack2.pop();
        return result;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};

 

posted on 2017-09-22 11:12  www_practice  阅读(94)  评论(0编辑  收藏  举报