题目描述
照着《剑指offer》给的代码改的
C++ 代码
class MyQueue {
private:
stack<int> st1;
stack<int> st2;
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
st1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(st2.size() <= 0){
while(st1.size() > 0){
int& data = st1.top();
st1.pop();
st2.push(data);
}
}
int head = st2.top();
st2.pop();
return head;
}
/** Get the front element. */
int peek() {
if(st2.size() <= 0){
while(st1.size() > 0){
int& data = st1.top();
st1.pop();
st2.push(data);
}
}
return st2.top();
}
/** Returns whether the queue is empty. */
bool empty() {
bool mark = false;
if(st1.empty() && st2.empty())
mark = true;
return mark;
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/