Write a Java program which implements LinkedList as a Queue (FIFO)
The two methods offer() and poll() makes LinkedList to work as a Queue.
import java.util.Scanner;
public class LinkedListAsQueue
{
public static void main(String[] args)
{
int i;
Scanner sc = new Scanner(System.in);
LinkedList<Integer> mylist= new LinkedList<Integer>();
while(true)
{
System.out.println("1.Insert in linked list");
System.out.println("2.Dequeue the linked list FIFO");
System.out.println("3.exit\n");
int num= sc.nextInt();
switch(num)
{
case 1:
System.out.println("Insert the number in the linked list=>");
int n=sc.nextInt();
mylist.add(n);
break;
case 2:
System.out.println("Dequeue the linked list FIFO using default method =>"+mylist.poll());
break;
case 3:
System.exit(0);
default:
System.out.println("Wrong choice\n");
}
}
}
}
No comments:
Post a Comment