主要是为了帮助理解队和栈的概念,使用栈来模式队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈一个输入栈,一个输出栈
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();
}
};