the type of each value in the list
Construct the linked list, and load its contents as follows:
undefined
valuesIf no parameters are provided, an empty list is constructed.
In the case of specifying a length, you should declare the list like this: LinkedList<TYPE | undefined>
where TYPE is the non-undefined type for your linked list elements.
Unlike Array, we don't currently support the concept of "empty slots" in linked lists.
Construct the linked list, and load its contents as follows:
undefined
valuesIf no parameters are provided, an empty list is constructed.
In the case of specifying a length, you should declare the list like this: LinkedList<TYPE | undefined>
where TYPE is the non-undefined type for your linked list elements.
Unlike Array, we don't currently support the concept of "empty slots" in linked lists.
Construct the linked list, and load its contents as follows:
undefined
valuesIf no parameters are provided, an empty list is constructed.
In the case of specifying a length, you should declare the list like this: LinkedList<TYPE | undefined>
where TYPE is the non-undefined type for your linked list elements.
Unlike Array, we don't currently support the concept of "empty slots" in linked lists.
Construct the linked list, and load its contents as follows:
undefined
valuesIf no parameters are provided, an empty list is constructed.
In the case of specifying a length, you should declare the list like this: LinkedList<TYPE | undefined>
where TYPE is the non-undefined type for your linked list elements.
Unlike Array, we don't currently support the concept of "empty slots" in linked lists.
Construct the linked list, and load its contents as follows:
undefined
valuesIf no parameters are provided, an empty list is constructed.
In the case of specifying a length, you should declare the list like this: LinkedList<TYPE | undefined>
where TYPE is the non-undefined type for your linked list elements.
Unlike Array, we don't currently support the concept of "empty slots" in linked lists.
Get the length of the list. The length is maintained internally, so there is no cost to this request.
the length of the list
Return an iterator for the list.
Return the value at the given index in the list.
From version 2.0.0, negative indexes are supported.
In general, this method is much slower than an array, but is still useful if you need to work with positions within the list.
However, if this is a major part of your use case and your list is large, then you should probably consider a different data structure.
Getting the first element in the queue is instant.
the element at the given index or undefined
if the index is beyond the end of the list. at(0)
will
return undefined
if the list is empty
Concatenate values onto the end of the list.
options values to concatenate; each value can be a T
or a LinkedList<T>
Return the last element in the list.
the last element in the list or undefined if the list is empty
Return a new LinkedList interator object that contains the index/value pairs for each in the list.
a new LinkedList iterator object
Test if each element of the list returns true
for the given predicate function.
a function to test each element in the list
a "this" value to bind to the predicate function
true
if all elements obey the predicate, otherwise false
Fill elements in the list with the given value. This method mutates the list.
the value to assign to each element in the list
the index of the first element to fill
the index of the element to be excluded from filling
the modified linked list
Filter the list and return a new list with the elements for which the predicate function returns true
.
a function to test each element in the list
a "this" value to bind to the predicate function
a new linked list with just the elements that satisfied the predicate
Find the first element in the list where the predicate function returns true
.
As recommended for Array.find
, do not mutate the list in your predicate function.
Unlike Array, LinkedList does not make any guarantees as to behaviour if you do this.
a function to test each element in the list
a "this" value to bind to the predicate function
the found element or undefined
if not found
Find the first element in the list where the predicate function returns true
and return the index of that element.
a function to test each element in the list
a "this" value to bind to the predicate function
the position in the list of the found element or -1 if not found
Create a new list with any sub-list elements interpolated into it, recursively up to the specified depth. Sub-lists are expanded and replace the sub-list element in the list.
For example,
1 -> 2 -> L -> 5 -> 6
|
3 -> 4
becomes:
1 -> 2 -> 3 -> 4 -> 5 -> 6
the depth at which to flatten; zero means nothing changes
a new linked list with sub-list elements interpolated into it
Create a new LinkedList populated with the results of calling the provided function on every element in the LinkedList and then flattening the list by a depth of 1.
It is identical to a map
followed by a flat
of depth 1 (list.map(...args).flat()),
but slightly more efficient than calling those two methods separately.
However, in this case, the callback function can return a single value or a linked list.
In that latter case, flatMap
extracts the values from that linked list and
inserts them in the final list that will be returned from flatMap
.
This is where flatMap
is faster than calling map
followed by flat
- because it only
traverses in the input list once and still achieves the flattening.
the basic return type of the callbackFn
(noting that callBackFn
can return
U
or a RecursiveLinkedList<U>
the function to call to map each element
a "this" value to bind to the callback function
a new linked list with each element being the result of the callback function and then the list flattened by a depth of 1.
Iterate over the elements calling the given function on each value.
a function to call for each element in the list
a "this" value to bind to the callback function
the linked list itself
Grow the linked list to the given length and set the new elements to the given value.
Use case for grow
:
Array(length)
then call Array.fill
. For LinkedList, use LinkedList.grow
instead.From version 2 of LinkedList, you can also use the same approach as Array, however, in that case
you'll need to declare the list as LinkedList<T | undefined>
because new LinkedList(length)
fills the list with undefined
- which may not be what you want.
grow
allows you to have a declaration of just LinkedList<T>
.
the new length; if this is <= the current length, no operation is performed
the value to use for the new elements
the modified list
Test if the list contains the given value, using the algorithm for comparison.
the value to look for in the list
the index from which to start the search; from version 2.0.0, -ve numbers are supported
true
if the list includes the given value
Find the first element in the list where the given value === the list value, and return the index.
the value to find in the list
the index from which to start the search; from version 2.0.0, -ve numbers are supported, just like Array
the index of the first list element that is === to testValue, or -1 if no such list element is found
Join the elements of the linked list with the given string as a separator.
Return the list of indexes in the linked list as an iterator.
If you want this list as a LinkedList, use keysAsList
.
Return the list of indexes in the linked list as a LinkedList.
If you want this as an iterator, use keys
.
Find the last element in the list where the given value === the list value, and return the index.
the value to find in the list
the index from which to start the search
the index of the last list element that is === to testValue, or -1 if no such list element is found
Create a new LinkedList populated with the results of calling the provided function on every element in the LinkedList.
the return type of the callbackFn
the function to call to map each element
a "this" value to bind to the callback function
a new linked list with each element being the result of the callback function
Remove the last element from the end of the list.
pop
requires complete traversal of the list, so you shouldn't use a singly-linked list
if you need to use pop
on large lists.
the removed element or undefined if the list is empty
Append to the end of the list, all the items in the given iterator.
push
is very fast on both Array and LinkedList. Even for very large LinkedList's,
push
remains typically O(1) complexity.
Note that Array does not have an overload that accepts an iterator, but for large appends an iterator uses much less stack space than spread syntax.
an iterator over a set of values
the new length of the list
Append to the end of the list.
push
is very fast on both Array and LinkedList. Even for very large LinkedList's,
push
remains typically O(1) complexity.
a set of values to append to the list
the new length of the list
The reduce
method executes a user-supplied callback function (reducerCallbackFn
) on each element of the list,
in left-to-right (first-to-last) order, passing in the return value from the calculation on the preceding element.
The final result of running the reducerCallbackFn
across all elements of the list is a single value.
The first time that the callback is run there is no "return value of the previous calculation".
Therefore, either the optional initialValue
parameter is used or, if that is not supplied,
the list element at index 0 is used as the initial value and iteration starts from
the next element (index 1 instead of index 0).
Regarding the optional initialValue
parameter: if initialValue
is supplied, that also causes currentValue
to be initialized to the first value in the list. If initialValue
is not supplied, accumulator
is initialized
to the first value in the list, and currentValue
is initialized to the second value in the list.
Regarding the reducerCallbackFn
callback function...
Its return value becomes the value of the accumulator
parameter on the next invocation of reducerCallbackFn
.
For the last invocation, the return value becomes the return value of reduce
.
reducerCallbackFn
is called with the following arguments:
accumulator
The value resulting from the previous call to reducerCallbackFn
. On first call, initialValue
if supplied,
otherwise the value of list.at(0)
.
currentValue
The value of the current element. On first call, the value of list.at(0)
if initialValue
was supplied,
otherwise the value of list.at(1)
.
currentIndex
The index position of currentValue
in list. On first call, 0 if initialValue
was supplied, otherwise 1.
list
The list reduce
was called upon.
the reducerCallbackFn
callback function
an optional value to which accumulator
is initialized the first time the callback is called.
Remove the first element from the list.
shift
is very fast on LinkedList and very slow on Array.
the element removed from the front of the list, or undefined
if the list is empty
Return a shallow copy of a part of the linked list, based on indexes of the elements in the list.
optional zero-based starting index, at which to start copying the list
optional; the index of the first element to exclude from the returned list
start
and end
can be negative from version 2.0.0
a new list containing the specified elements from the original list
Test if at least one element of the list returns true
for the given predicate function.
a function to test each element in the list
a "this" value to bind to the predicate function
true
if at least one element obeys the predicate, otherwise false
Change the linked list by removing or replacing existing elements and/or adding new elements.
zero-based starting index, at which to start changing the list; a negative value counts back from the end of the list
the number of elements to remove beginning at the start index
items to be inserted after the start index; if you don't provide any items then
splice
only removes elements from the list
a list containing the deleted elements
Return a string representation of the list and its elements.
Truncate the linked list to the given length. The removed elements are discarded.
if < 0, then it is set to zero. If >= the current length, no operation is performed
the modified list
Insert at the beginning of a list
unshift
is very fast on LinkedList and very slow on Array.
the new length of the list
The values of the list (as an iterator).
an iterator for the list
Create a new LinkedList from a string. The string is split into single characters and each character becomes an element in the list.
This behaviour mirrors Array.from(string)
.
Create a new LinkedList from a string but map each character to a different value.
The string is split into single characters and each character is mapped to another value and the result becomes an element in the list.
This behaviour mirrors Array.from(string)
.
if provided, every character to be added to the list is first passed through this function and the
mapFn
's return value is added to the list instead
value to use as this
when executing mapFn
Create a new LinkedList from an array of values.
Create a new LinkedList from an array of values, mapping each value to a different value.
if provided, every value to be added to the list is first passed through this function and the
mapFn
's return value is added to the list instead
value to use as this
when executing mapFn
Create a new LinkedList from an iterator over some values.
Create a new LinkedList from an iterator over some values, mapping each value to a different value.
if provided, every value to be added to the list is first passed through this function and the
mapFn
's return value is added to the list instead
value to use as this
when executing mapFn
Test if the given argument is an Iterable.
Test if the given value is a LinkedList.
Note that the T
generic type parameter cannot be verified using plain Typescript
and you must use other Typescript techniques to confirm matching types for the elements.
the value to test
true
if the value is a LinkedList
Test if the given argument is a true object.
Create a new LinkedList from the given variable number of arguments
Implements the sameValueZero
algorithm described here:
the left hand side of the equality test
the left hand side of the equality test
true
if lhs and rhs have the same value as defined by the sameValueZero
algorithm
Generated using TypeDoc
LinkedList is an implementation of a singly-linked Linked List using generics.
It's designed as a mostly drop-in replacement for arrays where you want to perform the following operations on large lists:
These operations are O(1) for a linked list, but O(n) for array shift and unshift.
Array push is extremely fast, and a linked list cannot generally compare because of the additional logic overhead. However, there is an element size and an array/list size threshold where linked list push is comparable to array push.
Use cases
Feature comparison with Array
LinkedList implements most of the same methods as Array. The unit tests specifically compare the operation of Array methods with LinkedList methods, to ensure compatibility.
However, a singly-linked list cannot easily or efficiently perform certain operations that you can with an Array, such as:
sort
,reverse
,reduceRight
- cannot be efficiently implemented and are excludedcopyWithin
- not yet implementedWorkaround
For missing methods or features, you can convert your LinkedList to an Array and perform the operation on that Array. For example:
Missing methods
Depending on the version of the
generic-linked-list
package, some methods that can be implemented may not yet be implemented. In this case, convert your list to an array and use array methods instead. See the above Workaround section for an example of how to achieve this.For those methods that cannot be implemented without a large performance issue, you should consider a different data structure (such as Array or Map).
Unit tests
This package includes unit tests that demonstrate operational and speed test results clearly. The push test, however, requires you to start node with
--expose-gc
, therefore, it is disabled.The tests show 3 orders of magnitude speed increase over Array for shift and unshift, which is significant for large arrays.
Examples