LinkedList in Java

Linked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. It also has few disadvantages like the nodes cannot be accessed directly instead we need to start from the head and follow through the link to reach a node we wish to access.
To store the elements in a linked list we use a doubly-linked list that provides a linear data structure and also used to inherit an abstract class and implement list and deque interfaces.

In Java, the LinkedList class implements the list interface. The LinkedList class also consists of various constructors and methods like other java collections.

Constructors for Java LinkedList:

  1. LinkedList(): Used to create an empty linked list.
  2. LinkedList(Collection C): Used to create an ordered list that contains all the elements of a specified collection, as returned by the collection’s iterator.
  3. // Java code for Linked List implementation

      

    import java.util.*;

      

    public class Test

    {

        public static void main(String args[])

        {

            // Creating object of class linked list

            LinkedList<String> object = new LinkedList<String>();

      

            // Adding elements to the linked list

            object.add("A");

            object.add("B");

            object.addLast("C");

            object.addFirst("D");

            object.add(2, "E");

            object.add("F");

            object.add("G");

            System.out.println("Linked list : " + object);

      

            // Removing elements from the linked list

            object.remove("B");

            object.remove(3);

            object.removeFirst();

            object.removeLast();

            System.out.println("Linked list after deletion: " + object);

      

            // Finding elements in the linked list

            boolean status = object.contains("E");

      

            if(status)

                System.out.println("List contains the element 'E' ");

            else

                System.out.println("List doesn't contain the element 'E'");

      

            // Number of elements in the linked list

            int size = object.size();

            System.out.println("Size of linked list = " + size);

      

            // Get and set elements from linked list

            Object element = object.get(2);

            System.out.println("Element returned by get() : " + element);

            object.set(2, "Y");

            System.out.println("Linked list after change : " + object);

        }

    }

    Output:

    Linked list : [D, A, E, B, C, F, G]
    Linked list after deletion: [A, E, F]
    List contains the element 'E' 
    Size of linked list = 3
    Element returned by get() : F
    Linked list after change : [A, E, Y]
    

    Methods for Java LinkedList:

  4. add? (int index, E element): This method Inserts the specified element at the specified position in this list.
  5. add? (E e): This method Appends the specified element to the end of this list.
  6. addAll?(int index, Collection c): This method Inserts all of the elements in the specified collection into this list, starting at the specified position.
  7. addAll?(Collection c): This method Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator.
  8. add first? (E e): This method Inserts the specified element at the beginning of this list.
  9. addLast?(E e): This method Appends the specified element to the end of this list.
  10. clear? (): This method removes all of the elements from this list.
  11. clone?(): This method returns a shallow copy of this LinkedList.
  12. contains? (Object o): This method returns true if this list contains the specified element.
  13. descendingIterator?(): This method returns an iterator over the elements in this deque in reverse sequential order.
  14. element?(): This method retrieves but does not remove, the head (first element) of this list.
  15. get? (int index): This method returns the element at the specified position in this list.
  16. get first? (): This method returns the first element in this list.
  17. get last? (): This method returns the last element in this list.
  18. indexOf?(Object o): This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
  19. lastIndexOf?(Object o): This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
  20. list iterator?(int index): This method returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
  21. offer? (E e): This method adds the specified element as the tail (last element) of this list.
  22. offer first?(E e): This method Inserts the specified element at the front of this list.
  23. offerLast?(E e): This method Inserts the specified element at the end of this list.
  24. peek? (): This method retrieves but does not remove, the head (first element) of this list.
  25. seek first? (): This method retrieves, but does not remove, the first element of this list, or returns null if this list is empty.
  26. Does peel last? (): This method retrieves, but does not remove, the last element of this list, or returns null if this list is empty.
  27. poll? (): This method retrieves and removes the head (first element) of this list.
  28. pollFirst?(): This method retrieves and removes the first element of this list, or returns null if this list is empty.
  29. Does a poll last? (): This method retrieves and removes the last element of this list, or returns null if this list is empty.
  30. pop?(): This method Pops an element from the stack represented by this list.
  31. push? (E e): This method Pushes an element onto the stack represented by this list.
  32. remove? (): This method retrieves and removes the head (first element) of this list.
  33. remove? (int index): This method removes the element at the specified position in this list.
  34. remove? (Object o): This method removes the first occurrence of the specified element from this list if it is present.
  35. remove first? (): This method removes and returns the first element from this list.
  36. remove the first occurrence?(Object o): This method removes the first occurrence of the specified element in this list (when traversing the list from head to tail).
  37. remove last? (): This method removes and returns the last element from this list.
  38. remove the last occurrence?(Object o): This method removes the last occurrence of the specified element in this list (when traversing the list from head to tail).
  39. set? (int index, E element): This method replaces the element at the specified position in this list with the specified element.
  40. size?(): This method returns the number of elements in this list.
  41. spliterator? (): This method Creates a late-binding and fail-fast Spliterator over the elements in this list.
  42. toArray?(): This method returns an array containing all of the elements in this list in proper sequence (from first to the last element).
  43. toArray?(T[] a): This method returns an array containing all of the elements in this list in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
  44.  
    This article is contributed by Mehak Narang.

     
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Related Articles

post a comment