The type of items stored in the list
Creates a new DoubleLinkedList with optional initial items.
Initial items to add to the list (added in order)
Makes the list iterable, enabling use with for...of loops, spread operator, Array.from(), etc.
An iterator that yields each item in the list from head to tail
Adds an item to the end of the list.
The item to add
A readonly reference to the newly created node
Returns the first item in the list that satisfies the provided testing function. Similar to Array.prototype.find().
Function to test each item. Receives (value, index, list) as parameters.
The first item that matches the predicate, or undefined
if no match is found
Executes a provided function once for each item in the list. Similar to Array.prototype.forEach().
Function to execute for each item. Receives (value, index, list) as parameters.
Gets the item at the specified index (array-like access). Supports negative indices to access from the end.
The zero-based index of the item to retrieve (supports negative indices)
The item at the specified index, or undefined
if index is out of bounds
Gets the first node in the list.
The first node, or undefined
if the list is empty
Gets the node at the specified index. Supports negative indices to access from the end.
The zero-based index of the node to retrieve (supports negative indices)
The node at the specified index, or undefined
if index is out of bounds
Gets the last node in the list.
The last node, or undefined
if the list is empty
Determines whether the list includes a certain item. Similar to Array.prototype.includes().
The item to search for
The index to start the search from (default: 0)
true
if the item is found, false
otherwise
Returns the first index at which a given item can be found in the list. Similar to Array.prototype.indexOf().
The item to search for
The index to start the search from (default: 0)
The index of the first occurrence of the item, or -1 if not found
Inserts a new item immediately after the specified node.
The node after which to insert the new item
The item to insert
The newly created node, or undefined
if the reference node doesn't belong to this list
Inserts a new item immediately before the specified node.
The node before which to insert the new item
The item to insert
The newly created node, or undefined
if the reference node doesn't belong to this list
Creates a new DoubleLinkedList with the results of calling a provided function on every item. Similar to Array.prototype.map().
The type of items in the returned list
Function that produces an item of the new list. Receives (value, index, list) as parameters.
A new DoubleLinkedList with the transformed items
Removes and returns the last item from the list (stack-like operation).
The last item, or undefined
if the list is empty
Adds an item to the beginning of the list.
The item to add
A readonly reference to the newly created node
Adds an item to the end of the list (stack/array-like operation).
Alias for append()
method.
The item to add
A readonly reference to the newly created node
Removes a specific node from the list. Safely handles removal of head, tail, or middle nodes.
The node to remove
true
if the node was removed, false
if it doesn't belong to this list
Removes and returns the first item from the list (queue-like operation).
The first item, or undefined
if the list is empty
Changes the contents of the list by removing existing items and/or adding new items. Similar to Array.prototype.splice() but with enhanced node-based operations.
Zero-based index, DoubleLinkedListNode, or undefined to specify where to start changing the list. - number: Zero-based index (supports negative indices) - DoubleLinkedListNode: Start at the position of this node (O(1) lookup) - undefined: Start at the end of the list (equivalent to list.length)
Number of items to remove from the list (default: 0)
Items to add to the list, beginning from the start position
A new DoubleLinkedList containing the deleted items
const list = new DoubleLinkedList('a', 'b', 'c', 'd');
const removed = list.splice(1, 2); // Remove 2 items starting at index 1
console.log([...list]); // ['a', 'd']
console.log([...removed]); // ['b', 'c']
const list = new DoubleLinkedList('a', 'd');
const nodeA = list.getHead();
if (nodeA) {
list.splice(nodeA.nextNode, 0, 'b', 'c'); // Insert at node position
console.log([...list]); // ['a', 'b', 'c', 'd']
}
const list = new DoubleLinkedList(1, 2, 3, 4);
const secondNode = list.getHead()?.nextNode;
if (secondNode) {
const removed = list.splice(secondNode, 2, 10, 20);
console.log([...list]); // [1, 10, 20, 4]
console.log([...removed]); // [2, 3]
}
Converts the list to a readonly array. Uses internal caching for performance - the array is only rebuilt when the list is modified.
A readonly array containing all items in the list
Adds an item to the beginning of the list (array-like operation).
Alias for prepend()
method.
The item to add
A readonly reference to the newly created node
A high-performance, type-safe doubly linked list implementation for TypeScript.
Features:
Example: Basic usage
Example: Iterator support
Example: Stack operations