Class CircularLinkedList<T>

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

public class CircularLinkedList<T> extends Object
A circular doubly linked list implementation where the last node points back to the first node, creating a circular structure that allows continuous traversal in both directions.

This implementation maintains a reference to the last node only, as the first node can be accessed via last.getNext(). This provides efficient append operations.

Since:
2024
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    append(T value)
    Appends the specified element to the end of this circular list.
    get(int index)
    Returns the node at the specified position in this circular list.
    void
    remove(int index)
    Removes the element at the specified position in this list.
    Returns a string representation of this circular list.

    Methods inherited from class java.lang.Object

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

    • CircularLinkedList

      public CircularLinkedList()
      Constructs an empty circular linked list. The last reference is initialized to null and size to zero.
  • Method Details

    • append

      public void append(T value)
      Appends the specified element to the end of this circular list. The new node becomes the last node in the circular structure.
      Parameters:
      value - the element to be appended to this list
    • remove

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

      public CircularNode<T> get(int index) throws IndexOutOfBoundsException
      Returns the node at the specified position in this circular list. Traversal starts from the first node (last.getNext()) and proceeds forward.
      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)
    • toString

      public String toString()
      Returns a string representation of this circular list. The string representation consists of a list of the list's elements in the order they appear when traversing from the first node, 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