Write a Java program to create a Queue using LinkedList


  • The program starts by importing the required classes from the java.util package. Specifically, it imports the LinkedList and Queue classes.
  • Next, we define a class called CreateQueue.
  • Inside the CreateQueue class, we have the main method, which serves as the entry point of the program.
  • Within the main method, we declare a variable named queue, which is of type Queue<String>. This line creates a queue using the Queue interface, which is a part of the Java Collections Framework. Here, we use the LinkedList class to implement the Queue interface, creating a queue that stores elements of type String.
  • We then add elements to the queue using the add method. In this example, we add seven strings to the queue: "Yellow", "Green", "Pink", "Black", "Blue", "White", and "Red". The add method adds elements to the end of the queue.
  • After adding elements, we print the elements of the queue using System.out.println(). The println method will automatically call the toString method of the queue object, which will display the elements separated by commas and enclosed within square brackets.

Source Code

import java.util.LinkedList;
import java.util.Queue;
public class CreateQueue
{
	public static void main(String[] args)
	{
		Queue <String> queue = new LinkedList <> ();
 
		queue.add("Yellow");
		queue.add("Green");
		queue.add("Pink");
		queue.add("Black");
		queue.add("Blue");
		queue.add("White");
		queue.add("Red");
 
		System.out.println("Queue Elements : " + queue);
	}
}

Output

Queue Elements : [Yellow, Green, Pink, Black, Blue, White, Red]

Example Programs