class MyQueue {
public:
stack
stack
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 ret = this->pop();//使用已有的pop函数,弹出栈顶元素
stOut.push(ret);//再将弹出的元素压入stOut栈中
return ret;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
};