This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
The elements
parameter in the constructor is an
iterable collection of elements of type E
or R
. It is used to initialize the DoublyLinkedList
with the elements provided in the iterable. If no elements are provided, the default value is an
empty iterable.
Optional
options: DoublyLinkedListOptions<E, R>The options
parameter in the constructor is of type
DoublyLinkedListOptions<E, R>
. It is an optional parameter that allows you to pass additional
configuration options to customize the behavior of the DoublyLinkedList.
Time Complexity: O(1) Space Complexity: O(1)
The get first
function returns the first node in a doubly linked list, or undefined if the list is empty.
The method get first()
returns the first node of the doubly linked list, or undefined
if the list is empty.
Time Complexity: O(1) Space Complexity: O(1)
The get last
function returns the last node in a doubly linked list, or undefined if the list is empty.
The method get last()
returns the last node of the doubly linked list, or undefined
if the list is empty.
Protected
_createProtected
_ensureThe function _ensureNode
ensures that the input is a valid node in a doubly linked list.
The elementOrNode
parameter can be either
an element of type E
or a DoublyLinkedListNode
containing an element of type E
.
If the elementOrNode
parameter is already a DoublyLinkedListNode
, it will be returned
as is. Otherwise, a new DoublyLinkedListNode
instance will be created with the elementOrNode
value and returned.
Protected
_ensureThe function _ensurePredicate
in TypeScript ensures that the input is either a node, a predicate
function, or a value to compare with the node's value.
elementNodeOrPredicate - The elementNodeOrPredicate
parameter can be one of the following
types:
A function is being returned that takes a DoublyLinkedListNode
as a parameter and
returns a boolean value based on the conditions specified in the code.
Protected
_getProtected
_getThe function returns an iterator that iterates over the nodes of a doubly linked list starting from the head.
Protected
_getThe function _getPrevNode
returns the previous node of a given node in a doubly linked list.
The parameter node
in the _getPrevNode
method is of type
DoublyLinkedListNode<E>
, which represents a node in a doubly linked list containing an element
of type E
.
The _getPrevNode
method is returning the previous node of the input node
in a doubly
linked list. If the input node has a previous node, it will return that node. Otherwise, it will
return undefined
.
Protected
_getProtected
_isThe function _isPredicate
checks if the input is a function that takes a DoublyLinkedListNode
as an argument and returns a boolean.
elementNodeOrPredicate - The elementNodeOrPredicate
parameter can be one of the following
types:
The _isPredicate method is returning a boolean value indicating whether the elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a function, the method will return true, indicating that it is a predicate function.
Time Complexity: O(n) Space Complexity: O(1)
The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
Rest
...args: any[]The args
parameter in the code snippet represents a rest parameter. It
allows the function to accept any number of arguments as an array. In this case, the args
parameter is used to pass any number of arguments to the _getIterator
method.
Time Complexity: O(1) or O(n) Space Complexity: O(1)
The addAfter
function in TypeScript adds a new element or node after an existing element or node
in a doubly linked list.
existingElementOrNode represents the element or node in the doubly linked list after which you want to add a new element or node.
The newElementOrNode
parameter in the
addAfter
method represents the element or node that you want to add after the existing element
or node in a doubly linked list. This parameter can be either an element value or a
DoublyLinkedListNode
object that you want to insert
The addAfter
method returns a boolean value - true
if the new element or node was
successfully added after the existing element or node, and false
if the existing element or node
was not found in the linked list.
Time Complexity: O(n) Space Complexity: O(1)
The addAt
function inserts a new element or node at a specified index in a doubly linked list.
The index
parameter in the addAt
method represents the position at
which you want to add a new element or node in the doubly linked list. It indicates the location
where the new element or node should be inserted.
The newElementOrNode
parameter in the
addAt
method can be either a value of type E
or a DoublyLinkedListNode<E>
object.
The addAt
method returns a boolean value. It returns true
if the element or node was
successfully added at the specified index, and false
if the index is out of bounds (less than 0
or greater than the length of the list).
Time Complexity: O(1) or O(n) Space Complexity: O(1)
The addBefore
function in TypeScript adds a new element or node before an existing element or
node in a doubly linked list.
The existingElementOrNode
parameter
in the addBefore
method can be either an element of type E
or a DoublyLinkedListNode<E>
.
The newElementOrNode
parameter
represents the element or node that you want to add before the existingElementOrNode
in a doubly
linked list.
The addBefore
method returns a boolean value - true
if the new element or node was
successfully added before the existing element or node, and false
if the existing element or
node was not found.
Time Complexity: O(n) Space Complexity: O(1)
The at
function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
The index parameter is a number that represents the position of the element we want to retrieve from the list.
The method is returning the value at the specified index in the linked list. If the index is out of bounds or the linked list is empty, it will return undefined.
Time Complexity: O(n) Space Complexity: O(n)
The clone
function creates a new instance of the DoublyLinkedList
class with the same values
as the original list.
The clone()
method is returning a new instance of the DoublyLinkedList
class, which
is a copy of the original list.
Time Complexity: O(n) Space Complexity: O(1)
The function countOccurrences
iterates through a doubly linked list and counts the occurrences
of a specified element or nodes that satisfy a given predicate.
The elementOrNode
parameter in the countOccurrences
method can accept three types of values:
The countOccurrences
method returns the number of occurrences of the specified element,
node, or predicate function in the doubly linked list.
Time Complexity: O(1) or O(n) Space Complexity: O(1)
The delete
function removes a specified element or node from a doubly linked list if it exists.
The elementOrNode
parameter in
the delete
method can accept an element of type E
, a DoublyLinkedListNode
of type E
, or it
can be undefined
. This parameter is used to identify the node that needs to be deleted from the
doubly linked list
The delete
method returns a boolean value - true
if the element or node was
successfully deleted from the doubly linked list, and false
if the element or node was not found
in the list.
Time Complexity: O(n) Space Complexity: O(1)
The deleteAt
function removes an element at a specified index from a linked list and returns the removed element.
The index parameter represents the position of the element that needs to be deleted in the data structure. It is of type number.
The method deleteAt
returns the value of the node that was deleted, or undefined
if the index is out of
bounds.
Time Complexity: O(n) Space Complexity: O(1)
The every
function checks if every element in the array satisfies a given predicate.
The predicate
parameter is a callback function that takes three arguments:
the current element being processed, its index, and the array it belongs to. It should return a
boolean value indicating whether the element satisfies a certain condition or not.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as this
when executing the predicate
function. If thisArg
is provided, it will be
passed as the this
value to the predicate
function. If thisArg
is
The every
method is returning a boolean value. It returns true
if every element in
the array satisfies the provided predicate function, and false
otherwise.
Time Complexity: O(n) Space Complexity: O(1)
The fill
function in TypeScript fills a specified range in an array-like object with a given
value.
The value
parameter in the fill
method represents the element that will be
used to fill the specified range in the array.
Optional
start: number = 0The start
parameter specifies the index at which to start filling the array
with the specified value. If not provided, it defaults to 0, indicating the beginning of the
array.
The end
parameter in the fill
function represents the index at which the filling
of values should stop. It specifies the end of the range within the array where the value
should
be filled.
The fill
method is returning the modified object (this
) after filling the specified
range with the provided value.
Time Complexity: O(n) Space Complexity: O(n)
The filter
function creates a new DoublyLinkedList by iterating over the elements of the current
list and applying a callback function to each element, returning only the elements for which the
callback function returns true.
The callback
parameter is a function that will be called for each element in
the DoublyLinkedList. It takes three arguments: the current element, the index of the current
element, and the DoublyLinkedList itself. The callback function should return a boolean value
indicating whether the current element should be included
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as this
when executing the callback
function. If thisArg
is provided, it will be
passed as the this
value to the callback
function. If thisArg
is
The filter
method is returning a new DoublyLinkedList
object that contains the
elements that pass the filter condition specified by the callback
function.
Time Complexity: O(n) Space Complexity: O(1)
The findIndex
function iterates over an array and returns the index of the first element that
satisfies the provided predicate function.
The predicate
parameter in the findIndex
function is a callback function
that takes three arguments: item
, index
, and the array this
. It should return a boolean
value indicating whether the current element satisfies the condition being checked for.
Optional
thisArg: anyThe thisArg
parameter in the findIndex
function is an optional
parameter that specifies the value to use as this
when executing the predicate
function. If
provided, the predicate
function will be called with thisArg
as its this
value. If @returns The
findIndex` method is returning the index of the first element in the array that
satisfies the provided predicate function. If no such element is found, it returns -1.
Time Complexity: O(n) Space Complexity: O(1)
The forEach
function iterates over each element in an array-like object and calls a callback
function for each element.
The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array that forEach was called upon.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as this
when executing the callbackfn
function. If thisArg
is provided, it will
be passed as the this
value to the callbackfn
function. If `thisArg
Time Complexity: O(n) Space Complexity: O(1)
The getBackward
function searches for a specific element in a doubly linked list starting from
the tail and moving backwards.
elementNodeOrPredicate - The elementNodeOrPredicate
parameter in the getBackward
function can be one of the following types:
The getBackward
method returns the value of the element node that matches the provided
predicate when traversing the doubly linked list backwards. If no matching element is found, it
returns undefined
.
Time Complexity: O(n) Space Complexity: O(1)
This TypeScript function searches for a node in a doubly linked list based on a given element node or predicate.
The getNode
method you provided is used to find a
node in a doubly linked list based on a given element, node, or predicate function. The
elementNodeOrPredicate
parameter can be one of the following:
The getNode
method returns a DoublyLinkedListNode<E>
or undefined
based on the
input elementNodeOrPredicate
. If the input is undefined
, the method returns undefined
.
Otherwise, it iterates through the linked list starting from the head node and applies the
provided predicate function to each node. If a node satisfies the predicate, that node is
returned. If
Time Complexity: O(n) Space Complexity: O(1)
The function getNodeAt
returns the node at a given index in a doubly linked list, or undefined if the index is out of
range.
The index
parameter is a number that represents the position of the node we want to
retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
The method getNodeAt(index: number)
returns a DoublyLinkedListNode<E>
object if the index is within the
valid range of the linked list, otherwise it returns undefined
.
Time Complexity: O(n) Space Complexity: O(1)
The function checks if a given element exists in a collection.
The parameter "element" is of type E, which means it can be any type. It represents the element that we want to check for existence in the collection.
a boolean value. It returns true if the element is found in the collection, and false otherwise.
Time Complexity: O(n) Space Complexity: O(1)
The function overrides the indexOf method to improve performance by searching for an element in a custom array implementation starting from a specified index.
The searchElement
parameter is the element that you are searching for
within the array. The indexOf
method will return the index of the first occurrence of this
element within the array.
Optional
fromIndex: number = 0The fromIndex
parameter in the indexOf
method specifies the
index in the array at which to start the search for the searchElement
. If provided, the search
will begin at the specified index and continue to the end of the array. If not provided, the
search will start at index
The indexOf
method is returning the index of the searchElement
if it is found in the
array starting from the fromIndex
. If the searchElement
is not found, it returns -1.
Time Complexity: O(1) Space Complexity: O(1)
The function isNode
in TypeScript checks if a given input is an instance of
DoublyLinkedListNode
.
elementNodeOrPredicate - The elementNodeOrPredicate
parameter in the isNode
function can
be one of the following types:
The isNode
function is checking if the elementNodeOrPredicate
parameter is an
instance of DoublyLinkedListNode<E>
. If it is, the function returns true
, indicating that the
parameter is a DoublyLinkedListNode<E>
. If it is not an instance of DoublyLinkedListNode<E>
,
the function returns false
.
Time Complexity: O(n) Space Complexity: O(1)
The join
function in TypeScript returns a string by joining the elements of an array with a
specified separator.
Optional
separator: string = ','The separator
parameter is a string that specifies the character
or characters that will be used to separate each element when joining them into a single string.
By default, the separator is set to a comma (,
), but you can provide a different separator if
needed.
The join
method is being returned, which takes an optional separator
parameter
(defaulting to a comma) and returns a string created by joining all elements of the array after
converting it to an array.
Time Complexity: O(n) Space Complexity: O(1)
The function overrides the lastIndexOf method in TypeScript to improve performance by searching for an element in reverse order starting from a specified index.
The searchElement
parameter is the element that you want to find
within the array. The lastIndexOf
method searches the array for this element starting from the
end of the array (or from the specified fromIndex
if provided) and returns the index of the last
occurrence of the element
The fromIndex
parameter in the lastIndexOf
method specifies the
index at which to start searching for the searchElement
in the array. If provided, the search
will begin at this index and move towards the beginning of the array. If not provided, the search
will start at the
The lastIndexOf
method is being overridden to search for the searchElement
starting
from the specified fromIndex
(defaulting to the end of the array). It iterates over the array in
reverse order using a custom iterator _getReverseIterator
and returns the index of the last
occurrence of the searchElement
if found, or -1 if not found.
Time Complexity: O(n) Space Complexity: O(n)
The map
function takes a callback function and returns a new DoublyLinkedList with the results
of applying the callback to each element in the original list.
The callback parameter is a function that will be called for each element in the original DoublyLinkedList. It takes three arguments: current (the current element being processed), index (the index of the current element), and this (the original DoublyLinkedList). The callback function should return a value of type
Optional
toElementFn: ((rawElement: RM) => EM)The toElementFn
parameter is an optional function that can be used to
convert the raw element (RR
) to the desired element type (T
). It takes the raw element as
input and returns the converted element. If this parameter is not provided, the raw element will
be used as is.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that allows you to
specify the value of this
within the callback function. It is used to set the context or scope
in which the callback function will be executed. If thisArg
is provided, it will be used as the
value of
a new instance of the DoublyLinkedList
class with elements of type T
and RR
.
Time Complexity: O(1) Space Complexity: O(1)
The push
function adds a new element or node to the end of a doubly linked list.
The elementOrNode
parameter in the push
method can accept either an element of type E
or a DoublyLinkedListNode<E>
object.
The push
method is returning a boolean value, specifically true
.
Time Complexity: O(k) Space Complexity: O(k)
The function pushMany
iterates over elements and pushes them into a data structure, applying a
transformation function if provided.
The elements
parameter in the pushMany
function can accept an iterable containing elements of type E
, R
,
or DoublyLinkedListNode<E>
. The function iterates over each element in the iterable and pushes
it onto the linked list. If a transformation function to @returns The
pushMany function is returning an array of boolean values (
ans`) which indicate
the success or failure of pushing each element into the data structure.
Time Complexity: O(n) Space Complexity: O(1)
This function retrieves an element from a doubly linked list based on a given element node or predicate.
elementNodeOrPredicate - The get
method takes in a parameter called elementNodeOrPredicate
,
which can be one of the following types:
The get
method returns the value of the first node in the doubly linked list that
satisfies the provided predicate function. If no such node is found, it returns undefined
.
Time Complexity: O(n) Space Complexity: O(1)
The function setAt
updates the value at a specified index in a data structure if the index
exists.
The index
parameter in the setAt
method refers to the position in the
data structure where you want to set a new value.
The value
parameter in the setAt
method represents the new value that you
want to set at the specified index in the data structure.
The setAt
method returns a boolean value - true
if the value at the specified index
is successfully updated, and false
if the index is out of bounds.
Time Complexity: O(m) Space Complexity: O(m)
The slice
method is overridden to improve performance by creating a new instance and iterating
through the array to extract a subset based on the specified start and end indices.
Optional
start: number = 0The start
parameter in the slice
method specifies the index at
which to begin extracting elements from the array. If no start
parameter is provided, the
default value is 0, indicating that extraction should start from the beginning of the array.
The end
parameter in the slice
method represents the index at which to
end the slicing of the array. If not provided, it defaults to the length of the array.
The slice
method is returning a new instance of the array implementation with elements
sliced from the original array based on the start
and end
parameters.
Time Complexity: O(n) Space Complexity: O(1)
The "some" function checks if at least one element in a collection satisfies a given predicate.
The predicate
parameter is a callback function that takes three arguments:
value
, index
, and array
. It should return a boolean value indicating whether the current
element satisfies the condition.
Optional
thisArg: anyThe thisArg
parameter is an optional argument that specifies the value
to be used as the this
value when executing the predicate
function. If thisArg
is provided,
it will be passed as the this
value to the predicate
function. If `thisArg
a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.
Time Complexity: O(n log n) Space Complexity: O(n)
The sort
function in TypeScript sorts the elements of a collection using a specified comparison
function.
Optional
compareFn: ((a: E, b: E) => number)The compareFn
parameter is a function that defines the sort order. It takes
two elements a
and b
as input and returns a number indicating their relative order. If the
returned value is negative, a
comes before b
. If the returned value is positive, @returns The
sort` method is returning the instance of the object on which it is called (this),
after sorting the elements based on the provided comparison function (compareFn).
Time Complexity: O(n + m) Space Complexity: O(m)
The function overrides the splice method to handle deletion and insertion of elements in a data structure while returning the removed elements.
The start
parameter in the splice
method indicates the index at which
to start modifying the array.
Optional
deleteCount: number = 0The deleteCount
parameter in the splice
method specifies the
number of elements to remove from the array starting at the specified start
index. If
deleteCount
is not provided, it defaults to 0, meaning no elements will be removed but new
elements can still be inserted at
Rest
...items: E[]The items
parameter in the splice
method represents the elements that
will be inserted into the array at the specified start
index. These elements can be of any type
and there can be multiple elements passed as arguments to be inserted into the array.
The splice
method is returning a new instance of the data structure that was modified
by removing elements specified by the start
and deleteCount
parameters, and inserting new
elements provided in the items
array.
Time Complexity: O(1) Space Complexity: O(1)
The unshift function adds a new element or node to the beginning of a doubly linked list.
The elementOrNode
parameter in the
unshift
method can be either an element of type E
or a DoublyLinkedListNode
containing an
element of type E
.
The unshift
method is returning a boolean value, specifically true
.
Time Complexity: O(k) Space Complexity: O(k)
The function unshiftMany
iterates through a collection of elements and adds them to the
beginning of a Doubly Linked List, returning an array of boolean values indicating the success of
each insertion.
The elements
parameter in the unshiftMany
function can accept an iterable containing elements of type E
,
R
, or DoublyLinkedListNode<E>
. The function iterates over each element in the iterable and
performs an unshift
operation on the doubly linked list
The unshiftMany
function returns an array of boolean values indicating the success of
each unshift operation performed on the elements passed as input.
Static
fromTime Complexity: O(n) Space Complexity: O(n)
The fromArray
function creates a new instance of a DoublyLinkedList and populates it with the elements from the
given array.
The data
parameter is an array of elements of type E
.
The fromArray
function returns a DoublyLinkedList object.
Example
Example
Example
Example
Example
Example