Program 1
package dataflair;
import java.util.*;
public class TestQueue
{
public static void main(String[] args)
{
int n,choice=0;
Scanner scan=new Scanner(System.in);
LinkedList<Integer>myqueue=new LinkedList<Integer>();
do
{
System.out.println("\n--------------------Queue Linked List-------------------------------");
System.out.println("1.Create");
System.out.println("2.Insert");
System.out.println("3.Delete");
System.out.println("4.Display");
System.out.println("5.Search");
System.out.println("6.Exit");
System.out.println("\n---------------------------------------------------------------------");
System.out.println("Enter your choice");
choice=scan.nextInt();
switch(choice)
{
case 1:
{
//Create
String ch;
System.out.println("Enter an element");
n=scan.nextInt();
myqueue.add(n);
System.out.println("Want to continue");
ch=scan.next();
while(ch.toUpperCase().equals("YES"))
{
System.out.println("Enter next element");
n=scan.nextInt();
myqueue.add(n);
System.out.println("Want to continue");
ch=scan.next();
}
break;
}
case 2:
{
//Insert
if(myqueue.isEmpty())
System.out.println("\nQueue is empty first Create it.....");
else
{
System.out.println("\nEnter an element for insert");
n=scan.nextInt();
myqueue.addLast(n);
}
break;
}
case 3:
{
//Delete
if(myqueue.isEmpty())
System.out.println("\nQueue is empty first Create it.....");
else
{
System.out.println("\nDelete Node is: "+myqueue.removeFirst());
}
break;
}
case 4:
{
//Display
if(myqueue.isEmpty())
System.out.println("\nQueue is empty first Create it.....");
else
{
System.out.println("\nElements of Queue");
for(Integer I:myqueue)
System.out.print(" "+I);
}
break;
}
case 5:
{
//Search
if(myqueue.isEmpty())
System.out.println("\nQueue is empty first Create it.....");
else
{
System.out.println("\nEnter an element for search");
n=scan.nextInt();
if(myqueue.contains(n))
System.out.println("Searching success");
else
System.out.println("Searching not success");
}
break;
}
case 6:break;
}
}while(choice!=6);
}
}