AppDividend
Latest Code Tutorials

LinkedList in Java Example | Java LinkedList Tutorial

0

LinkedList in Java Example | Java LinkedList Tutorial is today’s topic. One most important part of being a programmer is to handle the data while working in the development process. The data structure is a fast and efficient way to store and organize data.  LinkedList is a linear data structure (like a stack), but instead of storage of data elements contiguous, each item of LinkedList is stored separately with the help of pointer. Each item is known as a node, and the node consists of two parts-data part and pointer part. Every pointer points to the address of the next node. The last node of the LinkedList points to NULL. 

LinkedList in Java

See the following figure.

 

LinkedList in Java

In Java, the LinkedList class implements the List interface, and this class uses a doubly linked list to store the data elements. Unlike a regular linked list, the doubly linked list consists of there part in each node- previous pointer, data part, and next pointer.

 

Java LinkedList Tutorial

#Class Declaration Syntax (for javautil.LinkedList)

public class LinkedList<E> 
    extends AbstractSequentialList<E>
    implements List<E>

Here, E is a data type of the data elements to be stored in the list.

LinkedList class consist of different constructors and methods like every other java class.

import java.util.*;

public class LinkedListEx {
  public static void main(String args[]) {
    LinkedList<String> ListEx = new LinkedList<String>();

    // using LinkedList constructor
    ListEx.add("1"); // adding elements
    ListEx.add("2"); // adding elements
    ListEx.addLast("3"); // adding elements
    ListEx.addFirst("4"); // adding elements
    ListEx.add(2, "5"); // adding elements in specifed position
    ListEx.add("6"); // adding elements
    ListEx.add("7"); // adding elements
    System.out.println("Linked list : " + ListEx);

  }
}

See the output.

 

Class Declaration Syntax

See another example.

import java.util.*;

class AddEx {
  public static void main(String[] args) {
    LinkedList<String> heroes = new LinkedList<String>();

    // adding elelemnts in likedlist
    heroes.add("Ironman");
    heroes.add("Captain America");
    heroes.add("Thor");
    heroes.add("Hulk");

    // showing the elelemnts of the linkedlist

    System.out.println("Elements in LinkesLists are: " + heroes);
  }
}

See the following output.

 

Java LinkedList

#Constructors for LinkedList

  1. LinkedList (): It is used to create an empty linked list.
  2. LinkedList (Collection c): It is used to generate a list containing the items of the specific collection; similarly, they are returned by the collection’s iterator.

#Method for Java LinkedList

Along with the constructors provided by the Java LinkedList class, there is also some method provided by the class.

#add​(int index, E element)

The add(i, e) method inserts the specified element at the specified position in the linked list. 

#add ​(E e)

The add(e) method Appends the specified element to the end of the list.

#addAll ​(int index, Collection c)

The addAll() method Inserts all of the elements from another list into the initial list, starting at the specified position. 

#addAll​ (Collection c):

The addAll(c) method Appends all of the elements from another list to the end of the initial list. 

#addFirst ​(E e):

The addFirst(e) method Inserts the specified element at the starting of the list. 

#addLast​ (E e):

The addLast(e) method inserts the specified element at the end of the list. 

See the following program.

import java.util.*;

public class LinkedListEx2 {
  public static void main(String args[]) {
    LinkedList<String> List1 = new LinkedList<String>();
    System.out.println("Starting list of elements: " + List1);
    List1.add("A");
    List1.add("B");
    List1.add("C");
    System.out.println("After invoking add(E e) method: " + List1);

    List1.add(1, "D"); // Adding an element at the specific position
    System.out.println("After invoking add(int index, E element) method: " + List1);
    LinkedList<String> List2 = new LinkedList<String>();
    List2.add("E");
    List2.add("F");
    List1.addAll(List2);// Adding second list elements to the first list

    System.out.println("After invoking addAll(collection c)  method: " + List1);
    LinkedList<String> List3 = new LinkedList<String>();
    List3.add("G");
    List3.add("H");
    List1.addAll(1, List3);// Adding second list elements to the first list at specific position
    System.out.println("After invoking addAll(int index, Collection c) method: " + List1);
    List1.addFirst("I");// Adding an element at the first position
    System.out.println("After invoking addFirst(E e) method: " + List1);
    List1.addLast("J");// Adding an element at the last position
    System.out.println("After invoking addLast(E e) method: " + List1);

  }
}

