Queue using Two Stacks

  • + 0 comments

    Java

    Following is a single stack solution for those who are interested

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
    
        int queries = sc.nextInt();       
        int command=0;
    
        Stack<Integer> stack = new Stack<>();
    
        while(queries>0){
            command=sc.nextInt();
            if(command==1){
                stack.push(sc.nextInt());
            }else if(command==2){
                stack.remove(0);
            }else if(command==3){
                System.out.println(stack.get(0));
            }
            queries--;
        }
    
    }