Ads Here

Saturday, June 4, 2022

Queue-Using-Two-Stacks/Solution.java

 import java.io.*;

import java.util.*;


public class Solution {


    public static void main(String[] args) {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner in = new Scanner(System.in);

        int noElements = in.nextInt();

        

        Stack<Integer> one = new Stack<Integer>();

        Stack<Integer> two = new Stack<Integer>();

        

        int command;

        

        for (int i = 0 ; i < noElements ; i++) {

            command = in.nextInt();

            

            if (command == 1)

            {

                one.push(in.nextInt());

            } 

            else if (command == 2)

            {

                if(two.isEmpty())

                {

                    while(!one.isEmpty())

                    {

                        two.push(one.pop());

                    }                    

                }

                

                if (!two.isEmpty()){                                

                    two.pop(); 

                }


            }

            else if (command == 3)

            {       

                if(two.isEmpty())

                {

                    while(!one.isEmpty())

                    {

                        two.push(one.pop());

                    }

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

                } 

                else 

                {

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

                }

            }

        }

    }

}

No comments:

Post a Comment