题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
 
import java.util.Stack;
 
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
 
    public void push(int node) {
        int temp = 0;
        if (stack1.isEmpty()) {
            stack1.push(node);
            return;
        }
        while(!stack1.isEmpty()){
            temp=stack1.pop();
            stack2.push(temp);
        }
        stack1.push(node);
        while(!stack2.isEmpty()){
            temp = stack2.pop();
            stack1.push(temp);
        }
       
 
    }
 
    public int pop() {
       if(stack1.isEmpty()){
             throw new RuntimeException("");
       }
        return stack1.pop();
    }
}
posted on 2015-12-28 21:24  小飞虫子  阅读(394)  评论(0编辑  收藏  举报