data-structure-typed 1.39.5 → 1.39.6
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/CHANGELOG.md +2 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +24 -24
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/cjs/data-structures/graph/directed-graph.js +15 -15
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/cjs/data-structures/graph/map-graph.js +13 -13
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/cjs/data-structures/graph/undirected-graph.js +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/cjs/data-structures/hash/hash-table.js +8 -8
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +54 -54
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/interfaces/graph.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +18 -18
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +26 -26
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/mjs/data-structures/graph/directed-graph.js +15 -15
- package/dist/mjs/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/mjs/data-structures/graph/map-graph.js +13 -13
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/mjs/data-structures/graph/undirected-graph.js +14 -14
- package/dist/mjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/mjs/data-structures/hash/hash-table.js +9 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +55 -55
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +53 -53
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/interfaces/graph.d.ts +2 -2
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +13 -13
- package/src/data-structures/binary-tree/binary-tree.ts +18 -18
- package/src/data-structures/binary-tree/bst.ts +16 -16
- package/src/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/data-structures/binary-tree/segment-tree.ts +15 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -18
- package/src/data-structures/graph/abstract-graph.ts +34 -34
- package/src/data-structures/graph/directed-graph.ts +16 -16
- package/src/data-structures/graph/map-graph.ts +13 -13
- package/src/data-structures/graph/undirected-graph.ts +15 -15
- package/src/data-structures/hash/hash-table.ts +9 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +61 -61
- package/src/data-structures/linked-list/singly-linked-list.ts +58 -58
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +2 -2
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/bst.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +99 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +22 -12
- package/test/unit/data-structures/graph/undirected-graph.test.ts +8 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +15 -15
- package/test/utils/big-o.ts +7 -7
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class HashTableNode<K, V> {
|
|
9
9
|
key: K;
|
|
10
|
-
|
|
10
|
+
value: V;
|
|
11
11
|
next: HashTableNode<K, V> | null;
|
|
12
|
-
constructor(key: K,
|
|
12
|
+
constructor(key: K, value: V);
|
|
13
13
|
}
|
|
14
14
|
import { HashFunction } from '../../types';
|
|
15
15
|
export declare class HashTable<K, V> {
|
|
@@ -31,12 +31,12 @@ export declare class HashTable<K, V> {
|
|
|
31
31
|
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
32
32
|
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
33
33
|
* table. It is of type K, which is a generic type representing the key's data type.
|
|
34
|
-
* @param {V}
|
|
34
|
+
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
35
35
|
* table.
|
|
36
36
|
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
37
37
|
* value.
|
|
38
38
|
*/
|
|
39
|
-
set(key: K,
|
|
39
|
+
set(key: K, value: V): void;
|
|
40
40
|
/**
|
|
41
41
|
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
42
42
|
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
@@ -10,11 +10,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.HashTable = exports.HashTableNode = void 0;
|
|
11
11
|
class HashTableNode {
|
|
12
12
|
key;
|
|
13
|
-
|
|
13
|
+
value;
|
|
14
14
|
next;
|
|
15
|
-
constructor(key,
|
|
15
|
+
constructor(key, value) {
|
|
16
16
|
this.key = key;
|
|
17
|
-
this.
|
|
17
|
+
this.value = value;
|
|
18
18
|
this.next = null;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -57,14 +57,14 @@ class HashTable {
|
|
|
57
57
|
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
58
58
|
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
59
59
|
* table. It is of type K, which is a generic type representing the key's data type.
|
|
60
|
-
* @param {V}
|
|
60
|
+
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
61
61
|
* table.
|
|
62
62
|
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
63
63
|
* value.
|
|
64
64
|
*/
|
|
65
|
-
set(key,
|
|
65
|
+
set(key, value) {
|
|
66
66
|
const index = this._hash(key);
|
|
67
|
-
const newNode = new HashTableNode(key,
|
|
67
|
+
const newNode = new HashTableNode(key, value);
|
|
68
68
|
if (!this._buckets[index]) {
|
|
69
69
|
this._buckets[index] = newNode;
|
|
70
70
|
}
|
|
@@ -74,7 +74,7 @@ class HashTable {
|
|
|
74
74
|
while (currentNode) {
|
|
75
75
|
if (currentNode.key === key) {
|
|
76
76
|
// If the key already exists, update the value
|
|
77
|
-
currentNode.
|
|
77
|
+
currentNode.value = value;
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
80
|
if (!currentNode.next) {
|
|
@@ -103,7 +103,7 @@ class HashTable {
|
|
|
103
103
|
let currentNode = this._buckets[index];
|
|
104
104
|
while (currentNode) {
|
|
105
105
|
if (currentNode.key === key) {
|
|
106
|
-
return currentNode.
|
|
106
|
+
return currentNode.value;
|
|
107
107
|
}
|
|
108
108
|
currentNode = currentNode.next;
|
|
109
109
|
}
|
|
@@ -231,7 +231,7 @@ class HashTable {
|
|
|
231
231
|
let currentNode = bucket;
|
|
232
232
|
while (currentNode) {
|
|
233
233
|
const newIndex = this._hash(currentNode.key);
|
|
234
|
-
const newNode = new HashTableNode(currentNode.key, currentNode.
|
|
234
|
+
const newNode = new HashTableNode(currentNode.key, currentNode.value);
|
|
235
235
|
if (!newBuckets[newIndex]) {
|
|
236
236
|
newBuckets[newIndex] = newNode;
|
|
237
237
|
}
|
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
export declare class DoublyLinkedListNode<E = any> {
|
|
9
9
|
/**
|
|
10
10
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
11
|
-
* @param {E}
|
|
11
|
+
* @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
|
|
12
12
|
* is defined as a generic type "E".
|
|
13
13
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
private
|
|
16
|
-
get
|
|
17
|
-
set
|
|
14
|
+
constructor(value: E);
|
|
15
|
+
private _value;
|
|
16
|
+
get value(): E;
|
|
17
|
+
set value(value: E);
|
|
18
18
|
private _next;
|
|
19
19
|
get next(): DoublyLinkedListNode<E> | null;
|
|
20
20
|
set next(value: DoublyLinkedListNode<E> | null);
|
|
@@ -45,23 +45,23 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
45
45
|
static fromArray<E>(data: E[]): DoublyLinkedList<E>;
|
|
46
46
|
/**
|
|
47
47
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
48
|
-
* @param {E}
|
|
48
|
+
* @param {E} value - The value to be added to the linked list.
|
|
49
49
|
*/
|
|
50
|
-
push(
|
|
50
|
+
push(value: E): void;
|
|
51
51
|
/**
|
|
52
52
|
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
53
|
-
* @param {E}
|
|
53
|
+
* @param {E} value - The value to be added to the linked list.
|
|
54
54
|
*/
|
|
55
|
-
addLast(
|
|
55
|
+
addLast(value: E): void;
|
|
56
56
|
/**
|
|
57
57
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
58
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
58
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
59
59
|
* list is empty, it returns null.
|
|
60
60
|
*/
|
|
61
61
|
pop(): E | undefined;
|
|
62
62
|
/**
|
|
63
63
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
64
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
64
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
65
65
|
* list is empty, it returns null.
|
|
66
66
|
*/
|
|
67
67
|
popLast(): E | undefined;
|
|
@@ -79,16 +79,16 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
79
79
|
popFirst(): E | undefined;
|
|
80
80
|
/**
|
|
81
81
|
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
82
|
-
* @param {E}
|
|
82
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
83
83
|
* doubly linked list.
|
|
84
84
|
*/
|
|
85
|
-
unshift(
|
|
85
|
+
unshift(value: E): void;
|
|
86
86
|
/**
|
|
87
87
|
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
88
|
-
* @param {E}
|
|
88
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
89
89
|
* doubly linked list.
|
|
90
90
|
*/
|
|
91
|
-
addFirst(
|
|
91
|
+
addFirst(value: E): void;
|
|
92
92
|
/**
|
|
93
93
|
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
94
94
|
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
@@ -119,21 +119,21 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
119
119
|
/**
|
|
120
120
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
121
121
|
* node if found, otherwise it returns null.
|
|
122
|
-
* @param {E}
|
|
123
|
-
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `
|
|
122
|
+
* @param {E} value - The `value` 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 `value`
|
|
124
124
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
125
125
|
*/
|
|
126
|
-
getNode(
|
|
126
|
+
getNode(value: E | null): DoublyLinkedListNode<E> | null;
|
|
127
127
|
/**
|
|
128
128
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
129
129
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
130
130
|
* DoublyLinkedList. It is of type number.
|
|
131
|
-
* @param {E}
|
|
131
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
132
132
|
* specified index.
|
|
133
133
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
134
134
|
* if the index is out of bounds.
|
|
135
135
|
*/
|
|
136
|
-
insertAt(index: number,
|
|
136
|
+
insertAt(index: number, value: E): boolean;
|
|
137
137
|
/**
|
|
138
138
|
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
139
139
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -182,15 +182,15 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
182
182
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
183
183
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
184
184
|
*/
|
|
185
|
-
find(callback: (
|
|
185
|
+
find(callback: (value: E) => boolean): E | null;
|
|
186
186
|
/**
|
|
187
187
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
188
|
-
* @param {E}
|
|
188
|
+
* @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
|
|
189
189
|
* that we are searching for in the linked list.
|
|
190
|
-
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `
|
|
190
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
|
|
191
191
|
* list. If the value is not found, it returns -1.
|
|
192
192
|
*/
|
|
193
|
-
indexOf(
|
|
193
|
+
indexOf(value: E): number;
|
|
194
194
|
/**
|
|
195
195
|
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
196
196
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
@@ -199,7 +199,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
199
199
|
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
200
200
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
201
201
|
*/
|
|
202
|
-
findBackward(callback: (
|
|
202
|
+
findBackward(callback: (value: E) => boolean): E | null;
|
|
203
203
|
/**
|
|
204
204
|
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
205
205
|
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
@@ -211,11 +211,11 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
211
211
|
reverse(): void;
|
|
212
212
|
/**
|
|
213
213
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
214
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
214
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
215
215
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
216
216
|
* current node in the linked list.
|
|
217
217
|
*/
|
|
218
|
-
forEach(callback: (
|
|
218
|
+
forEach(callback: (value: E, index: number) => void): void;
|
|
219
219
|
/**
|
|
220
220
|
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
221
221
|
* DoublyLinkedList with the transformed values.
|
|
@@ -224,7 +224,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
224
224
|
* DoublyLinkedList).
|
|
225
225
|
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
226
226
|
*/
|
|
227
|
-
map<U>(callback: (
|
|
227
|
+
map<U>(callback: (value: E) => U): DoublyLinkedList<U>;
|
|
228
228
|
/**
|
|
229
229
|
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
230
230
|
* elements that satisfy the given callback function.
|
|
@@ -232,18 +232,18 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
232
232
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
233
233
|
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
234
234
|
*/
|
|
235
|
-
filter(callback: (
|
|
235
|
+
filter(callback: (value: E) => boolean): DoublyLinkedList<E>;
|
|
236
236
|
/**
|
|
237
237
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
238
238
|
* single value.
|
|
239
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
239
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
240
240
|
* used to perform a specific operation on each element of the linked list.
|
|
241
241
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
242
242
|
* point for the reduction operation.
|
|
243
243
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
244
244
|
* elements in the linked list.
|
|
245
245
|
*/
|
|
246
|
-
reduce<U>(callback: (accumulator: U,
|
|
246
|
+
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U;
|
|
247
247
|
/**
|
|
248
248
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
249
249
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -11,20 +11,20 @@ exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
|
11
11
|
class DoublyLinkedListNode {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
14
|
-
* @param {E}
|
|
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
15
|
* is defined as a generic type "E".
|
|
16
16
|
*/
|
|
17
|
-
constructor(
|
|
18
|
-
this.
|
|
17
|
+
constructor(value) {
|
|
18
|
+
this._value = value;
|
|
19
19
|
this._next = null;
|
|
20
20
|
this._prev = null;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
get
|
|
24
|
-
return this.
|
|
22
|
+
_value;
|
|
23
|
+
get value() {
|
|
24
|
+
return this._value;
|
|
25
25
|
}
|
|
26
|
-
set
|
|
27
|
-
this.
|
|
26
|
+
set value(value) {
|
|
27
|
+
this._value = value;
|
|
28
28
|
}
|
|
29
29
|
_next;
|
|
30
30
|
get next() {
|
|
@@ -87,10 +87,10 @@ class DoublyLinkedList {
|
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
89
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
90
|
-
* @param {E}
|
|
90
|
+
* @param {E} value - The value to be added to the linked list.
|
|
91
91
|
*/
|
|
92
|
-
push(
|
|
93
|
-
const newNode = new DoublyLinkedListNode(
|
|
92
|
+
push(value) {
|
|
93
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
94
94
|
if (!this.head) {
|
|
95
95
|
this.head = newNode;
|
|
96
96
|
this.tail = newNode;
|
|
@@ -104,14 +104,14 @@ class DoublyLinkedList {
|
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
107
|
-
* @param {E}
|
|
107
|
+
* @param {E} value - The value to be added to the linked list.
|
|
108
108
|
*/
|
|
109
|
-
addLast(
|
|
110
|
-
this.push(
|
|
109
|
+
addLast(value) {
|
|
110
|
+
this.push(value);
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
113
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
114
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
114
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
115
115
|
* list is empty, it returns null.
|
|
116
116
|
*/
|
|
117
117
|
pop() {
|
|
@@ -127,11 +127,11 @@ class DoublyLinkedList {
|
|
|
127
127
|
this.tail.next = null;
|
|
128
128
|
}
|
|
129
129
|
this._length--;
|
|
130
|
-
return removedNode.
|
|
130
|
+
return removedNode.value;
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
133
133
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
134
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
134
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
135
135
|
* list is empty, it returns null.
|
|
136
136
|
*/
|
|
137
137
|
popLast() {
|
|
@@ -155,7 +155,7 @@ class DoublyLinkedList {
|
|
|
155
155
|
this.head.prev = null;
|
|
156
156
|
}
|
|
157
157
|
this._length--;
|
|
158
|
-
return removedNode.
|
|
158
|
+
return removedNode.value;
|
|
159
159
|
}
|
|
160
160
|
/**
|
|
161
161
|
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
@@ -167,11 +167,11 @@ class DoublyLinkedList {
|
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
169
169
|
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
170
|
-
* @param {E}
|
|
170
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
171
171
|
* doubly linked list.
|
|
172
172
|
*/
|
|
173
|
-
unshift(
|
|
174
|
-
const newNode = new DoublyLinkedListNode(
|
|
173
|
+
unshift(value) {
|
|
174
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
175
175
|
if (!this.head) {
|
|
176
176
|
this.head = newNode;
|
|
177
177
|
this.tail = newNode;
|
|
@@ -185,25 +185,25 @@ class DoublyLinkedList {
|
|
|
185
185
|
}
|
|
186
186
|
/**
|
|
187
187
|
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
188
|
-
* @param {E}
|
|
188
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
189
189
|
* doubly linked list.
|
|
190
190
|
*/
|
|
191
|
-
addFirst(
|
|
192
|
-
this.unshift(
|
|
191
|
+
addFirst(value) {
|
|
192
|
+
this.unshift(value);
|
|
193
193
|
}
|
|
194
194
|
/**
|
|
195
195
|
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
196
196
|
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
197
197
|
*/
|
|
198
198
|
getFirst() {
|
|
199
|
-
return this.head?.
|
|
199
|
+
return this.head?.value;
|
|
200
200
|
}
|
|
201
201
|
/**
|
|
202
202
|
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
203
203
|
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
204
204
|
*/
|
|
205
205
|
getLast() {
|
|
206
|
-
return this.tail?.
|
|
206
|
+
return this.tail?.value;
|
|
207
207
|
}
|
|
208
208
|
/**
|
|
209
209
|
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
@@ -219,7 +219,7 @@ class DoublyLinkedList {
|
|
|
219
219
|
for (let i = 0; i < index; i++) {
|
|
220
220
|
current = current.next;
|
|
221
221
|
}
|
|
222
|
-
return current.
|
|
222
|
+
return current.value;
|
|
223
223
|
}
|
|
224
224
|
/**
|
|
225
225
|
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
|
|
@@ -241,14 +241,14 @@ class DoublyLinkedList {
|
|
|
241
241
|
/**
|
|
242
242
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
243
243
|
* node if found, otherwise it returns null.
|
|
244
|
-
* @param {E}
|
|
245
|
-
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `
|
|
244
|
+
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
|
245
|
+
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
|
246
246
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
247
247
|
*/
|
|
248
|
-
getNode(
|
|
248
|
+
getNode(value) {
|
|
249
249
|
let current = this.head;
|
|
250
250
|
while (current) {
|
|
251
|
-
if (current.
|
|
251
|
+
if (current.value === value) {
|
|
252
252
|
return current;
|
|
253
253
|
}
|
|
254
254
|
current = current.next;
|
|
@@ -259,23 +259,23 @@ class DoublyLinkedList {
|
|
|
259
259
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
260
260
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
261
261
|
* DoublyLinkedList. It is of type number.
|
|
262
|
-
* @param {E}
|
|
262
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
263
263
|
* specified index.
|
|
264
264
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
265
265
|
* if the index is out of bounds.
|
|
266
266
|
*/
|
|
267
|
-
insertAt(index,
|
|
267
|
+
insertAt(index, value) {
|
|
268
268
|
if (index < 0 || index > this.length)
|
|
269
269
|
return false;
|
|
270
270
|
if (index === 0) {
|
|
271
|
-
this.unshift(
|
|
271
|
+
this.unshift(value);
|
|
272
272
|
return true;
|
|
273
273
|
}
|
|
274
274
|
if (index === this.length) {
|
|
275
|
-
this.push(
|
|
275
|
+
this.push(value);
|
|
276
276
|
return true;
|
|
277
277
|
}
|
|
278
|
-
const newNode = new DoublyLinkedListNode(
|
|
278
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
279
279
|
const prevNode = this.getNodeAt(index - 1);
|
|
280
280
|
const nextNode = prevNode.next;
|
|
281
281
|
newNode.prev = prevNode;
|
|
@@ -339,7 +339,7 @@ class DoublyLinkedList {
|
|
|
339
339
|
prevNode.next = nextNode;
|
|
340
340
|
nextNode.prev = prevNode;
|
|
341
341
|
this._length--;
|
|
342
|
-
return removedNode.
|
|
342
|
+
return removedNode.value;
|
|
343
343
|
}
|
|
344
344
|
/**
|
|
345
345
|
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
@@ -382,7 +382,7 @@ class DoublyLinkedList {
|
|
|
382
382
|
const array = [];
|
|
383
383
|
let current = this.head;
|
|
384
384
|
while (current) {
|
|
385
|
-
array.push(current.
|
|
385
|
+
array.push(current.value);
|
|
386
386
|
current = current.next;
|
|
387
387
|
}
|
|
388
388
|
return array;
|
|
@@ -412,8 +412,8 @@ class DoublyLinkedList {
|
|
|
412
412
|
find(callback) {
|
|
413
413
|
let current = this.head;
|
|
414
414
|
while (current) {
|
|
415
|
-
if (callback(current.
|
|
416
|
-
return current.
|
|
415
|
+
if (callback(current.value)) {
|
|
416
|
+
return current.value;
|
|
417
417
|
}
|
|
418
418
|
current = current.next;
|
|
419
419
|
}
|
|
@@ -421,16 +421,16 @@ class DoublyLinkedList {
|
|
|
421
421
|
}
|
|
422
422
|
/**
|
|
423
423
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
424
|
-
* @param {E}
|
|
424
|
+
* @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
|
|
425
425
|
* that we are searching for in the linked list.
|
|
426
|
-
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `
|
|
426
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
|
|
427
427
|
* list. If the value is not found, it returns -1.
|
|
428
428
|
*/
|
|
429
|
-
indexOf(
|
|
429
|
+
indexOf(value) {
|
|
430
430
|
let index = 0;
|
|
431
431
|
let current = this.head;
|
|
432
432
|
while (current) {
|
|
433
|
-
if (current.
|
|
433
|
+
if (current.value === value) {
|
|
434
434
|
return index;
|
|
435
435
|
}
|
|
436
436
|
index++;
|
|
@@ -449,8 +449,8 @@ class DoublyLinkedList {
|
|
|
449
449
|
findBackward(callback) {
|
|
450
450
|
let current = this.tail;
|
|
451
451
|
while (current) {
|
|
452
|
-
if (callback(current.
|
|
453
|
-
return current.
|
|
452
|
+
if (callback(current.value)) {
|
|
453
|
+
return current.value;
|
|
454
454
|
}
|
|
455
455
|
current = current.prev;
|
|
456
456
|
}
|
|
@@ -464,7 +464,7 @@ class DoublyLinkedList {
|
|
|
464
464
|
const array = [];
|
|
465
465
|
let current = this.tail;
|
|
466
466
|
while (current) {
|
|
467
|
-
array.push(current.
|
|
467
|
+
array.push(current.value);
|
|
468
468
|
current = current.prev;
|
|
469
469
|
}
|
|
470
470
|
return array;
|
|
@@ -483,7 +483,7 @@ class DoublyLinkedList {
|
|
|
483
483
|
}
|
|
484
484
|
/**
|
|
485
485
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
486
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
486
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
487
487
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
488
488
|
* current node in the linked list.
|
|
489
489
|
*/
|
|
@@ -491,7 +491,7 @@ class DoublyLinkedList {
|
|
|
491
491
|
let current = this.head;
|
|
492
492
|
let index = 0;
|
|
493
493
|
while (current) {
|
|
494
|
-
callback(current.
|
|
494
|
+
callback(current.value, index);
|
|
495
495
|
current = current.next;
|
|
496
496
|
index++;
|
|
497
497
|
}
|
|
@@ -508,7 +508,7 @@ class DoublyLinkedList {
|
|
|
508
508
|
const mappedList = new DoublyLinkedList();
|
|
509
509
|
let current = this.head;
|
|
510
510
|
while (current) {
|
|
511
|
-
mappedList.push(callback(current.
|
|
511
|
+
mappedList.push(callback(current.value));
|
|
512
512
|
current = current.next;
|
|
513
513
|
}
|
|
514
514
|
return mappedList;
|
|
@@ -524,8 +524,8 @@ class DoublyLinkedList {
|
|
|
524
524
|
const filteredList = new DoublyLinkedList();
|
|
525
525
|
let current = this.head;
|
|
526
526
|
while (current) {
|
|
527
|
-
if (callback(current.
|
|
528
|
-
filteredList.push(current.
|
|
527
|
+
if (callback(current.value)) {
|
|
528
|
+
filteredList.push(current.value);
|
|
529
529
|
}
|
|
530
530
|
current = current.next;
|
|
531
531
|
}
|
|
@@ -534,7 +534,7 @@ class DoublyLinkedList {
|
|
|
534
534
|
/**
|
|
535
535
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
536
536
|
* single value.
|
|
537
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
537
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
538
538
|
* used to perform a specific operation on each element of the linked list.
|
|
539
539
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
540
540
|
* point for the reduction operation.
|
|
@@ -545,7 +545,7 @@ class DoublyLinkedList {
|
|
|
545
545
|
let accumulator = initialValue;
|
|
546
546
|
let current = this.head;
|
|
547
547
|
while (current) {
|
|
548
|
-
accumulator = callback(accumulator, current.
|
|
548
|
+
accumulator = callback(accumulator, current.value);
|
|
549
549
|
current = current.next;
|
|
550
550
|
}
|
|
551
551
|
return accumulator;
|
|
@@ -589,7 +589,7 @@ class DoublyLinkedList {
|
|
|
589
589
|
*[Symbol.iterator]() {
|
|
590
590
|
let current = this.head;
|
|
591
591
|
while (current) {
|
|
592
|
-
yield current.
|
|
592
|
+
yield current.value;
|
|
593
593
|
current = current.next;
|
|
594
594
|
}
|
|
595
595
|
}
|