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

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

时间:2022-08-15 21:33:09浏览次数:68  
标签:stIn int pop queue 用栈 stOut push using empty

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
From: https://www.cnblogs.com/zwyyy456/p/16589696.html

相关文章