//stack 和 queue 在add 元素上相反
public Stack<Integer> stack1 = new Stack<Integer>();
       public Stack<Integer> stack2 = new Stack<Integer>();

    public Queue() {
       // do initialization if necessary

    }

    public void push(int element) {
        // write your code here
        if(stack1.isEmpty()){
            stack1.push(element);
        }
        else{
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            stack1.push(element);
            while(!stack2.isEmpty()){
                stack1.push(stack2.pop());
            }
        }
    }

    public int pop() {
        return stack1.pop();
    }

    public int top() {
        // write your code here
       return stack1.peek();
    }

results matching ""

    No results matching ""