Implement LinkedList and perform given operations

Q. Write a program to implement LinkedList and perform different operations on it.

Answer:

In Java LinkedList class having the doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces. It can store duplicate elements in the list.

In this example of linked list, we perform add, remove and retrieve the elements from the list.

LinkedListDemo.java

import java.util.*;
public class LinkedListDemo
{
      public static void main(String args[])
      {
             // create a linked list
             LinkedList<String> ll = new LinkedList<String>();
             System.out.println("Initial size of LinkedList: "+ll.size());
     
             // add elements to the linked list
             ll.add("Pune");
             ll.add("Mumbai");
             ll.add("Delhi");
             ll.add("Noida");
             System.out.println("Element of LinkedList: " + ll);
             System.out.println("Size of LinkedList after addition: "+ll.size());

             // remove elements from the linked list
             ll.remove(2);
             System.out.println("Element of LinkedList after deletion: " + ll);
             System.out.println("Size of the LinkedList after deletion: "+ll.size());
      
             // remove first and last elements
             ll.removeFirst();
             ll.removeLast();
             System.out.println("ll after deleting first and last: " + ll);
             System.out.println("Size of the LinkedList after deletion: "+ll.size());
     
             //Retriving First and Last element
             System.out.println("Fisrt element of LinkedList: "+ll.getFirst());
             System.out.println("Last element of LinkedList: "+ll.getLast());

             Iterator<String> itr = ll.iterator();
             while(itr.hasNext())
             {
                   System.out.print(itr.next()+", ");
             }
      }
}


Output:

linked list