用两个栈实现队列
class MyQueue {
public:
stack<int> stack1;
stack<int> stack2;
MyQueue() {
// do intialization if necessary
stack1 = stack<int>();
stack2 = stack<int>();
}
void push(int element) {
// write your code here
stack1.push(element);
}
int pop() {
// write your code here
int pop_elem = top();
stack2.pop();
return pop_elem;
}
int top() {
// write your code here
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
}
return stack2.top();
}
};
优质内容筛选与推荐>>