Dequeueefficient

main.java:

import java.util.*;
public class Main
{
public static void main(String[] args) throws Exception {

	Dequeuefficient queue= new Dequeuefficient();
	
	for(int i=0;i<5;i++)
	{
	    queue.enqueue(i*10);
	}
	
	queue.display();
}

}
dequeuefficient.java

public class Dequeuefficient
{
public Dequeuefficient()
{
System.out.println(“two stacks are used”);
}

Stack<Integer> primary= new Stack<Integer>();
Stack<Integer> secondary= new Stack<Integer>();

public void enqueue(int item) throws Exception
{
    while(!primary.isEmpty())
    {
        secondary.push(primary.pop());
    }
    primary.push(item);
    
    while(!secondary.isEmpty())
    {
        primary.push(secondary.pop());
    }
}

public int dequeue() throws Exception
{
    int x=primary.peek();
    primary.pop();
    return x;
}

public void display()
{
    return primary.display();
}

}
can we use this code

@minal.251298,
Yes you can use this code. But this code has some errors:

  1. Dequeue efficient should be private.
  2. display function is incorrect.

Corrected code: https://ide.geeksforgeeks.org/g5goF0vwYp