C
LinkedList
v0.0.8testedDoubly linked list with O(1) push/pop on both ends and O(1) insert/remove by node reference
Signature
ts
class LinkedList<T> implements LinkedListLike<T>Type Parameters
TProperties
| Property | Type | Description |
|---|---|---|
lengthreadonly | number | Gets the number of elements in the list |
isEmptyreadonly | boolean | Checks if the list is empty |
headreadonly | LinkedListNode<T> | undefined | Gets the first node |
tailreadonly | LinkedListNode<T> | undefined | Gets the last node |
Methods
pushBackAppends a value to the end of the list
ts
public pushBack(value: T): LinkedListNode<T>| Parameter | Type | Description |
|---|---|---|
value | T | The value to append |
Returns
LinkedListNode<T>The created nodepushFrontPrepends a value to the beginning of the list
ts
public pushFront(value: T): LinkedListNode<T>| Parameter | Type | Description |
|---|---|---|
value | T | The value to prepend |
Returns
LinkedListNode<T>The created nodepopBackRemoves and returns the last value
ts
public popBack(): T | undefinedReturns
T | undefinedThe last value, or `undefined` if the list is emptypopFrontRemoves and returns the first value
ts
public popFront(): T | undefinedReturns
T | undefinedThe first value, or `undefined` if the list is emptypeekBackReturns the last value without removing it
ts
public peekBack(): T | undefinedReturns
T | undefinedThe last value, or `undefined` if the list is emptypeekFrontReturns the first value without removing it
ts
public peekFront(): T | undefinedReturns
T | undefinedThe first value, or `undefined` if the list is emptyinsertBeforeInserts a value before the given node
ts
public insertBefore(node: LinkedListNode<T>, value: T): LinkedListNode<T>| Parameter | Type | Description |
|---|---|---|
node | LinkedListNode<T> | The reference node |
value | T | The value to insert |
Returns
LinkedListNode<T>The created nodeinsertAfterInserts a value after the given node
ts
public insertAfter(node: LinkedListNode<T>, value: T): LinkedListNode<T>| Parameter | Type | Description |
|---|---|---|
node | LinkedListNode<T> | The reference node |
value | T | The value to insert |
Returns
LinkedListNode<T>The created noderemoveRemoves a node from the list by reference in O(1)
ts
public remove(node: LinkedListNode<T>): T| Parameter | Type | Description |
|---|---|---|
node | LinkedListNode<T> | The node to remove |
Returns
TThe value of the removed nodeclearRemoves all elements from the list
ts
public clear(): thisReturns
thisThe list instance for chainingtoArrayReturns a shallow copy of the list values as an array
ts
public toArray(): T[]Returns
T[]Array of values from head to tailtoStringReturns a string representation of the list
ts
public toString(): stringReturns
stringString representation[Symbol.iterator]Iterator over list values from head to tail
ts
public* [Symbol.iterator](): Iterator<T>Returns
Iterator<T, any, any>[Symbol.asyncIterator]Async iterator over list values from head to tail
ts
public async* [Symbol.asyncIterator](): AsyncIterator<T>Returns
AsyncIterator<T, any, any>