data-structure-typed 1.35.0 → 1.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +3 -0
- package/CHANGELOG.md +8 -1
- package/CONTRIBUTING.md +18 -0
- package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
- package/dist/data-structures/binary-tree/avl-tree.js +90 -3
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +21 -0
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +133 -0
- package/dist/data-structures/binary-tree/bst.js +114 -0
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/index.d.ts +12 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
- package/dist/data-structures/graph/abstract-graph.js +270 -7
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/data-structures/graph/directed-graph.js +167 -0
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +54 -0
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
- package/dist/data-structures/graph/undirected-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
- package/dist/data-structures/hash/coordinate-map.js +35 -0
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
- package/dist/data-structures/hash/coordinate-set.js +28 -0
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +56 -0
- package/dist/data-structures/hash/hash-map.js +29 -1
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +106 -0
- package/dist/data-structures/hash/hash-table.js +88 -6
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/index.d.ts +7 -0
- package/dist/data-structures/hash/pair.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/heap/heap.d.ts +100 -0
- package/dist/data-structures/heap/heap.js +215 -76
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/max-heap.d.ts +12 -0
- package/dist/data-structures/heap/max-heap.js +16 -6
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.d.ts +12 -0
- package/dist/data-structures/heap/min-heap.js +16 -6
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +15 -0
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
- package/dist/data-structures/matrix/matrix2d.js +91 -2
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.d.ts +201 -0
- package/dist/data-structures/matrix/vector2d.js +188 -1
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +16 -17
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +16 -17
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/priority-queue.js +11 -174
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.d.ts +165 -0
- package/dist/data-structures/queue/deque.js +124 -0
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/queue.d.ts +107 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/stack.d.ts +63 -0
- package/dist/data-structures/stack/stack.js +50 -0
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +1 -0
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/trie.d.ts +61 -0
- package/dist/data-structures/trie/trie.js +36 -0
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
- package/dist/interfaces/abstract-graph.d.ts +5 -0
- package/dist/interfaces/avl-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +6 -0
- package/dist/interfaces/directed-graph.d.ts +3 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/rb-tree.d.ts +6 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/tree-multiset.d.ts +6 -0
- package/dist/interfaces/undirected-graph.d.ts +3 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +6 -0
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/hash.d.ts +1 -0
- package/dist/types/data-structures/heap.d.ts +1 -0
- package/dist/types/data-structures/index.d.ts +16 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/utils.d.ts +19 -0
- package/lib/data-structures/graph/abstract-graph.js +3 -5
- package/lib/data-structures/heap/heap.d.ts +85 -68
- package/lib/data-structures/heap/heap.js +186 -108
- package/lib/data-structures/heap/max-heap.d.ts +6 -17
- package/lib/data-structures/heap/max-heap.js +11 -17
- package/lib/data-structures/heap/min-heap.d.ts +6 -18
- package/lib/data-structures/heap/min-heap.js +11 -18
- package/lib/data-structures/priority-queue/max-priority-queue.d.ts +5 -8
- package/lib/data-structures/priority-queue/max-priority-queue.js +11 -30
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +5 -8
- package/lib/data-structures/priority-queue/min-priority-queue.js +11 -31
- package/lib/data-structures/priority-queue/priority-queue.d.ts +6 -174
- package/lib/data-structures/priority-queue/priority-queue.js +11 -315
- package/lib/types/data-structures/heap.d.ts +1 -3
- package/package.json +11 -7
- package/src/data-structures/graph/abstract-graph.ts +3 -5
- package/src/data-structures/heap/heap.ts +182 -140
- package/src/data-structures/heap/max-heap.ts +15 -22
- package/src/data-structures/heap/min-heap.ts +15 -23
- package/src/data-structures/priority-queue/max-priority-queue.ts +14 -47
- package/src/data-structures/priority-queue/min-priority-queue.ts +14 -48
- package/src/data-structures/priority-queue/priority-queue.ts +7 -350
- package/src/types/data-structures/heap.ts +1 -5
- package/test/integration/avl-tree.test.ts +4 -4
- package/test/integration/bst.test.ts +8 -8
- package/test/unit/data-structures/graph/directed-graph.test.ts +5 -5
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +26 -18
- package/test/unit/data-structures/heap/max-heap.test.ts +50 -42
- package/test/unit/data-structures/heap/min-heap.test.ts +38 -68
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +7 -9
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +14 -30
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.LICENSE.txt +8 -0
- package/umd/bundle.min.js.map +1 -1
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/linked-list/index.ts +0 -4
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
export declare class DoublyLinkedListNode<E = any> {
|
|
9
|
+
/**
|
|
10
|
+
* The constructor function initializes the value, next, and previous properties of an object.
|
|
11
|
+
* @param {E} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
12
|
+
* is defined as a generic type "E".
|
|
13
|
+
*/
|
|
14
|
+
constructor(val: E);
|
|
15
|
+
private _val;
|
|
16
|
+
get val(): E;
|
|
17
|
+
set val(value: E);
|
|
18
|
+
private _next;
|
|
19
|
+
get next(): DoublyLinkedListNode<E> | null;
|
|
20
|
+
set next(value: DoublyLinkedListNode<E> | null);
|
|
21
|
+
private _prev;
|
|
22
|
+
get prev(): DoublyLinkedListNode<E> | null;
|
|
23
|
+
set prev(value: DoublyLinkedListNode<E> | null);
|
|
24
|
+
}
|
|
25
|
+
export declare class DoublyLinkedList<E = any> {
|
|
26
|
+
/**
|
|
27
|
+
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
|
+
*/
|
|
29
|
+
constructor();
|
|
30
|
+
private _head;
|
|
31
|
+
get head(): DoublyLinkedListNode<E> | null;
|
|
32
|
+
set head(value: DoublyLinkedListNode<E> | null);
|
|
33
|
+
private _tail;
|
|
34
|
+
get tail(): DoublyLinkedListNode<E> | null;
|
|
35
|
+
set tail(value: DoublyLinkedListNode<E> | null);
|
|
36
|
+
private _length;
|
|
37
|
+
get length(): number;
|
|
38
|
+
/**
|
|
39
|
+
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
40
|
+
* given array.
|
|
41
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
42
|
+
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
43
|
+
*/
|
|
44
|
+
static fromArray<E>(data: E[]): DoublyLinkedList<E>;
|
|
45
|
+
/**
|
|
46
|
+
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
47
|
+
* @param {E} val - The value to be added to the linked list.
|
|
48
|
+
*/
|
|
49
|
+
push(val: E): void;
|
|
50
|
+
/**
|
|
51
|
+
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
52
|
+
* @param {E} val - The value to be added to the linked list.
|
|
53
|
+
*/
|
|
54
|
+
addLast(val: E): void;
|
|
55
|
+
/**
|
|
56
|
+
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
57
|
+
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
58
|
+
* list is empty, it returns null.
|
|
59
|
+
*/
|
|
60
|
+
pop(): E | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
63
|
+
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
64
|
+
* list is empty, it returns null.
|
|
65
|
+
*/
|
|
66
|
+
pollLast(): E | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
69
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
70
|
+
* list.
|
|
71
|
+
*/
|
|
72
|
+
shift(): E | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
75
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
76
|
+
* list.
|
|
77
|
+
*/
|
|
78
|
+
pollFirst(): E | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
81
|
+
* @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
|
82
|
+
* doubly linked list.
|
|
83
|
+
*/
|
|
84
|
+
unshift(val: E): void;
|
|
85
|
+
/**
|
|
86
|
+
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
87
|
+
* @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
|
88
|
+
* doubly linked list.
|
|
89
|
+
*/
|
|
90
|
+
addFirst(val: E): void;
|
|
91
|
+
/**
|
|
92
|
+
* The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
93
|
+
* @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
94
|
+
*/
|
|
95
|
+
peekFirst(): E | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
98
|
+
* @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
99
|
+
*/
|
|
100
|
+
peekLast(): E | undefined;
|
|
101
|
+
get size(): number;
|
|
102
|
+
/**
|
|
103
|
+
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
104
|
+
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
105
|
+
* retrieve from the list.
|
|
106
|
+
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
107
|
+
* or the linked list is empty, it will return null.
|
|
108
|
+
*/
|
|
109
|
+
getAt(index: number): E | undefined;
|
|
110
|
+
/**
|
|
111
|
+
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
|
|
112
|
+
* range.
|
|
113
|
+
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
114
|
+
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
|
115
|
+
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<E>` object if the index is within the
|
|
116
|
+
* valid range of the linked list, otherwise it returns `null`.
|
|
117
|
+
*/
|
|
118
|
+
getNodeAt(index: number): DoublyLinkedListNode<E> | null;
|
|
119
|
+
/**
|
|
120
|
+
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
121
|
+
* node if found, otherwise it returns null.
|
|
122
|
+
* @param {E} val - The `val` parameter is the value that we want to search for in the doubly linked list.
|
|
123
|
+
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
124
|
+
* is found in the linked list. If no such node is found, it returns `null`.
|
|
125
|
+
*/
|
|
126
|
+
findNode(val: E): DoublyLinkedListNode<E> | null;
|
|
127
|
+
/**
|
|
128
|
+
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
129
|
+
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
130
|
+
* DoublyLinkedList. It is of type number.
|
|
131
|
+
* @param {E} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
132
|
+
* specified index.
|
|
133
|
+
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
134
|
+
* if the index is out of bounds.
|
|
135
|
+
*/
|
|
136
|
+
insertAt(index: number, val: E): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
139
|
+
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
140
|
+
* data structure. It is of type number.
|
|
141
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
142
|
+
* bounds.
|
|
143
|
+
*/
|
|
144
|
+
deleteAt(index: number): E | undefined;
|
|
145
|
+
delete(valOrNode: E): boolean;
|
|
146
|
+
delete(valOrNode: DoublyLinkedListNode<E>): boolean;
|
|
147
|
+
/**
|
|
148
|
+
* The `toArray` function converts a linked list into an array.
|
|
149
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
150
|
+
*/
|
|
151
|
+
toArray(): E[];
|
|
152
|
+
/**
|
|
153
|
+
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
154
|
+
* @returns A boolean value is being returned.
|
|
155
|
+
*/
|
|
156
|
+
isEmpty(): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
159
|
+
*/
|
|
160
|
+
clear(): void;
|
|
161
|
+
/**
|
|
162
|
+
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
163
|
+
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
164
|
+
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
165
|
+
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
166
|
+
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
167
|
+
*/
|
|
168
|
+
find(callback: (val: E) => boolean): E | null;
|
|
169
|
+
/**
|
|
170
|
+
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
171
|
+
* @param {E} val - The parameter `val` is of type `E`, which means it can be any data type. It represents the value
|
|
172
|
+
* that we are searching for in the linked list.
|
|
173
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
|
|
174
|
+
* list. If the value is not found, it returns -1.
|
|
175
|
+
*/
|
|
176
|
+
indexOf(val: E): number;
|
|
177
|
+
/**
|
|
178
|
+
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last
|
|
179
|
+
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
180
|
+
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
181
|
+
* function is used to determine whether a given value satisfies a certain condition.
|
|
182
|
+
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
|
|
183
|
+
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
184
|
+
*/
|
|
185
|
+
findLast(callback: (val: E) => boolean): E | null;
|
|
186
|
+
/**
|
|
187
|
+
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
|
|
188
|
+
* @returns The `toArrayReverse()` function returns an array of type `E[]`.
|
|
189
|
+
*/
|
|
190
|
+
toArrayReverse(): E[];
|
|
191
|
+
/**
|
|
192
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
193
|
+
*/
|
|
194
|
+
reverse(): void;
|
|
195
|
+
/**
|
|
196
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
197
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
198
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
199
|
+
* current node in the linked list.
|
|
200
|
+
*/
|
|
201
|
+
forEach(callback: (val: E, index: number) => void): void;
|
|
202
|
+
/**
|
|
203
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
204
|
+
* DoublyLinkedList with the transformed values.
|
|
205
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
206
|
+
* the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
207
|
+
* DoublyLinkedList).
|
|
208
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
209
|
+
*/
|
|
210
|
+
map<U>(callback: (val: E) => U): DoublyLinkedList<U>;
|
|
211
|
+
/**
|
|
212
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
213
|
+
* elements that satisfy the given callback function.
|
|
214
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
215
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
216
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
217
|
+
*/
|
|
218
|
+
filter(callback: (val: E) => boolean): DoublyLinkedList<E>;
|
|
219
|
+
/**
|
|
220
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
221
|
+
* single value.
|
|
222
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
223
|
+
* used to perform a specific operation on each element of the linked list.
|
|
224
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
225
|
+
* point for the reduction operation.
|
|
226
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
227
|
+
* elements in the linked list.
|
|
228
|
+
*/
|
|
229
|
+
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
|
|
230
|
+
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
231
|
+
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
232
|
+
insertBefore(existingValueOrNode: E, newValue: E): boolean;
|
|
233
|
+
insertBefore(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
234
|
+
}
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
4
11
|
class DoublyLinkedListNode {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes the value, next, and previous properties of an object.
|
|
14
|
+
* @param {E} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
15
|
+
* is defined as a generic type "E".
|
|
16
|
+
*/
|
|
5
17
|
constructor(val) {
|
|
6
18
|
this._val = val;
|
|
7
19
|
this._next = null;
|
|
@@ -28,6 +40,9 @@ class DoublyLinkedListNode {
|
|
|
28
40
|
}
|
|
29
41
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
30
42
|
class DoublyLinkedList {
|
|
43
|
+
/**
|
|
44
|
+
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
45
|
+
*/
|
|
31
46
|
constructor() {
|
|
32
47
|
this._head = null;
|
|
33
48
|
this._tail = null;
|
|
@@ -48,6 +63,12 @@ class DoublyLinkedList {
|
|
|
48
63
|
get length() {
|
|
49
64
|
return this._length;
|
|
50
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
68
|
+
* given array.
|
|
69
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
70
|
+
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
71
|
+
*/
|
|
51
72
|
static fromArray(data) {
|
|
52
73
|
const doublyLinkedList = new DoublyLinkedList();
|
|
53
74
|
for (const item of data) {
|
|
@@ -55,6 +76,10 @@ class DoublyLinkedList {
|
|
|
55
76
|
}
|
|
56
77
|
return doublyLinkedList;
|
|
57
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
81
|
+
* @param {E} val - The value to be added to the linked list.
|
|
82
|
+
*/
|
|
58
83
|
push(val) {
|
|
59
84
|
const newNode = new DoublyLinkedListNode(val);
|
|
60
85
|
if (!this.head) {
|
|
@@ -68,9 +93,18 @@ class DoublyLinkedList {
|
|
|
68
93
|
}
|
|
69
94
|
this._length++;
|
|
70
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
98
|
+
* @param {E} val - The value to be added to the linked list.
|
|
99
|
+
*/
|
|
71
100
|
addLast(val) {
|
|
72
101
|
this.push(val);
|
|
73
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
105
|
+
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
106
|
+
* list is empty, it returns null.
|
|
107
|
+
*/
|
|
74
108
|
pop() {
|
|
75
109
|
if (!this.tail)
|
|
76
110
|
return undefined;
|
|
@@ -86,9 +120,19 @@ class DoublyLinkedList {
|
|
|
86
120
|
this._length--;
|
|
87
121
|
return removedNode.val;
|
|
88
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
125
|
+
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
126
|
+
* list is empty, it returns null.
|
|
127
|
+
*/
|
|
89
128
|
pollLast() {
|
|
90
129
|
return this.pop();
|
|
91
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
133
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
134
|
+
* list.
|
|
135
|
+
*/
|
|
92
136
|
shift() {
|
|
93
137
|
if (!this.head)
|
|
94
138
|
return undefined;
|
|
@@ -104,9 +148,19 @@ class DoublyLinkedList {
|
|
|
104
148
|
this._length--;
|
|
105
149
|
return removedNode.val;
|
|
106
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
153
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
154
|
+
* list.
|
|
155
|
+
*/
|
|
107
156
|
pollFirst() {
|
|
108
157
|
return this.shift();
|
|
109
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
161
|
+
* @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
|
162
|
+
* doubly linked list.
|
|
163
|
+
*/
|
|
110
164
|
unshift(val) {
|
|
111
165
|
const newNode = new DoublyLinkedListNode(val);
|
|
112
166
|
if (!this.head) {
|
|
@@ -120,13 +174,26 @@ class DoublyLinkedList {
|
|
|
120
174
|
}
|
|
121
175
|
this._length++;
|
|
122
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
179
|
+
* @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
|
180
|
+
* doubly linked list.
|
|
181
|
+
*/
|
|
123
182
|
addFirst(val) {
|
|
124
183
|
this.unshift(val);
|
|
125
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
187
|
+
* @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
188
|
+
*/
|
|
126
189
|
peekFirst() {
|
|
127
190
|
var _a;
|
|
128
191
|
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
|
|
129
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
195
|
+
* @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
196
|
+
*/
|
|
130
197
|
peekLast() {
|
|
131
198
|
var _a;
|
|
132
199
|
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.val;
|
|
@@ -134,6 +201,13 @@ class DoublyLinkedList {
|
|
|
134
201
|
get size() {
|
|
135
202
|
return this.length;
|
|
136
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
206
|
+
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
207
|
+
* retrieve from the list.
|
|
208
|
+
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
209
|
+
* or the linked list is empty, it will return null.
|
|
210
|
+
*/
|
|
137
211
|
getAt(index) {
|
|
138
212
|
if (index < 0 || index >= this.length)
|
|
139
213
|
return undefined;
|
|
@@ -143,6 +217,14 @@ class DoublyLinkedList {
|
|
|
143
217
|
}
|
|
144
218
|
return current.val;
|
|
145
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
|
|
222
|
+
* range.
|
|
223
|
+
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
224
|
+
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
|
225
|
+
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<E>` object if the index is within the
|
|
226
|
+
* valid range of the linked list, otherwise it returns `null`.
|
|
227
|
+
*/
|
|
146
228
|
getNodeAt(index) {
|
|
147
229
|
if (index < 0 || index >= this.length)
|
|
148
230
|
return null;
|
|
@@ -152,6 +234,13 @@ class DoublyLinkedList {
|
|
|
152
234
|
}
|
|
153
235
|
return current;
|
|
154
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
239
|
+
* node if found, otherwise it returns null.
|
|
240
|
+
* @param {E} val - The `val` parameter is the value that we want to search for in the doubly linked list.
|
|
241
|
+
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
242
|
+
* is found in the linked list. If no such node is found, it returns `null`.
|
|
243
|
+
*/
|
|
155
244
|
findNode(val) {
|
|
156
245
|
let current = this.head;
|
|
157
246
|
while (current) {
|
|
@@ -162,6 +251,15 @@ class DoublyLinkedList {
|
|
|
162
251
|
}
|
|
163
252
|
return null;
|
|
164
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
256
|
+
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
257
|
+
* DoublyLinkedList. It is of type number.
|
|
258
|
+
* @param {E} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
259
|
+
* specified index.
|
|
260
|
+
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
261
|
+
* if the index is out of bounds.
|
|
262
|
+
*/
|
|
165
263
|
insertAt(index, val) {
|
|
166
264
|
if (index < 0 || index > this.length)
|
|
167
265
|
return false;
|
|
@@ -183,6 +281,13 @@ class DoublyLinkedList {
|
|
|
183
281
|
this._length++;
|
|
184
282
|
return true;
|
|
185
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
286
|
+
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
287
|
+
* data structure. It is of type number.
|
|
288
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
289
|
+
* bounds.
|
|
290
|
+
*/
|
|
186
291
|
deleteAt(index) {
|
|
187
292
|
if (index < 0 || index >= this.length)
|
|
188
293
|
return undefined;
|
|
@@ -198,6 +303,13 @@ class DoublyLinkedList {
|
|
|
198
303
|
this._length--;
|
|
199
304
|
return removedNode.val;
|
|
200
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
308
|
+
* @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
|
|
309
|
+
* a `DoublyLinkedListNode<E>` object.
|
|
310
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
|
|
311
|
+
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
|
|
312
|
+
*/
|
|
201
313
|
delete(valOrNode) {
|
|
202
314
|
let node;
|
|
203
315
|
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
@@ -224,6 +336,10 @@ class DoublyLinkedList {
|
|
|
224
336
|
}
|
|
225
337
|
return false;
|
|
226
338
|
}
|
|
339
|
+
/**
|
|
340
|
+
* The `toArray` function converts a linked list into an array.
|
|
341
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
342
|
+
*/
|
|
227
343
|
toArray() {
|
|
228
344
|
const array = [];
|
|
229
345
|
let current = this.head;
|
|
@@ -233,14 +349,28 @@ class DoublyLinkedList {
|
|
|
233
349
|
}
|
|
234
350
|
return array;
|
|
235
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
354
|
+
* @returns A boolean value is being returned.
|
|
355
|
+
*/
|
|
236
356
|
isEmpty() {
|
|
237
357
|
return this.length === 0;
|
|
238
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
361
|
+
*/
|
|
239
362
|
clear() {
|
|
240
363
|
this._head = null;
|
|
241
364
|
this._tail = null;
|
|
242
365
|
this._length = 0;
|
|
243
366
|
}
|
|
367
|
+
/**
|
|
368
|
+
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
369
|
+
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
370
|
+
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
371
|
+
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
372
|
+
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
373
|
+
*/
|
|
244
374
|
find(callback) {
|
|
245
375
|
let current = this.head;
|
|
246
376
|
while (current) {
|
|
@@ -251,6 +381,13 @@ class DoublyLinkedList {
|
|
|
251
381
|
}
|
|
252
382
|
return null;
|
|
253
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
386
|
+
* @param {E} val - The parameter `val` is of type `E`, which means it can be any data type. It represents the value
|
|
387
|
+
* that we are searching for in the linked list.
|
|
388
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
|
|
389
|
+
* list. If the value is not found, it returns -1.
|
|
390
|
+
*/
|
|
254
391
|
indexOf(val) {
|
|
255
392
|
let index = 0;
|
|
256
393
|
let current = this.head;
|
|
@@ -263,6 +400,14 @@ class DoublyLinkedList {
|
|
|
263
400
|
}
|
|
264
401
|
return -1;
|
|
265
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last
|
|
405
|
+
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
406
|
+
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
407
|
+
* function is used to determine whether a given value satisfies a certain condition.
|
|
408
|
+
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
|
|
409
|
+
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
410
|
+
*/
|
|
266
411
|
findLast(callback) {
|
|
267
412
|
let current = this.tail;
|
|
268
413
|
while (current) {
|
|
@@ -273,6 +418,10 @@ class DoublyLinkedList {
|
|
|
273
418
|
}
|
|
274
419
|
return null;
|
|
275
420
|
}
|
|
421
|
+
/**
|
|
422
|
+
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
|
|
423
|
+
* @returns The `toArrayReverse()` function returns an array of type `E[]`.
|
|
424
|
+
*/
|
|
276
425
|
toArrayReverse() {
|
|
277
426
|
const array = [];
|
|
278
427
|
let current = this.tail;
|
|
@@ -282,6 +431,9 @@ class DoublyLinkedList {
|
|
|
282
431
|
}
|
|
283
432
|
return array;
|
|
284
433
|
}
|
|
434
|
+
/**
|
|
435
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
436
|
+
*/
|
|
285
437
|
reverse() {
|
|
286
438
|
let current = this.head;
|
|
287
439
|
[this.head, this.tail] = [this.tail, this.head];
|
|
@@ -291,6 +443,12 @@ class DoublyLinkedList {
|
|
|
291
443
|
current = next;
|
|
292
444
|
}
|
|
293
445
|
}
|
|
446
|
+
/**
|
|
447
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
448
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
449
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
450
|
+
* current node in the linked list.
|
|
451
|
+
*/
|
|
294
452
|
forEach(callback) {
|
|
295
453
|
let current = this.head;
|
|
296
454
|
let index = 0;
|
|
@@ -300,6 +458,14 @@ class DoublyLinkedList {
|
|
|
300
458
|
index++;
|
|
301
459
|
}
|
|
302
460
|
}
|
|
461
|
+
/**
|
|
462
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
463
|
+
* DoublyLinkedList with the transformed values.
|
|
464
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
465
|
+
* the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
466
|
+
* DoublyLinkedList).
|
|
467
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
468
|
+
*/
|
|
303
469
|
map(callback) {
|
|
304
470
|
const mappedList = new DoublyLinkedList();
|
|
305
471
|
let current = this.head;
|
|
@@ -309,6 +475,13 @@ class DoublyLinkedList {
|
|
|
309
475
|
}
|
|
310
476
|
return mappedList;
|
|
311
477
|
}
|
|
478
|
+
/**
|
|
479
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
480
|
+
* elements that satisfy the given callback function.
|
|
481
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
482
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
483
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
484
|
+
*/
|
|
312
485
|
filter(callback) {
|
|
313
486
|
const filteredList = new DoublyLinkedList();
|
|
314
487
|
let current = this.head;
|
|
@@ -320,6 +493,16 @@ class DoublyLinkedList {
|
|
|
320
493
|
}
|
|
321
494
|
return filteredList;
|
|
322
495
|
}
|
|
496
|
+
/**
|
|
497
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
498
|
+
* single value.
|
|
499
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
500
|
+
* used to perform a specific operation on each element of the linked list.
|
|
501
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
502
|
+
* point for the reduction operation.
|
|
503
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
504
|
+
* elements in the linked list.
|
|
505
|
+
*/
|
|
323
506
|
reduce(callback, initialValue) {
|
|
324
507
|
let accumulator = initialValue;
|
|
325
508
|
let current = this.head;
|
|
@@ -329,6 +512,15 @@ class DoublyLinkedList {
|
|
|
329
512
|
}
|
|
330
513
|
return accumulator;
|
|
331
514
|
}
|
|
515
|
+
/**
|
|
516
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
517
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
518
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
519
|
+
* itself.
|
|
520
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
521
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
522
|
+
* existing value or node is not found in the doubly linked list.
|
|
523
|
+
*/
|
|
332
524
|
insertAfter(existingValueOrNode, newValue) {
|
|
333
525
|
let existingNode;
|
|
334
526
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
@@ -353,6 +545,16 @@ class DoublyLinkedList {
|
|
|
353
545
|
}
|
|
354
546
|
return false;
|
|
355
547
|
}
|
|
548
|
+
/**
|
|
549
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
550
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
551
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
552
|
+
* itself.
|
|
553
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
554
|
+
* list.
|
|
555
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
556
|
+
* insertion fails.
|
|
557
|
+
*/
|
|
356
558
|
insertBefore(existingValueOrNode, newValue) {
|
|
357
559
|
let existingNode;
|
|
358
560
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|