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

    1. import java.util.Stack;
    2. public class Solution {
    3. Stack<Integer> stack1 = new Stack<Integer>();
    4. Stack<Integer> stack2 = new Stack<Integer>();
    5. public void push(int node) {
    6. stack1.push(node);
    7. }
    8. public int pop() {
    9. if(stack1.empty()&&stack2.empty()){
    10. throw new RuntimeException("stack is all of empty");
    11. }
    12. if(stack2.empty()){
    13. while(!stack1.empty()){
    14. stack2.push(stack1.pop());
    15. }
    16. }
    17. return stack2.pop();
    18. }
    19. }