Skip to content
On this page

LinkedList<T>

A linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.

Usage

typescript
import { LinkedList } from '@algoasaurujs/tsds';

Constructors

OverloadDescription
new LinkedList(): LinkedList<T>Create new LinkedList instance * // instantiate new linked list without initial set-up const list = new LinkedList(); // instantiate new linked list with initial values const list2 = new LinkedList([1, 2, 3, 4, 5]);
new LinkedList(initialValue: T[]): LinkedList<T>Create new LinkedList instance

Properties

NameDescription
firstGets the first node of the LinkedList<T>.
lastGets the last node of the LinkedList<T>.
lengthGets the number of nodes actually contained in the LinkedList<T>.

Methods

NameDescription
append(value: T | LinkedListNode<T>): voidAdds a new node or value at the end of the LinkedList<T>.
clear(): voidRemoves all nodes from the LinkedList<T>.
delete(node: LinkedListNode<T>): LinkedListNode<T>Removes the first occurrence of a node from the LinkedList<T>.
delete(value: T): LinkedListNode<T>Removes the first occurrence of the specified value from the LinkedList<T>.
deleteFirst(): LinkedListNode<T>Removes the node at the start of the LinkedList<T>.
deleteLast(): LinkedListNode<T>Removes the last node from the list.
find(value: T): null | LinkedListNode<T>Finds the first node that contains the specified value.
get(index: number): null | LinkedListNode<T>Returns Node at the specified index
includes(o: T): booleanThis implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element.
insertAfter(node: LinkedListNode<T>, newNode: T): voidAdds a new value after an existing node in the LinkedList<T>.
insertAfter(node: LinkedListNode<T>, newNode: LinkedListNode<T>): voidAdds a new node or after an existing node in the LinkedList<T>.
isEmpty(): booleanThis implementation returns length === 0.
map(callback: MapCallback<T, U>): U[]This function iterates through the linked list, and for each node in the linked list, it calls the callback function. It returns an array of values, where each value is the return value of the callback function.
prepend(value: T | LinkedListNode<T>): voidAppends new Node at the beginning of the LinkedList<T>.
toArray(): T[]This implementation returns an array containing all the elements returned by this collection's iterator, in the same order, stored in consecutive elements of the array, starting with index 0. The length of the returned array is equal to the number of elements returned by the iterator, even if the size of this collection changes during iteration, as might happen if the collection permits concurrent modification during iteration. The length property is called only as an optimization hint; the correct result is returned even if the iterator returns a different number of elements.
toArrayReverse(): T[]Returns an array of the values in the list in reverse order.