See the following output.

 

addLast​ (E e)

#clear​()

The clear() method deletes all of the elements of the list.

#contains​(Object o) 

It returns true if the linked list contains the specified element.

#get​(int index)

The get() method returns an item at the specified position of the list. 

#getFirst()

The getFirst() method returns a first item in the list. 

#getLast​()

The getLast() method returns the last item in the list. 

#indexOf​(Object o)

It returns an index of the first occurrence of the specified item in this list, and return -1 if the list does not contain the element. 

#lastIndexOf​(Object o)

It returns the index of the last occurrence of the given element in the list, and return -1 if the list does not contain the element. 

#poll​()

It retrieves and removes the initial element (first element) of this list. 

#pollFirst()

It retrieves and removes the first element of this list, or returns null if the linked list is empty.

See the following program.

import java.util.*;

public class LinkedListMethodEx {
  public static void main(String args[]) {
    LinkedList<String> List_1 = new LinkedList<String>();
    List_1.add("A");
    List_1.add("B");
    List_1.add("C");
    System.out.println("Initial list:: " + List_1);
    List_1.clear();
    System.out.println("After invoking clear:" + List_1);
    List_1.add("A");
    List_1.add("B");
    List_1.add("C");
    System.out.println("List contain B-" + List_1.contains("B"));
    System.out.println("List Contain D-" + List_1.contains("D"));
    System.out.println("Element at index 1-" + List_1.get(1));
    System.out.println("First element using getFirst()-" + List_1.getFirst());
    System.out.println("Last element using getLast()-" + List_1.getLast());
    System.out.println("Invoking indexOf(B):" + List_1.indexOf("B"));
    List_1.add("D");
    List_1.add("E");
    List_1.add("F");
    System.out.println("Invoking poll():" + List_1.poll());
    System.out.println("List after invoking poll():" + List_1);

  }
}

See the following output.

 

LinkedList Methods

#pollLast​()

The pollLast() method retrieves and deletes the last element of the linked list.

#remove​()

The remove() method retrieves and deletes the initial element (first element) of the list. 

#remove ​(int index)

Invoking this method deletes the element at the specified position in of the list. 

#remove ​(Object o)

The remove() method deletes the first occurrence of the given element from the list if it is present. 

#removeFirst​()

The removeFirst() method deletes and returns the first element of the list. 

#removeFirstOccurrence​(Object o)

Invoking this method deletes the first occurrence of the specified element in the list. 

#removeLast​()

Invoking this method deletes and returns the last element from the list. 

#set ​(int index, E element)

It replaces an item at the given position in the linked list with the given element.

#size ​()

The Size() method returns a number of items in a given list.

See the following program.

import java.util.*;

public class LinkedListFinal {
  public static void main(String args[]) {

    LinkedList<String> list = new LinkedList<String>(); // using linked list constructor

    list.add("1"); // applying add() method
    list.add("2"); // applying add() method
    list.addLast("3"); // applying add() method
    list.addFirst("4"); // applying add() method
    list.add(2, "5"); // applying add() method
    list.add("6"); // applying add() method
    list.add("7"); // applying add() method
    System.out.println("Initial list : " + list);

    list.remove("8"); // removing elements
    list.remove(3); // removing elements
    list.removeFirst();
    list.removeLast();
    System.out.println("Linked list after removing elements: " + list);

    // Finding elements in the linked list
    boolean check = list.contains("9");

    if (check)
      System.out.println("List contains the element '9' ");
    else
      System.out.println("List doesn't contain the element '9'");

    // Number of elements in the linked list
    int size = list.size();
    System.out.println("No of element in linked list = " + size);

    // Get and set elements from linked list
    Object element = list.get(3);
    System.out.println("Data element returned by get(3) : " + element);
    list.set(2, "10");
    System.out.println("Final list : " + list);
  }
}

See the following output.

 

LinkedList

Finally, LinkedList in Java Example | Java LinkedList Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.