1import java.util.*;
2public class LinkedListExample {
3 public static void main(String args[]) {
4
5 /* Linked List Declaration */
6 LinkedList<String> linkedlist = new LinkedList<String>();
7
8 /*add(String Element) is used for adding
9 * the elements to the linked list*/
10 linkedlist.add("Item1");
11 linkedlist.add("Item5");
12 linkedlist.add("Item3");
13 linkedlist.add("Item6");
14 linkedlist.add("Item2");
15
16 /*Display Linked List Content*/
17 System.out.println("Linked List Content: " +linkedlist);
18
19 /*Add First and Last Element*/
20 linkedlist.addFirst("First Item");
21 linkedlist.addLast("Last Item");
22 System.out.println("LinkedList Content after addition: " +linkedlist);
23
24 /*This is how to get and set Values*/
25 Object firstvar = linkedlist.get(0);
26 System.out.println("First element: " +firstvar);
27 linkedlist.set(0, "Changed first item");
28 Object firstvar2 = linkedlist.get(0);
29 System.out.println("First element after update by set method: " +firstvar2);
30
31 /*Remove first and last element*/
32 linkedlist.removeFirst();
33 linkedlist.removeLast();
34 System.out.println("LinkedList after deletion of first and last element: " +linkedlist);
35
36 /* Add to a Position and remove from a position*/
37 linkedlist.add(0, "Newly added item");
38 linkedlist.remove(2);
39 System.out.println("Final Content: " +linkedlist);
40 }
41}