用两个栈实现队列

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Stack;

public class {
Stack<Integer> stackPush = new Stack<Integer>();
Stack<Integer> stackPop = new Stack<Integer>();

public void push(int node) {
stackPush.push(node);
}

public int pop() {
if (stackPop.isEmpty()){
while(!stackPush.isEmpty()){
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
}