@d1g1tal/collections
Version:
JavaScript Collections ES Modules
178 lines (176 loc) • 6.81 kB
TypeScript
type LinkedListType = (typeof LinkedList.Type)[keyof typeof LinkedList.Type];
/** JavaScript implementation of a LinkedList */
declare class LinkedList<E> {
private $head;
private $tail;
private $size;
private $doublyLinked;
static Type: {
readonly Singly: "singly";
readonly Doubly: "doubly";
};
/**
* Creates a new LinkedList.
* @param type The type of the list ('singly' or 'doubly' linked).
*/
constructor(type?: LinkedListType);
/**
* Adds an element to the start of the list.
* @param value The element to add.
*/
addFirst(value: E): void;
/**
* Adds an element to the end of the list.
* @param value The element to add.
*/
addLast(value: E): void;
/**
* Gets the first node in the list.
* @returns The first node in the list, or null if the list is empty.
*/
getFirst(): E | null;
/**
* Gets the last node in the list.
* @returns The last node in the list, or null if the list is empty.
*/
getLast(): E | null;
/**
* Removes the first element from the list.
* @returns The removed element, or null if the list was empty.
*/
removeFirst(): E | null;
/**
* Removes the last element from the list.
* @returns The removed element, or null if the list was empty.
*/
removeLast(): E | null;
/**
* Removes the first occurrence of an element from the list.
* @param value The element to remove.
* @returns The removed element, or null if the element was not found.
*/
remove(value: E): E | null;
/**
* Gets the value of the node at the specified index.
* @param index The index of the element to remove.
* @returns The removed element, or null if the index was out of bounds.
*/
get(index: number): E | null;
/**
* Sets the value of the node at the specified index.
* Replaces the value of the node at the specified index with the specified value.
* @param index The index of the element to set.
* @param value The new value of the element.
* @throws {RangeError} If the index is out of bounds.
*/
set(index: number, value: E): void;
/**
* Inserts an element at the specified index.
* Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
* If the index is equal to the size of the list, the element is added to the end of the list.
* If the index is 0, the element is added to the start of the list.
* Otherwise, the element is inserted at the specified index.
* @param index The index at which to insert the element.
* @param value The element to insert.
* @throws {RangeError} If the index is out of bounds.
*/
insert(index: number, value: E): void;
/**
* Checks if the list contains the specified element.
* @param value The element to check for.
* @returns True if the list contains the element, false otherwise.
*/
contains(value: E): boolean;
/**
* Reverses the list.
* The first element becomes the last, and the last element becomes the first.
* This method runs in linear time.
*/
reverse(): void;
/**
* Removes all elements from the list.
* The list will be empty after this call returns.
* This method runs in linear time.
*/
clear(): void;
/**
* Checks if the list is empty.
* @returns True if the list is empty, false otherwise.
*/
isEmpty(): boolean;
/**
* Gets the index of the value in the list.
* If the value is not found, -1 is returned.
* If the value is found multiple times, the index of the first occurrence is returned.
* This method runs in linear time.
* @param value The value to search for.
* @returns The index of the value in the list, or -1 if the value is not found.
*/
indexOf(value: E): number;
/**
* Iterates over the list and calls the specified consumer function for each element.
* The consumer function is called with three arguments: the value of the element, the index of the element, and the list itself.
* @param consumer The consumer function to call for each element.
* @param [context] The context to call the consumer function in.
*/
forEach(consumer: (arg0: E, arg1: number, arg2: LinkedList<E>) => void, context?: object): void;
/**
* Returns an iterator over the values in the list.
* The values are returned in order from the first to the last element.
* This method runs in constant time.
* The returned iterator is fail-fast.
* Modifying the list after getting the iterator, except through the iterator's own methods, will throw an error.
* @yields {Generator<E, void, unknown>} An iterator over the values in the list.
*/
values(): Generator<E, void, unknown>;
/**
* Gets the size of the list.
* @returns The size of the list.
*/
get size(): number;
/**
* Gets an array containing all the values in the list.
* The values are returned in order from the first to the last element.
* This method runs in linear time.
* The returned array is a shallow copy of the list.
* Modifying the array will not modify the list.
* @returns An array containing all the values in the list.
*/
toArray(): E[];
/**
* Returns an iterator over the values in the list.
* The values are returned in order from the first to the last element.
* This method runs in constant time.
* The returned iterator is fail-fast.
* Modifying the list after getting the iterator, except through the iterator's own methods, will throw an error.
* The iterator does not support modifying the list during iteration.
* This method is called when the list is used in a for-of loop.
* @yields {Iterable<E>} An iterator over the values in the list.
* @example
* ````js
* for (const value of list) {
* console.log(value);
* }
* ````
*/
[Symbol.iterator](): Generator<E, void, unknown>;
/**
* Returns a string description of the list.
* @returns A string description of the list.
*/
get [Symbol.toStringTag](): string;
/**
* Gets the node at the specified index.
* If the index is out of bounds, null is returned.
* @param index The index of the node to get.
* @returns The node at the specified index, or null if the index is out of bounds.
*/
private getNodeAt;
/**
* Removes a node from the list.
* @param node The node to remove.
* @returns The value of the removed node.
*/
private removeNode;
}
export { LinkedList };