Pages

Monday, October 13, 2025

Queue using 2 Stacks

 import java.util.*;


class Main {

    

    static class Stack {

        static Queue<Integer> q1 = new LinkedList<>();

        static Queue<Integer> q2 = new LinkedList<>();

        

        public boolean isEmpty() {

            return q1.isEmpty() && q2.isEmpty();

        }

        

        public void push(int data) {

            if(!q1.isEmpty()) {

                q1.add(data);

            } else if(!q2.isEmpty()) {

                q2.add(data);

            } else {

                q1.add(data);

            }

        }

        

        public int pop() {

            if(isEmpty()) {

                System.out.println("empty stack");

                return -1;

            }

            int top = -1;

            

            

            // case-1

            if(!q1.isEmpty()) {

                while(!q1.isEmpty()) {

                    top = q1.remove();

                    if(!q1.isEmpty()) {

                        break;

                    }

                    q2. add(top);

                } 

            } else {

                // case 2

                while(!q2.isEmpty()) {

                    top = q2.remove();

                    if(q2.isEmpty()) {

                        break;

                    }

                    q1.add(top);

                }

            }

            return top;

        }

        

        public int peek() {

            if(isEmpty()) {

                System.out.println("empty stack");

                return -1;

            }

            int top = -1;

            

            

            // case-1

            if(!q1.isEmpty()) {

                while(!q1.isEmpty()) {

                    top = q1.remove();

                    q2. add(top);

                } 

            } else {

                // case 2

                while(!q2.isEmpty()) {

                    top = q2.remove();

                    q1.add(top);

                }

            }

            return top;

        }

    }

    

    

    public static void main(String[] args) {

    Stack s = new Stack();

    s.push(1);

    s.push(2);

    s.push(3);

    

    while(!s.isEmpty()) {

        System.out.println(s.peek());

        s.pop();

    }

        

    }

}


OUTPUT:

3

2

1


=== Code Execution Successful ===

No comments:

Post a Comment

Encode and Decode Strings - NeetCode.IO Solution by me + gemini pro 3.0 explained

  class Solution {         // encode: List of Strings -> String     public String encode ( List < String > strs) {         Stri...