Norway


import java.util.;

 

// Implement using single stack

class Queue<T> {

    private Stack<T> s;

 

    // Constructor

    Queue() {

        s = new Stack<>();

    }

 

    // Enqueue an item to the queue

    public void enqueue(T ) {

        // Push item into the first stack

        s.push(data);

    }

 

    // Dequeue an item from the queue

    public T dequeue()

    {

        // if stack is empty

        if (s.isEmpty()) {

            System.out.println(20;Underflow!!21;);

            System.exit(0);

        }

 

        // pop an item from the stack

        T = s.pop();

 

        // if stack becomes empty, return the popped item

        if (s.isEmpty()) {

            return top;

        }

 

        // recur

        T item = dequeue();

 

        // push popped item back to the stack

        s.push(top);

 

        // return the result of dequeue() call

        return item;

    }

 

    public static void main(String[] args) {

        int[] keys = { 1, 2, , 4, 5 };

        Queue q = new Queue();

 

        // insert above keys into the queue

        for (int : keys) {

            q.enqueue(key);

        }

 

        System.out.println(q.dequeue());    // print 1

        System.out.println(q.dequeue());    // print 2

    }

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here