Class LinkedList<T>

java.lang.Object
com.Roukan.datastructures.list.LinkedList<T>
Type Parameters:
T - the type of elements stored in this list

public class LinkedList<T> extends Object
A singly linked list implementation that provides various operations for adding, removing, and accessing elements in a linear sequence.

This implementation maintains a head reference and tracks the size of the list for efficient operations.

Since:
2024
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty linked list.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(T value)
    Appends the specified element to the end of this list.
    void
    Removes all of the elements from this list.
    boolean
    contains(T value)
    Returns true if this list contains the specified element.
    get(int index)
    Returns the node at the specified position in this list.
    Returns the first node in this list.
    Returns the last node in this list.
    int
    indexOf(T value)
    Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    void
    insert(T value, int index)
    Inserts the specified element at the specified position in this list.
    boolean
    Checks if the linked list is empty.
    int
    lastIndexOf(T value)
    Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
    void
    prepend(T value)
    Inserts the specified element at the beginning of this list.
    void
    remove(int index)
    Removes the element at the specified position in this list.
    void
    Removes and returns the first element from this list.
    void
    Removes and returns the last element from this list.
    int
    Returns the number of elements in this list.
    Returns a string representation of this list.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • LinkedList

      public LinkedList()
      Constructs an empty linked list. The head is initialized to null and size to zero.
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Checks if the linked list is empty.
      Returns:
      true if the list contains no elements, false otherwise
    • append

      public void append(T value)
      Appends the specified element to the end of this list.
      Parameters:
      value - the element to be appended to this list
    • prepend

      public void prepend(T value)
      Inserts the specified element at the beginning of this list.
      Parameters:
      value - the element to be inserted at the beginning
    • insert

      public void insert(T value, int index) throws IndexOutOfBoundsException
      Inserts the specified element at the specified position in this list.
      Parameters:
      value - the element to be inserted
      index - the index at which the specified element is to be inserted
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index invalid input: '<' 0 || index > size)
    • removeFirst

      public void removeFirst() throws IllegalArgumentException
      Removes and returns the first element from this list.
      Throws:
      IllegalArgumentException - if the list is empty
    • removeLast

      public void removeLast() throws IllegalArgumentException
      Removes and returns the last element from this list.
      Throws:
      IllegalArgumentException - if the list is empty
    • remove

      public void remove(int index) throws IllegalArgumentException, IndexOutOfBoundsException
      Removes the element at the specified position in this list.
      Parameters:
      index - the index of the element to be removed
      Throws:
      IllegalArgumentException - if the list is empty
      IndexOutOfBoundsException - if the index is out of range (index invalid input: '<' 0 || index >= size)
    • get

      public Node<T> get(int index) throws IllegalArgumentException, IndexOutOfBoundsException
      Returns the node at the specified position in this list.
      Parameters:
      index - the index of the node to return
      Returns:
      the node at the specified position in this list
      Throws:
      IllegalArgumentException - if the list is empty
      IndexOutOfBoundsException - if the index is out of range (index invalid input: '<' 0 || index >= size)
    • getFirst

      public Node<T> getFirst() throws IllegalArgumentException
      Returns the first node in this list.
      Returns:
      the first node in this list
      Throws:
      IllegalArgumentException - if the list is empty
    • getLast

      public Node<T> getLast()
      Returns the last node in this list.
      Returns:
      the last node in this list, or null if the list is empty
    • indexOf

      public int indexOf(T value)
      Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
      Parameters:
      value - the element to search for
      Returns:
      the index of the first occurrence of the specified element, or -1 if this list does not contain the element
    • lastIndexOf

      public int lastIndexOf(T value)
      Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
      Parameters:
      value - the element to search for
      Returns:
      the index of the last occurrence of the specified element, or -1 if this list does not contain the element
    • contains

      public boolean contains(T value)
      Returns true if this list contains the specified element.
      Parameters:
      value - the element whose presence in this list is to be tested
      Returns:
      true if this list contains the specified element
    • clear

      public void clear()
      Removes all of the elements from this list. The list will be empty after this call returns.
    • toString

      public String toString()
      Returns a string representation of this list. The string representation consists of a list of the list's elements in the order they are stored, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).
      Overrides:
      toString in class Object
      Returns:
      a string representation of this list
    • size

      public int size()
      Returns the number of elements in this list.
      Returns:
      the number of elements in this list