俩个栈模拟队列

#include <iostream>
#include <stack>
#include <exception>
using namespace std;
class TwoQue
{
public:
    void add(int num)
    {
        stackPush.push(num);
    }
    int poll()
    {
        if(stackPop.empty() && stackPush.empty())
            throw new exception();
        else if(stackPop.empty())
            {
                while(!stackPush.empty())
                {
                    int val = stackPush.top();
                    stackPop.push(val);
                    stackPush.pop();
                }
            }
        int val = stackPop.top();
        stackPop.pop();
        return val;
    }

    int top()
    {
        if(stackPop.empty() && stackPush.empty())
            throw new exception();
        else if(stackPop.empty())
            {
                while(!stackPush.empty())
                {
                    int val = stackPush.top();
                    stackPop.push(val);
                    stackPush.pop();
                }
            }
        return stackPop.top();
    }
private:
    stack<int> stackPush;
    stack<int> stackPop;
};

猜你喜欢

转载自blog.csdn.net/wzc2608/article/details/80563930