Implement Queue by Two Stacks
Question
push(1)
pop() // return 1
push(2)
push(3)
top() // return 2
pop() // return 2Analysis
Solution
public class Queue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public Queue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
private void switchStack() {
while (!stack2.empty()) {
stack1.push(stack2.pop());
}
}
public void push(int element) {
stack2.push(element);
}
public int pop() {
if (stack1.empty()) {
switchStack();
}
return stack1.pop();
}
public int top() {
if (stack1.empty()) {
switchStack();
}
return stack1.peek();
}
}Reference
Last updated