Package com.Roukan.datastructures.list
Class LinkedList<T>
java.lang.Object
com.Roukan.datastructures.list.LinkedList<T>
- Type Parameters:
T
- the type of elements stored in this list
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 -
Method Summary
Modifier and TypeMethodDescriptionvoid
Appends the specified element to the end of this list.void
clear()
Removes all of the elements from this list.boolean
Returns true if this list contains the specified element.get
(int index) Returns the node at the specified position in this list.getFirst()
Returns the first node in this list.getLast()
Returns the last node in this list.int
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
Inserts the specified element at the specified position in this list.boolean
isEmpty()
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
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
size()
Returns the number of elements in this list.toString()
Returns a string representation of this list.
-
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
Appends the specified element to the end of this list.- Parameters:
value
- the element to be appended to this list
-
prepend
Inserts the specified element at the beginning of this list.- Parameters:
value
- the element to be inserted at the beginning
-
insert
Inserts the specified element at the specified position in this list.- Parameters:
value
- the element to be insertedindex
- 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
Removes and returns the first element from this list.- Throws:
IllegalArgumentException
- if the list is empty
-
removeLast
Removes and returns the last element from this list.- Throws:
IllegalArgumentException
- if the list is empty
-
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 emptyIndexOutOfBoundsException
- if the index is out of range (index invalid input: '<' 0 || index >= size)
-
get
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 emptyIndexOutOfBoundsException
- if the index is out of range (index invalid input: '<' 0 || index >= size)
-
getFirst
Returns the first node in this list.- Returns:
- the first node in this list
- Throws:
IllegalArgumentException
- if the list is empty
-
getLast
Returns the last node in this list.- Returns:
- the last node in this list, or null if the list is empty
-
indexOf
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
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
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
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). -
size
public int size()Returns the number of elements in this list.- Returns:
- the number of elements in this list
-