其他分享
首页 > 其他分享> > 232.implement-queue-using-stacks 用栈实现队列

232.implement-queue-using-stacks 用栈实现队列

作者:互联网

stOut为空时,将stIn中所有元素pushstOut

#include <stack>
using std::stack;
class MyQueue {
  public:
    stack<int> stIn;
    stack<int> stOut;
    MyQueue() {
    }

    void push(int x) {
        stIn.push(x);
    }

    int pop() {
        if (stOut.empty()) {
            while (!stIn.empty()) {
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }

    int peek() {
        int res = this->pop();
        stOut.push(res);
        return res;
    }

    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

标签:stIn,int,pop,queue,用栈,stOut,push,using,empty
来源: https://www.cnblogs.com/zwyyy456/p/16589696.html