Question

What will be the output of the following queue implementation using two stacks? class QueueUsingStacks {      Stack s1 = new Stack ();      Stack s2 = new Stack ();        void enqueue ( int
x) {          s1.push(x);      }        int dequeue () {           if (s2.isEmpty()) {               while (!s1.isEmpty()) {                   s2.push(s1.pop());              }   }           if (!s2.isEmpty()) {               return s2.pop();          }           throw new RuntimeException ( "Queue is empty!" );      }  }    QueueUsingStacks queue = new QueueUsingStacks ();  queue.enqueue( 1 );  queue.enqueue( 2 );  queue.enqueue( 3 );  System.out.println(queue.dequeue());  queue.enqueue( 4 );  System.out.println(queue.dequeue()); 

A 1, 2
B 1, 3
C 1, 4
D 2, 3
E 2, 4
Practice Next

Hey! Ask a query