Class DoublyLinkedList<T>

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

public class DoublyLinkedList<T> extends Object
A doubly linked list implementation that provides bidirectional traversal and efficient operations at both ends of the list.

This implementation maintains references to both head and tail nodes, allowing for optimized operations at both ends of the list.

Since:
2024
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    append(T value)
    Appends the specified element to the end of this list.
    get(int index)
    Returns the node at the specified position in this list.
    int
    index(T value)
    Returns the index of the first occurrence of the specified element in this list.
    void
    insert(int index, T value)
    Inserts the specified element at the specified position in this list.
    boolean
    Returns true if this list contains no elements.
    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.
    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

    • DoublyLinkedList

      public DoublyLinkedList()
      Constructs an empty doubly linked list. The head and last references are initialized to null and size to zero.
  • Method Details

    • 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
    • get

      public DoublyNode<T> get(int index) throws IllegalArgumentException
      Returns the node at the specified position in this list. This method uses an optimization strategy by traversing from the head if the index is in the first half of the list, or from the tail if the index is in the second half.
      Parameters:
      index - the index of the node to return
      Returns:
      the node at the specified position in this list
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index invalid input: '<' 0 || index >= size)
      IllegalArgumentException
    • index

      public int index(T value) throws IllegalArgumentException
      Returns the index of the first occurrence of the specified element in this list.
      Parameters:
      value - the element to search for
      Returns:
      the index of the first occurrence of the specified element
      Throws:
      IllegalArgumentException - if the list is empty or the value is null, or if the element is not found in the list
    • insert

      public void insert(int index, T value) throws IllegalArgumentException
      Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
      Parameters:
      index - the index at which the specified element is to be inserted
      value - the element to be inserted
      Throws:
      IllegalArgumentException - if the index is out of range (index invalid input: '<' 0 || index > size)
    • prepend

      public void prepend(T value) throws IllegalArgumentException
      Inserts the specified element at the beginning of this list.
      Parameters:
      value - the element to be inserted at the beginning
      Throws:
      IllegalArgumentException - if the list state is invalid for prepend operation
    • remove

      public void remove(int index) throws IllegalArgumentException
      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 or the index is out of range
    • size

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

      public boolean isEmpty()
      Returns true if this list contains no elements.
      Returns:
      true if this list contains no elements
    • 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