public Queue<Integer> q1 = new LinkedList<Integer>();
    public Queue<Integer> q2 = new LinkedList<Integer>();

    public void push(int x) {
        q1.add(x);
    }

    // Pop the top of the stack
    public void pop() {
        while (q1.size() > 1) {  
            q2.offer(q1.poll());  
        }  
        q1.poll();  

        Queue tmp = q1;  
        q1 = q2;  
        q2 = tmp;  
    }

    // Return the top of the stack
    public int top() {
       while (q1.size() > 1) {  
            q2.offer(q1.poll());  
        }  
        int top = q1.peek();  
        q2.offer(q1.poll());  

        Queue tmp = q1;  
        q1 = q2;  
        q2 = tmp;  

        return top;
    }

    // Check the stack is empty or not.
    public boolean isEmpty() {
        return q1.isEmpty();
    }

results matching ""

    No results matching ""