Write a Java program to append the specified element to the end of a linked list


This Java code demonstrates how to create and manipulate a linked list using the java.util.LinkedList class.

  • The code defines a class named Append_Element with a main method, which serves as the entry point for the Java program.
  • Inside the main method, a new LinkedList object named list_col is created with a type parameter of String, indicating that it will store strings as its elements.
  • Strings such as "Blue", "Orange", "Black", etc., are added to the list_col using the add method, which appends the elements to the end of the list.
  • The initial contents of the list_col are printed using a System.out.println statement, which displays the list as: "Linked list : [Blue, Orange, Black, White, Pink, Yellow, Purple]".
  • Note that the order of the elements in the linked list is preserved, as elements are added sequentially to the end of the list.
  • The code does not perform any further operations or modifications to the linked list, so the program will simply print the initial contents of the list and then terminate.
  • The output of the program will be the initial contents of the list_col linked list, which is: "Linked list : [Blue, Orange, Black, White, Pink, Yellow, Purple]".

Source Code

import java.util.LinkedList;
public class Append_Element
{
	public static void main(String[] args)
	{
		LinkedList<String> list_col = new LinkedList<String>();
		list_col.add("Blue");
		list_col.add("Orange");
		list_col.add("Black");
		list_col.add("White");
		list_col.add("Pink");
		list_col.add("Yellow");
		list_col.add("Purple");
		System.out.println("Linked list : " + list_col);
	}
}

Output

Linked list : [Blue, Orange, Black, White, Pink, Yellow, Purple]

Example Programs