Package trial.gaurav.collection
Class LinkedList<T>
java.lang.Object
trial.gaurav.collection.LinkedList<T>
- Type Parameters:
T- The type of elements stored in the linked list.
A simple implementation of a singly linked list.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassInner class representing a node in the linked list. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(Comparator comparator) Checks if the linked list contains an element that satisfies the given comparator.current()Retrieves the data from the current node.voidfirst()Sets the current node to the first node in the list.getFirst()Gets the first element of the linked list.getLast()Gets the last element of the linked list.booleanhasNext()Checks if there is a next node in the list.voidInserts a new element at the end of the linked list.booleanisEmpty()Checks if the linked list is empty.voidlast()Sets the current node to the last node in the list.voidnext()Sets the current node to the next node in the list.voidRemoves the first occurrence of the specified item from the linked list.intsize()Gets the size of the linked list.
-
Field Details
-
head
-
first
-
last
-
current
-
size
private int size
-
-
Constructor Details
-
LinkedList
public LinkedList()Constructs an empty linked list.
-
-
Method Details
-
getFirst
Gets the first element of the linked list.- Returns:
- The first element or null if the list is empty.
-
getLast
Gets the last element of the linked list.- Returns:
- The last element or null if the list is empty.
-
first
public void first()Sets the current node to the first node in the list. -
last
public void last()Sets the current node to the last node in the list. -
current
Retrieves the data from the current node.- Returns:
- The data value stored in the current node.
-
insert
Inserts a new element at the end of the linked list.- Parameters:
value- The value to be inserted.
-
size
public int size()Gets the size of the linked list.- Returns:
- The number of elements in the linked list.
-
isEmpty
public boolean isEmpty()Checks if the linked list is empty.- Returns:
- True if the linked list is empty, otherwise false.
-
next
public void next()Sets the current node to the next node in the list. -
hasNext
public boolean hasNext()Checks if there is a next node in the list.- Returns:
- True if there is a next node, otherwise false.
-
remove
Removes the first occurrence of the specified item from the linked list.- Parameters:
item- The item to be removed.
-
contains
Checks if the linked list contains an element that satisfies the given comparator.- Parameters:
comparator- The comparator to use for the check.- Returns:
- True if the comparator's condition is met, otherwise false.
-