主要思想就是 压入栈1的元素在输出到栈2中,元素的排列就成了队列了
class MyQueue {
stack<int > inStk,outStk;
void fun() {
while(!inStk.empty()) {
outStk.push(inStk.top());
inStk.pop();
}
}
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
inStk.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(outStk.empty())fun();
int t = outStk.top();
outStk.pop();
return t;
}
/** Get the front element. */
int peek() {
if(outStk.empty())fun();
return outStk.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return inStk.empty()&&outStk.empty();
}
};