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