Write a Java program to which implements LinkedList as a Queue (FIFO)


The LinkedListExamples class contains a main method where a LinkedList of strings, que, is created and populated with some values using the offer method. The que contains the strings "Apple", "Mango", "Pineapple", "Cherry", "Guava", "Banana", and "Watermelon".

The offer method is used to add elements to the end of the linked list, which simulates adding elements to a queue. The poll method is called on the que linked list, which removes and returns the element from the front of the list, simulating dequeuing an element from the queue. The returned elements, "Apple" and "Mango", are printed using System.out.println.

Finally, the resulting linked list after removing elements from the queue is printed using System.out.println.

Source Code

import java.util.*;
public class LinkedListExamples
{
    public static void main(String[] args)
    {
		LinkedList<String> que = new LinkedList<String>();		
		que.offer("Apple"); 
		que.offer("Mango");        
		que.offer("Pineapple");        
		que.offer("Cherry");        
		que.offer("Guava");
		que.offer("Banana");
		que.offer("Watermelon");
		System.out.println("Given LinkedList : "+que);
 
 
		System.out.println("Removing the elements from the queue .."); 
		System.out.println(que.poll()); 
		System.out.println(que.poll());
 
		System.out.println("After Removing the elements LinkedList : "+que);
    }
}

Output

Given LinkedList : [Apple, Mango, Pineapple, Cherry, Guava, Banana, Watermelon]
Removing the elements from the queue ..
Apple
Mango
After Removing the elements LinkedList : [Pineapple, Cherry, Guava, Banana, Watermelon]

Example Programs