data-structure-typed 0.8.18 → 1.3.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/LICENSE +21 -0
- package/README.md +690 -2
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -18
- package/dist/data-structures/binary-tree/avl-tree.js +110 -37
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
- package/dist/data-structures/binary-tree/binary-tree.js +27 -979
- package/dist/data-structures/binary-tree/bst.d.ts +118 -28
- package/dist/data-structures/binary-tree/bst.js +162 -124
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
- package/dist/data-structures/binary-tree/segment-tree.js +80 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
- package/dist/data-structures/graph/abstract-graph.js +365 -92
- package/dist/data-structures/graph/directed-graph.d.ts +175 -26
- package/dist/data-structures/graph/directed-graph.js +249 -95
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -8
- package/dist/data-structures/graph/undirected-graph.js +154 -44
- package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
- package/dist/data-structures/hash/coordinate-map.js +44 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +34 -0
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -51
- package/dist/data-structures/heap/heap.js +106 -63
- package/dist/data-structures/heap/max-heap.d.ts +13 -4
- package/dist/data-structures/heap/max-heap.js +10 -2
- package/dist/data-structures/heap/min-heap.d.ts +14 -4
- package/dist/data-structures/heap/min-heap.js +11 -2
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -319
- package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
- package/dist/data-structures/matrix/matrix2d.js +91 -8
- package/dist/data-structures/matrix/navigator.d.ts +37 -16
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +156 -29
- package/dist/data-structures/matrix/vector2d.js +184 -55
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
- package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
- package/dist/data-structures/priority-queue/priority-queue.js +219 -75
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +151 -7
- package/dist/data-structures/queue/queue.d.ts +68 -42
- package/dist/data-structures/queue/queue.js +95 -51
- package/dist/data-structures/stack/stack.d.ts +30 -36
- package/dist/data-structures/stack/stack.js +31 -37
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/{types/utils.js → data-structures/tree/tree.js} +26 -19
- package/dist/data-structures/trie/trie.d.ts +39 -6
- package/dist/data-structures/trie/trie.js +81 -12
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-binary-tree.js +2 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/abstract-graph.js +2 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/avl-tree.js +2 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/bst.js +2 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/dist/interfaces/directed-graph.js +2 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/index.js +31 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/rb-tree.js +2 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +13 -7
- package/dist/types/data-structures/index.js +31 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +19 -0
- package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
- package/package.json +106 -55
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.d.ts +0 -122
- package/dist/utils.js +0 -569
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -232
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1088
- package/src/data-structures/binary-tree/bst.ts +0 -404
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -164
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -789
- package/src/data-structures/graph/directed-graph.ts +0 -322
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -154
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -136
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -22
- package/src/data-structures/heap/min-heap.ts +0 -24
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -99
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/priority-queue.ts +0 -208
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -123
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -104
- package/src/data-structures/trampoline.ts +0 -91
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -153
- package/src/index.ts +0 -1
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +0 -158
- package/src/utils.ts +0 -605
- package/tsconfig.json +0 -53
- /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
- /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
- /package/dist/{types/types → utils}/index.d.ts +0 -0
|
@@ -1,259 +1,526 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby
|
|
3
|
-
// 尾部插入 inject, snoc Append offerLast push push_back append push array_push push
|
|
4
|
-
// 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift
|
|
5
|
-
// 尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop
|
|
6
|
-
// 头部删除 pop Delete_First pollFirst shift pop_front popleft shift array_shift shift
|
|
7
|
-
// 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
|
|
8
|
-
// 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
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
|
+
*/
|
|
11
11
|
class DoublyLinkedListNode {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes the value, next, and previous properties of an object.
|
|
14
|
+
* @param {T} 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 "T".
|
|
16
|
+
*/
|
|
17
|
+
constructor(val) {
|
|
18
|
+
this._val = val;
|
|
19
|
+
this._next = null;
|
|
20
|
+
this._prev = null;
|
|
21
|
+
}
|
|
22
|
+
get val() {
|
|
23
|
+
return this._val;
|
|
24
|
+
}
|
|
25
|
+
set val(value) {
|
|
26
|
+
this._val = value;
|
|
27
|
+
}
|
|
28
|
+
get next() {
|
|
29
|
+
return this._next;
|
|
30
|
+
}
|
|
31
|
+
set next(value) {
|
|
32
|
+
this._next = value;
|
|
33
|
+
}
|
|
34
|
+
get prev() {
|
|
35
|
+
return this._prev;
|
|
36
|
+
}
|
|
37
|
+
set prev(value) {
|
|
38
|
+
this._prev = value;
|
|
16
39
|
}
|
|
17
40
|
}
|
|
18
41
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
19
42
|
class DoublyLinkedList {
|
|
43
|
+
/**
|
|
44
|
+
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
45
|
+
*/
|
|
20
46
|
constructor() {
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
|
|
47
|
+
this._head = null;
|
|
48
|
+
this._tail = null;
|
|
49
|
+
this._length = 0;
|
|
50
|
+
}
|
|
51
|
+
get head() {
|
|
52
|
+
return this._head;
|
|
53
|
+
}
|
|
54
|
+
set head(value) {
|
|
55
|
+
this._head = value;
|
|
25
56
|
}
|
|
26
|
-
get
|
|
27
|
-
return this.
|
|
57
|
+
get tail() {
|
|
58
|
+
return this._tail;
|
|
28
59
|
}
|
|
29
|
-
set
|
|
30
|
-
this.
|
|
60
|
+
set tail(value) {
|
|
61
|
+
this._tail = value;
|
|
62
|
+
}
|
|
63
|
+
get length() {
|
|
64
|
+
return this._length;
|
|
31
65
|
}
|
|
32
66
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
67
|
+
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
68
|
+
* given array.
|
|
69
|
+
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
|
70
|
+
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
35
71
|
*/
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this._last = newNode;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
if (this._first)
|
|
44
|
-
this._first.prev = newNode;
|
|
45
|
-
newNode.next = this._first;
|
|
46
|
-
this._first = newNode;
|
|
72
|
+
static fromArray(data) {
|
|
73
|
+
const doublyLinkedList = new DoublyLinkedList();
|
|
74
|
+
for (const item of data) {
|
|
75
|
+
doublyLinkedList.push(item);
|
|
47
76
|
}
|
|
48
|
-
|
|
49
|
-
return true;
|
|
77
|
+
return doublyLinkedList;
|
|
50
78
|
}
|
|
51
79
|
/**
|
|
52
|
-
*
|
|
53
|
-
* @param val
|
|
80
|
+
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
81
|
+
* @param {T} val - The value to be added to the linked list.
|
|
54
82
|
*/
|
|
55
|
-
|
|
83
|
+
push(val) {
|
|
56
84
|
const newNode = new DoublyLinkedListNode(val);
|
|
57
|
-
if (this.
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
85
|
+
if (!this.head) {
|
|
86
|
+
this.head = newNode;
|
|
87
|
+
this.tail = newNode;
|
|
60
88
|
}
|
|
61
89
|
else {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
this._last = newNode;
|
|
66
|
-
}
|
|
67
|
-
this._size++;
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
peekFirst(by) {
|
|
71
|
-
var _a, _b, _c, _d, _e;
|
|
72
|
-
switch (by) {
|
|
73
|
-
case 'node':
|
|
74
|
-
return (_a = this._first) !== null && _a !== void 0 ? _a : null;
|
|
75
|
-
case 'val':
|
|
76
|
-
return (_c = (_b = this._first) === null || _b === void 0 ? void 0 : _b.val) !== null && _c !== void 0 ? _c : null;
|
|
77
|
-
default:
|
|
78
|
-
return (_e = (_d = this._first) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
peekLast(by = 'val') {
|
|
82
|
-
var _a, _b, _c, _d, _e;
|
|
83
|
-
switch (by) {
|
|
84
|
-
case 'node':
|
|
85
|
-
return (_a = this._last) !== null && _a !== void 0 ? _a : null;
|
|
86
|
-
case 'val':
|
|
87
|
-
return (_c = (_b = this._last) === null || _b === void 0 ? void 0 : _b.val) !== null && _c !== void 0 ? _c : null;
|
|
88
|
-
default:
|
|
89
|
-
return (_e = (_d = this._last) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
|
|
90
|
+
newNode.prev = this.tail;
|
|
91
|
+
this.tail.next = newNode;
|
|
92
|
+
this.tail = newNode;
|
|
90
93
|
}
|
|
94
|
+
this._length++;
|
|
91
95
|
}
|
|
92
96
|
/**
|
|
93
|
-
*
|
|
97
|
+
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
98
|
+
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
99
|
+
* list is empty, it returns null.
|
|
94
100
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (this._size === 0)
|
|
101
|
+
pop() {
|
|
102
|
+
if (!this.tail)
|
|
98
103
|
return null;
|
|
99
|
-
const
|
|
100
|
-
if (this.
|
|
101
|
-
this.
|
|
102
|
-
this.
|
|
104
|
+
const removedNode = this.tail;
|
|
105
|
+
if (this.head === this.tail) {
|
|
106
|
+
this.head = null;
|
|
107
|
+
this.tail = null;
|
|
103
108
|
}
|
|
104
109
|
else {
|
|
105
|
-
this.
|
|
106
|
-
|
|
107
|
-
this._first.prev = null;
|
|
108
|
-
if (oldHead)
|
|
109
|
-
oldHead.next = null;
|
|
110
|
-
}
|
|
111
|
-
this._size--;
|
|
112
|
-
switch (by) {
|
|
113
|
-
case 'node':
|
|
114
|
-
return oldHead !== null && oldHead !== void 0 ? oldHead : null;
|
|
115
|
-
case 'val':
|
|
116
|
-
return (_b = oldHead === null || oldHead === void 0 ? void 0 : oldHead.val) !== null && _b !== void 0 ? _b : null;
|
|
117
|
-
default:
|
|
118
|
-
return (_c = oldHead === null || oldHead === void 0 ? void 0 : oldHead.val) !== null && _c !== void 0 ? _c : null;
|
|
110
|
+
this.tail = removedNode.prev;
|
|
111
|
+
this.tail.next = null;
|
|
119
112
|
}
|
|
113
|
+
this._length--;
|
|
114
|
+
return removedNode.val;
|
|
120
115
|
}
|
|
121
116
|
/**
|
|
122
|
-
*
|
|
117
|
+
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
118
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
119
|
+
* list.
|
|
123
120
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (this._size === 0)
|
|
121
|
+
shift() {
|
|
122
|
+
if (!this.head)
|
|
127
123
|
return null;
|
|
128
|
-
const
|
|
129
|
-
if (this.
|
|
130
|
-
this.
|
|
131
|
-
this.
|
|
124
|
+
const removedNode = this.head;
|
|
125
|
+
if (this.head === this.tail) {
|
|
126
|
+
this.head = null;
|
|
127
|
+
this.tail = null;
|
|
132
128
|
}
|
|
133
129
|
else {
|
|
134
|
-
this.
|
|
135
|
-
|
|
136
|
-
this._last.next = null;
|
|
137
|
-
if (polled)
|
|
138
|
-
polled.prev = null;
|
|
139
|
-
}
|
|
140
|
-
this._size--;
|
|
141
|
-
switch (by) {
|
|
142
|
-
case 'node':
|
|
143
|
-
return polled !== null && polled !== void 0 ? polled : null;
|
|
144
|
-
case 'val':
|
|
145
|
-
return (_b = polled === null || polled === void 0 ? void 0 : polled.val) !== null && _b !== void 0 ? _b : null;
|
|
146
|
-
default:
|
|
147
|
-
return (_c = polled === null || polled === void 0 ? void 0 : polled.val) !== null && _c !== void 0 ? _c : null;
|
|
130
|
+
this.head = removedNode.next;
|
|
131
|
+
this.head.prev = null;
|
|
148
132
|
}
|
|
133
|
+
this._length--;
|
|
134
|
+
return removedNode.val;
|
|
149
135
|
}
|
|
150
136
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* @param index Index of the node to be retrieved
|
|
155
|
-
* @param by Return value type
|
|
137
|
+
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
138
|
+
* @param {T} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
|
139
|
+
* doubly linked list.
|
|
156
140
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (index <= this._size / 2) {
|
|
163
|
-
count = 0;
|
|
164
|
-
current = this._first;
|
|
165
|
-
while (count !== index) {
|
|
166
|
-
current = current === null || current === void 0 ? void 0 : current.next;
|
|
167
|
-
count++;
|
|
168
|
-
}
|
|
141
|
+
unshift(val) {
|
|
142
|
+
const newNode = new DoublyLinkedListNode(val);
|
|
143
|
+
if (!this.head) {
|
|
144
|
+
this.head = newNode;
|
|
145
|
+
this.tail = newNode;
|
|
169
146
|
}
|
|
170
147
|
else {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
current = current === null || current === void 0 ? void 0 : current.prev;
|
|
175
|
-
count--;
|
|
176
|
-
}
|
|
148
|
+
newNode.next = this.head;
|
|
149
|
+
this.head.prev = newNode;
|
|
150
|
+
this.head = newNode;
|
|
177
151
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
152
|
+
this._length++;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
156
|
+
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
157
|
+
* retrieve from the list.
|
|
158
|
+
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
159
|
+
* or the linked list is empty, it will return null.
|
|
160
|
+
*/
|
|
161
|
+
getAt(index) {
|
|
162
|
+
if (index < 0 || index >= this.length)
|
|
163
|
+
return null;
|
|
164
|
+
let current = this.head;
|
|
165
|
+
for (let i = 0; i < index; i++) {
|
|
166
|
+
current = current.next;
|
|
185
167
|
}
|
|
168
|
+
return current.val;
|
|
186
169
|
}
|
|
187
170
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
* @
|
|
171
|
+
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
|
|
172
|
+
* range.
|
|
173
|
+
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
174
|
+
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
|
175
|
+
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<T>` object if the index is within the
|
|
176
|
+
* valid range of the linked list, otherwise it returns `null`.
|
|
193
177
|
*/
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
178
|
+
getNodeAt(index) {
|
|
179
|
+
if (index < 0 || index >= this.length)
|
|
180
|
+
return null;
|
|
181
|
+
let current = this.head;
|
|
182
|
+
for (let i = 0; i < index; i++) {
|
|
183
|
+
current = current.next;
|
|
199
184
|
}
|
|
200
|
-
return
|
|
185
|
+
return current;
|
|
201
186
|
}
|
|
202
|
-
|
|
203
|
-
|
|
187
|
+
/**
|
|
188
|
+
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
189
|
+
* node if found, otherwise it returns null.
|
|
190
|
+
* @param {T} val - The `val` parameter is the value that we want to search for in the doubly linked list.
|
|
191
|
+
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<T>` if a node with the specified value `val`
|
|
192
|
+
* is found in the linked list. If no such node is found, it returns `null`.
|
|
193
|
+
*/
|
|
194
|
+
findNode(val) {
|
|
195
|
+
let current = this.head;
|
|
196
|
+
while (current) {
|
|
197
|
+
if (current.val === val) {
|
|
198
|
+
return current;
|
|
199
|
+
}
|
|
200
|
+
current = current.next;
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
204
203
|
}
|
|
205
|
-
// --- start extra methods ---
|
|
206
204
|
/**
|
|
207
|
-
*
|
|
208
|
-
* @param index
|
|
209
|
-
*
|
|
205
|
+
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
206
|
+
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
207
|
+
* DoublyLinkedList. It is of type number.
|
|
208
|
+
* @param {T} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
209
|
+
* specified index.
|
|
210
|
+
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
211
|
+
* if the index is out of bounds.
|
|
210
212
|
*/
|
|
211
|
-
|
|
212
|
-
if (index < 0 || index > this.
|
|
213
|
+
insertAt(index, val) {
|
|
214
|
+
if (index < 0 || index > this.length)
|
|
213
215
|
return false;
|
|
214
|
-
if (index === 0)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
216
|
+
if (index === 0) {
|
|
217
|
+
this.unshift(val);
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
if (index === this.length) {
|
|
221
|
+
this.push(val);
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
218
224
|
const newNode = new DoublyLinkedListNode(val);
|
|
219
|
-
const prevNode = this.
|
|
220
|
-
const nextNode = prevNode
|
|
221
|
-
if (prevNode)
|
|
222
|
-
prevNode.next = newNode;
|
|
225
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
226
|
+
const nextNode = prevNode.next;
|
|
223
227
|
newNode.prev = prevNode;
|
|
224
|
-
newNode.next = nextNode
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
this.
|
|
228
|
+
newNode.next = nextNode;
|
|
229
|
+
prevNode.next = newNode;
|
|
230
|
+
nextNode.prev = newNode;
|
|
231
|
+
this._length++;
|
|
228
232
|
return true;
|
|
229
233
|
}
|
|
230
234
|
/**
|
|
231
|
-
*
|
|
232
|
-
* @param index
|
|
235
|
+
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
236
|
+
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
237
|
+
* data structure. It is of type number.
|
|
238
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
239
|
+
* bounds.
|
|
233
240
|
*/
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (index < 0 || index > this._size - 1)
|
|
241
|
+
deleteAt(index) {
|
|
242
|
+
if (index < 0 || index >= this.length)
|
|
237
243
|
return null;
|
|
238
|
-
|
|
239
|
-
return this.
|
|
240
|
-
|
|
241
|
-
return
|
|
244
|
+
if (index === 0)
|
|
245
|
+
return this.shift();
|
|
246
|
+
if (index === this.length - 1)
|
|
247
|
+
return this.pop();
|
|
248
|
+
const removedNode = this.getNodeAt(index);
|
|
249
|
+
const prevNode = removedNode.prev;
|
|
250
|
+
const nextNode = removedNode.next;
|
|
251
|
+
prevNode.next = nextNode;
|
|
252
|
+
nextNode.prev = prevNode;
|
|
253
|
+
this._length--;
|
|
254
|
+
return removedNode.val;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
258
|
+
* @param {T | DoublyLinkedListNode<T>} valOrNode - The `valOrNode` parameter can accept either a value of type `T` or
|
|
259
|
+
* a `DoublyLinkedListNode<T>` object.
|
|
260
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
|
|
261
|
+
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
|
|
262
|
+
*/
|
|
263
|
+
delete(valOrNode) {
|
|
264
|
+
let node;
|
|
265
|
+
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
266
|
+
node = valOrNode;
|
|
267
|
+
}
|
|
242
268
|
else {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
269
|
+
node = this.findNode(valOrNode);
|
|
270
|
+
}
|
|
271
|
+
if (node) {
|
|
272
|
+
if (node === this.head) {
|
|
273
|
+
this.shift();
|
|
274
|
+
}
|
|
275
|
+
else if (node === this.tail) {
|
|
276
|
+
this.pop();
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
const prevNode = node.prev;
|
|
280
|
+
const nextNode = node.next;
|
|
281
|
+
prevNode.next = nextNode;
|
|
249
282
|
nextNode.prev = prevNode;
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
283
|
+
this._length--;
|
|
284
|
+
}
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* The `toArray` function converts a linked list into an array.
|
|
291
|
+
* @returns The `toArray()` method is returning an array of type `T[]`.
|
|
292
|
+
*/
|
|
293
|
+
toArray() {
|
|
294
|
+
const array = [];
|
|
295
|
+
let current = this.head;
|
|
296
|
+
while (current) {
|
|
297
|
+
array.push(current.val);
|
|
298
|
+
current = current.next;
|
|
299
|
+
}
|
|
300
|
+
return array;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
304
|
+
*/
|
|
305
|
+
clear() {
|
|
306
|
+
this._head = null;
|
|
307
|
+
this._tail = null;
|
|
308
|
+
this._length = 0;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
312
|
+
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
313
|
+
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
314
|
+
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
315
|
+
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
316
|
+
*/
|
|
317
|
+
find(callback) {
|
|
318
|
+
let current = this.head;
|
|
319
|
+
while (current) {
|
|
320
|
+
if (callback(current.val)) {
|
|
321
|
+
return current.val;
|
|
322
|
+
}
|
|
323
|
+
current = current.next;
|
|
324
|
+
}
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
329
|
+
* @param {T} val - The parameter `val` is of type `T`, which means it can be any data type. It represents the value
|
|
330
|
+
* that we are searching for in the linked list.
|
|
331
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
|
|
332
|
+
* list. If the value is not found, it returns -1.
|
|
333
|
+
*/
|
|
334
|
+
indexOf(val) {
|
|
335
|
+
let index = 0;
|
|
336
|
+
let current = this.head;
|
|
337
|
+
while (current) {
|
|
338
|
+
if (current.val === val) {
|
|
339
|
+
return index;
|
|
340
|
+
}
|
|
341
|
+
index++;
|
|
342
|
+
current = current.next;
|
|
343
|
+
}
|
|
344
|
+
return -1;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last
|
|
348
|
+
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
349
|
+
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
350
|
+
* function is used to determine whether a given value satisfies a certain condition.
|
|
351
|
+
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
|
|
352
|
+
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
353
|
+
*/
|
|
354
|
+
findLast(callback) {
|
|
355
|
+
let current = this.tail;
|
|
356
|
+
while (current) {
|
|
357
|
+
if (callback(current.val)) {
|
|
358
|
+
return current.val;
|
|
359
|
+
}
|
|
360
|
+
current = current.prev;
|
|
256
361
|
}
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
|
|
366
|
+
* @returns The `toArrayReverse()` function returns an array of type `T[]`.
|
|
367
|
+
*/
|
|
368
|
+
toArrayReverse() {
|
|
369
|
+
const array = [];
|
|
370
|
+
let current = this.tail;
|
|
371
|
+
while (current) {
|
|
372
|
+
array.push(current.val);
|
|
373
|
+
current = current.prev;
|
|
374
|
+
}
|
|
375
|
+
return array;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
379
|
+
*/
|
|
380
|
+
reverse() {
|
|
381
|
+
let current = this.head;
|
|
382
|
+
[this.head, this.tail] = [this.tail, this.head];
|
|
383
|
+
while (current) {
|
|
384
|
+
const next = current.next;
|
|
385
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
386
|
+
current = next;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
391
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
392
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
393
|
+
* current node in the linked list.
|
|
394
|
+
*/
|
|
395
|
+
forEach(callback) {
|
|
396
|
+
let current = this.head;
|
|
397
|
+
let index = 0;
|
|
398
|
+
while (current) {
|
|
399
|
+
callback(current.val, index);
|
|
400
|
+
current = current.next;
|
|
401
|
+
index++;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
406
|
+
* DoublyLinkedList with the transformed values.
|
|
407
|
+
* @param callback - The callback parameter is a function that takes a value of type T (the type of values stored in
|
|
408
|
+
* the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
409
|
+
* DoublyLinkedList).
|
|
410
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
411
|
+
*/
|
|
412
|
+
map(callback) {
|
|
413
|
+
const mappedList = new DoublyLinkedList();
|
|
414
|
+
let current = this.head;
|
|
415
|
+
while (current) {
|
|
416
|
+
mappedList.push(callback(current.val));
|
|
417
|
+
current = current.next;
|
|
418
|
+
}
|
|
419
|
+
return mappedList;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
423
|
+
* elements that satisfy the given callback function.
|
|
424
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `T` and returns a boolean value.
|
|
425
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
426
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
427
|
+
*/
|
|
428
|
+
filter(callback) {
|
|
429
|
+
const filteredList = new DoublyLinkedList();
|
|
430
|
+
let current = this.head;
|
|
431
|
+
while (current) {
|
|
432
|
+
if (callback(current.val)) {
|
|
433
|
+
filteredList.push(current.val);
|
|
434
|
+
}
|
|
435
|
+
current = current.next;
|
|
436
|
+
}
|
|
437
|
+
return filteredList;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
441
|
+
* single value.
|
|
442
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
443
|
+
* used to perform a specific operation on each element of the linked list.
|
|
444
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
445
|
+
* point for the reduction operation.
|
|
446
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
447
|
+
* elements in the linked list.
|
|
448
|
+
*/
|
|
449
|
+
reduce(callback, initialValue) {
|
|
450
|
+
let accumulator = initialValue;
|
|
451
|
+
let current = this.head;
|
|
452
|
+
while (current) {
|
|
453
|
+
accumulator = callback(accumulator, current.val);
|
|
454
|
+
current = current.next;
|
|
455
|
+
}
|
|
456
|
+
return accumulator;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
460
|
+
* @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
461
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
462
|
+
* itself.
|
|
463
|
+
* @param {T} newValue - The value that you want to insert into the doubly linked list.
|
|
464
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
465
|
+
* existing value or node is not found in the doubly linked list.
|
|
466
|
+
*/
|
|
467
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
468
|
+
let existingNode;
|
|
469
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
470
|
+
existingNode = existingValueOrNode;
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
existingNode = this.findNode(existingValueOrNode);
|
|
474
|
+
}
|
|
475
|
+
if (existingNode) {
|
|
476
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
477
|
+
newNode.next = existingNode.next;
|
|
478
|
+
if (existingNode.next) {
|
|
479
|
+
existingNode.next.prev = newNode;
|
|
480
|
+
}
|
|
481
|
+
newNode.prev = existingNode;
|
|
482
|
+
existingNode.next = newNode;
|
|
483
|
+
if (existingNode === this.tail) {
|
|
484
|
+
this.tail = newNode;
|
|
485
|
+
}
|
|
486
|
+
this._length++;
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
493
|
+
* @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
494
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
495
|
+
* itself.
|
|
496
|
+
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
497
|
+
* list.
|
|
498
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
499
|
+
* insertion fails.
|
|
500
|
+
*/
|
|
501
|
+
insertBefore(existingValueOrNode, newValue) {
|
|
502
|
+
let existingNode;
|
|
503
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
504
|
+
existingNode = existingValueOrNode;
|
|
505
|
+
}
|
|
506
|
+
else {
|
|
507
|
+
existingNode = this.findNode(existingValueOrNode);
|
|
508
|
+
}
|
|
509
|
+
if (existingNode) {
|
|
510
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
511
|
+
newNode.prev = existingNode.prev;
|
|
512
|
+
if (existingNode.prev) {
|
|
513
|
+
existingNode.prev.next = newNode;
|
|
514
|
+
}
|
|
515
|
+
newNode.next = existingNode;
|
|
516
|
+
existingNode.prev = newNode;
|
|
517
|
+
if (existingNode === this.head) {
|
|
518
|
+
this.head = newNode;
|
|
519
|
+
}
|
|
520
|
+
this._length++;
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
return false;
|
|
257
524
|
}
|
|
258
525
|
}
|
|
259
526
|
exports.DoublyLinkedList = DoublyLinkedList;
|