https://leetcode.com/problems/implement-queue-using-stacks/description/
class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> st, tmp;
int front_val=0;
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
if(st.empty()){
st.push(x);
front_val = x;
}
else st.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
while (st.size()>1) {
tmp.push(st.top());
st.pop();
}
int remove_val = st.top();
st.pop(); //删除元素
if(!tmp.empty()) front_val = tmp.top();//判断是否非空
while (tmp.size()>0) {
st.push(tmp.top());
tmp.pop();
}
return remove_val;
}
/** Get the front element. */
int peek() {
return front_val;
}
/** Returns whether the queue is empty. */
bool empty() {
return st.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/