I have implemented the Stack using two Queues q1 and q2 respectively as shown below in the code. I would like to get you reviews on whether it's an efficient way of implementing it or now. For Pop operation :I will keep on removing elements from the Queue q1 and adding it to Queue 2 until only one element remains in Queue q1, then I will be removing that particular element from the Queue q1 to accomplish the Stack Pop operationFor Push Operation: It's simple, I am just checking if Queue q1 is empty then I will start adding the elements in Queue q2 otherwise in queue q1.import java.util.LinkedList;import java.util.Queue;public class StackUsingQueues {    Queue<Integer> q1 = new LinkedList<>();    Queue<Integer> q2 = new LinkedList<>();    public  void push(int data){        if(q1.isEmpty()){            q2.add(data);        }        else{            q1.add(data);        }    }    public int pop(){        int x;        if(q1.isEmpty()){                    if(q2.isEmpty()){                        System.out.println(Stack Underflow);                        System.exit(0);                    } else {                            /* I will keep on removing elements from the Queue q1 and adding                               it to Queue 2 until only one element remains in Queue q1, then                                I will be removing that particular element from the Queue q1 to                                accomplish the Stack Pop operation */                                     while(q1.size()!=1){                                    x = q1.remove();                                    q2.add(x);                                    }                                 return q1.remove();                          }}                        else {                                while(q2.size()!=1){                                    x = q2.remove();                                    q1.add(x);                                    }                                    return q2.remove();                            }        return 0;        }    public static void main(String[] args) {        StackUsingQueues st = new StackUsingQueues();        st.push(1);        st.push(2);    }}
Stack using two queues
java;stack;queue
BrokenYour implementation doesn't actually work.(The question should have been closed, but it already has an answer, so... too late.)The two st.push calls will append to q2If you call pop after that, the algorithm tries to remove elements from q1 until its size becomes 1, but its size is 0 now, to that will throw a NoSuchElementException=> You cannot pop from this stack, it's brokenMissing methodsA sorely missing method is isEmpty (or empty).Without such method,there's no really easy way to explore the elements of a stack.Basically keep popping until the program crashes? Not very ergonomic.Avoid System.exitSystem.exit doesn't belong in the middle of an algorithm.In fact it's best to avoid it altogether.It's extremely rare when using this is the right way to go in Java.Popping an empty stackJava has an exception dedicated to popping an empty stack,called EmptyStackException.How could you know that?When reinventing the wheel,it's good to look at the SDK (Stack in particular, in this case),to learn from what exists.ImplementationSuggested implementation, using your algorithm idea:import java.util.EmptyStackException;import java.util.LinkedList;import java.util.Queue;public class StackUsingQueues<T> {    private Queue<T> q1 = new LinkedList<>();    private Queue<T> q2 = new LinkedList<>();    public void push(T data) {        q1.add(data);    }    public T pop() {        if (isEmpty()) {            throw new EmptyStackException();        }        while (q1.size() > 1) {            q2.add(q1.poll());        }        T top = q1.poll();        Queue<T> temp = q1;        q1 = q2;        q2 = temp;        return top;    }    public boolean isEmpty() {        return q1.isEmpty();    }}Note that this algorithm is fast for pushes but slow for pops.The opposite is also possible: fast for pops but slow for pushes.For that alternative approach, see this answer on Stack Overflow.