232.implement-queue-using-stacks 用栈实现队列
作者:互联网
当stOut
为空时,将stIn
中所有元素push
到stOut
#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