Package com.Roukan.datastructures.list
Class CircularLinkedList<T>
java.lang.Object
com.Roukan.datastructures.list.CircularLinkedList<T>
- Type Parameters:
T
- the type of elements stored in this list
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 -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.toString()
Returns a string representation of this circular list.
-
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
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
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
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
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).
-