Package com.Roukan.datastructures.list
Class DoublyLinkedList<T>
java.lang.Object
com.Roukan.datastructures.list.DoublyLinkedList<T>
- Type Parameters:
T
- the type of elements stored in this list
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 -
Method Summary
Modifier and TypeMethodDescriptionvoid
Appends the specified element to the end of this list.get
(int index) Returns the node at the specified position in this list.int
Returns the index of the first occurrence of the specified element in this list.void
Inserts the specified element at the specified position in this list.boolean
isEmpty()
Returns true if this list contains no elements.void
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
size()
Returns the number of elements in this list.toString()
Returns a string representation of this list.
-
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
Appends the specified element to the end of this list.- Parameters:
value
- the element to be appended to this list
-
get
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
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
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 insertedvalue
- the element to be inserted- Throws:
IllegalArgumentException
- if the index is out of range (index invalid input: '<' 0 || index > size)
-
prepend
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
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
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).
-