directed-graph-typed 1.39.5 → 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/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -14
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -27
- package/dist/data-structures/binary-tree/binary-tree.js +25 -46
- package/dist/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/data-structures/binary-tree/bst.js +16 -16
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
- package/dist/data-structures/binary-tree/rb-tree.js +5 -11
- package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
- package/dist/data-structures/binary-tree/segment-tree.js +20 -68
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
- package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
- package/dist/data-structures/graph/abstract-graph.js +18 -57
- package/dist/data-structures/graph/directed-graph.d.ts +16 -22
- package/dist/data-structures/graph/directed-graph.js +17 -35
- package/dist/data-structures/graph/map-graph.d.ts +13 -19
- package/dist/data-structures/graph/map-graph.js +15 -33
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
- package/dist/data-structures/graph/undirected-graph.js +15 -28
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +10 -13
- package/dist/data-structures/hash/hash-table.js +8 -17
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +34 -42
- package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
- package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/interfaces/graph.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +15 -17
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +35 -60
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/rb-tree.ts +19 -27
- package/src/data-structures/binary-tree/segment-tree.ts +25 -92
- package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
- package/src/data-structures/graph/abstract-graph.ts +42 -88
- package/src/data-structures/graph/directed-graph.ts +29 -55
- package/src/data-structures/graph/map-graph.ts +20 -45
- package/src/data-structures/graph/undirected-graph.ts +24 -41
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +15 -27
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +81 -115
- package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/interfaces/graph.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -6,31 +6,25 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export declare class SinglyLinkedListNode<E = any> {
|
|
9
|
+
value: E;
|
|
10
|
+
next: SinglyLinkedListNode<E> | null;
|
|
9
11
|
/**
|
|
10
12
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
11
|
-
* @param {E}
|
|
13
|
+
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
12
14
|
* will be stored in the node of a linked list.
|
|
13
15
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
private _val;
|
|
16
|
-
get val(): E;
|
|
17
|
-
set val(value: E);
|
|
18
|
-
private _next;
|
|
19
|
-
get next(): SinglyLinkedListNode<E> | null;
|
|
20
|
-
set next(value: SinglyLinkedListNode<E> | null);
|
|
16
|
+
constructor(value: E);
|
|
21
17
|
}
|
|
22
18
|
export declare class SinglyLinkedList<E = any> {
|
|
23
19
|
/**
|
|
24
20
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
25
21
|
*/
|
|
26
22
|
constructor();
|
|
27
|
-
|
|
23
|
+
protected _head: SinglyLinkedListNode<E> | null;
|
|
28
24
|
get head(): SinglyLinkedListNode<E> | null;
|
|
29
|
-
|
|
30
|
-
private _tail;
|
|
25
|
+
protected _tail: SinglyLinkedListNode<E> | null;
|
|
31
26
|
get tail(): SinglyLinkedListNode<E> | null;
|
|
32
|
-
|
|
33
|
-
private _length;
|
|
27
|
+
protected _length: number;
|
|
34
28
|
get length(): number;
|
|
35
29
|
/**
|
|
36
30
|
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
@@ -40,17 +34,17 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
40
34
|
*/
|
|
41
35
|
static fromArray<E>(data: E[]): SinglyLinkedList<E>;
|
|
42
36
|
/**
|
|
43
|
-
* The `push` function adds a new node with the given
|
|
44
|
-
* @param {E}
|
|
37
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
38
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
45
39
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
46
40
|
*/
|
|
47
|
-
push(
|
|
41
|
+
push(value: E): void;
|
|
48
42
|
/**
|
|
49
|
-
* The `push` function adds a new node with the given
|
|
50
|
-
* @param {E}
|
|
43
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
44
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
51
45
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
52
46
|
*/
|
|
53
|
-
addLast(
|
|
47
|
+
addLast(value: E): void;
|
|
54
48
|
/**
|
|
55
49
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
56
50
|
* pointers accordingly.
|
|
@@ -77,16 +71,16 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
77
71
|
popFirst(): E | undefined;
|
|
78
72
|
/**
|
|
79
73
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
80
|
-
* @param {E}
|
|
74
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
81
75
|
* linked list.
|
|
82
76
|
*/
|
|
83
|
-
unshift(
|
|
77
|
+
unshift(value: E): void;
|
|
84
78
|
/**
|
|
85
79
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
86
|
-
* @param {E}
|
|
80
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
87
81
|
* linked list.
|
|
88
82
|
*/
|
|
89
|
-
addFirst(
|
|
83
|
+
addFirst(value: E): void;
|
|
90
84
|
/**
|
|
91
85
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
92
86
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -123,12 +117,12 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
123
117
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
124
118
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
125
119
|
* linked list. It is of type number.
|
|
126
|
-
* @param {E}
|
|
120
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
127
121
|
* specified index.
|
|
128
122
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
129
123
|
* if the index is out of bounds.
|
|
130
124
|
*/
|
|
131
|
-
insertAt(index: number,
|
|
125
|
+
insertAt(index: number, value: E): boolean;
|
|
132
126
|
/**
|
|
133
127
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
134
128
|
* whether it is empty or not.
|
|
@@ -156,7 +150,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
156
150
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
157
151
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
158
152
|
*/
|
|
159
|
-
find(callback: (
|
|
153
|
+
find(callback: (value: E) => boolean): E | null;
|
|
160
154
|
/**
|
|
161
155
|
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
162
156
|
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
@@ -198,11 +192,11 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
198
192
|
countOccurrences(value: E): number;
|
|
199
193
|
/**
|
|
200
194
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
201
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
195
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
202
196
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
203
197
|
* current node in the linked list.
|
|
204
198
|
*/
|
|
205
|
-
forEach(callback: (
|
|
199
|
+
forEach(callback: (value: E, index: number) => void): void;
|
|
206
200
|
/**
|
|
207
201
|
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
208
202
|
* SinglyLinkedList with the transformed values.
|
|
@@ -211,7 +205,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
211
205
|
* SinglyLinkedList).
|
|
212
206
|
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
213
207
|
*/
|
|
214
|
-
map<U>(callback: (
|
|
208
|
+
map<U>(callback: (value: E) => U): SinglyLinkedList<U>;
|
|
215
209
|
/**
|
|
216
210
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
217
211
|
* elements that satisfy the given callback function.
|
|
@@ -219,18 +213,18 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
219
213
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
220
214
|
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
221
215
|
*/
|
|
222
|
-
filter(callback: (
|
|
216
|
+
filter(callback: (value: E) => boolean): SinglyLinkedList<E>;
|
|
223
217
|
/**
|
|
224
218
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
225
219
|
* single value.
|
|
226
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
220
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
227
221
|
* used to perform a specific operation on each element of the linked list.
|
|
228
222
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
229
223
|
* point for the reduction operation.
|
|
230
224
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
231
225
|
* elements in the linked list.
|
|
232
226
|
*/
|
|
233
|
-
reduce<U>(callback: (accumulator: U,
|
|
227
|
+
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U;
|
|
234
228
|
/**
|
|
235
229
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
236
230
|
*/
|
|
@@ -11,24 +11,12 @@ exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
|
11
11
|
class SinglyLinkedListNode {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
14
|
-
* @param {E}
|
|
14
|
+
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
15
15
|
* will be stored in the node of a linked list.
|
|
16
16
|
*/
|
|
17
|
-
constructor(
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
}
|
|
21
|
-
get val() {
|
|
22
|
-
return this._val;
|
|
23
|
-
}
|
|
24
|
-
set val(value) {
|
|
25
|
-
this._val = value;
|
|
26
|
-
}
|
|
27
|
-
get next() {
|
|
28
|
-
return this._next;
|
|
29
|
-
}
|
|
30
|
-
set next(value) {
|
|
31
|
-
this._next = value;
|
|
17
|
+
constructor(value) {
|
|
18
|
+
this.value = value;
|
|
19
|
+
this.next = null;
|
|
32
20
|
}
|
|
33
21
|
}
|
|
34
22
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
@@ -44,15 +32,9 @@ class SinglyLinkedList {
|
|
|
44
32
|
get head() {
|
|
45
33
|
return this._head;
|
|
46
34
|
}
|
|
47
|
-
set head(value) {
|
|
48
|
-
this._head = value;
|
|
49
|
-
}
|
|
50
35
|
get tail() {
|
|
51
36
|
return this._tail;
|
|
52
37
|
}
|
|
53
|
-
set tail(value) {
|
|
54
|
-
this._tail = value;
|
|
55
|
-
}
|
|
56
38
|
get length() {
|
|
57
39
|
return this._length;
|
|
58
40
|
}
|
|
@@ -70,29 +52,29 @@ class SinglyLinkedList {
|
|
|
70
52
|
return singlyLinkedList;
|
|
71
53
|
}
|
|
72
54
|
/**
|
|
73
|
-
* The `push` function adds a new node with the given
|
|
74
|
-
* @param {E}
|
|
55
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
56
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
75
57
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
76
58
|
*/
|
|
77
|
-
push(
|
|
78
|
-
const newNode = new SinglyLinkedListNode(
|
|
59
|
+
push(value) {
|
|
60
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
79
61
|
if (!this.head) {
|
|
80
|
-
this.
|
|
81
|
-
this.
|
|
62
|
+
this._head = newNode;
|
|
63
|
+
this._tail = newNode;
|
|
82
64
|
}
|
|
83
65
|
else {
|
|
84
66
|
this.tail.next = newNode;
|
|
85
|
-
this.
|
|
67
|
+
this._tail = newNode;
|
|
86
68
|
}
|
|
87
69
|
this._length++;
|
|
88
70
|
}
|
|
89
71
|
/**
|
|
90
|
-
* The `push` function adds a new node with the given
|
|
91
|
-
* @param {E}
|
|
72
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
73
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
92
74
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
93
75
|
*/
|
|
94
|
-
addLast(
|
|
95
|
-
this.push(
|
|
76
|
+
addLast(value) {
|
|
77
|
+
this.push(value);
|
|
96
78
|
}
|
|
97
79
|
/**
|
|
98
80
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
@@ -104,21 +86,21 @@ class SinglyLinkedList {
|
|
|
104
86
|
if (!this.head)
|
|
105
87
|
return undefined;
|
|
106
88
|
if (this.head === this.tail) {
|
|
107
|
-
const
|
|
108
|
-
this.
|
|
109
|
-
this.
|
|
89
|
+
const value = this.head.value;
|
|
90
|
+
this._head = null;
|
|
91
|
+
this._tail = null;
|
|
110
92
|
this._length--;
|
|
111
|
-
return
|
|
93
|
+
return value;
|
|
112
94
|
}
|
|
113
95
|
let current = this.head;
|
|
114
96
|
while (current.next !== this.tail) {
|
|
115
97
|
current = current.next;
|
|
116
98
|
}
|
|
117
|
-
const
|
|
99
|
+
const value = this.tail.value;
|
|
118
100
|
current.next = null;
|
|
119
|
-
this.
|
|
101
|
+
this._tail = current;
|
|
120
102
|
this._length--;
|
|
121
|
-
return
|
|
103
|
+
return value;
|
|
122
104
|
}
|
|
123
105
|
/**
|
|
124
106
|
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
@@ -137,9 +119,9 @@ class SinglyLinkedList {
|
|
|
137
119
|
if (!this.head)
|
|
138
120
|
return undefined;
|
|
139
121
|
const removedNode = this.head;
|
|
140
|
-
this.
|
|
122
|
+
this._head = this.head.next;
|
|
141
123
|
this._length--;
|
|
142
|
-
return removedNode.
|
|
124
|
+
return removedNode.value;
|
|
143
125
|
}
|
|
144
126
|
/**
|
|
145
127
|
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
@@ -150,28 +132,28 @@ class SinglyLinkedList {
|
|
|
150
132
|
}
|
|
151
133
|
/**
|
|
152
134
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
153
|
-
* @param {E}
|
|
135
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
154
136
|
* linked list.
|
|
155
137
|
*/
|
|
156
|
-
unshift(
|
|
157
|
-
const newNode = new SinglyLinkedListNode(
|
|
138
|
+
unshift(value) {
|
|
139
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
158
140
|
if (!this.head) {
|
|
159
|
-
this.
|
|
160
|
-
this.
|
|
141
|
+
this._head = newNode;
|
|
142
|
+
this._tail = newNode;
|
|
161
143
|
}
|
|
162
144
|
else {
|
|
163
145
|
newNode.next = this.head;
|
|
164
|
-
this.
|
|
146
|
+
this._head = newNode;
|
|
165
147
|
}
|
|
166
148
|
this._length++;
|
|
167
149
|
}
|
|
168
150
|
/**
|
|
169
151
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
170
|
-
* @param {E}
|
|
152
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
171
153
|
* linked list.
|
|
172
154
|
*/
|
|
173
|
-
addFirst(
|
|
174
|
-
this.unshift(
|
|
155
|
+
addFirst(value) {
|
|
156
|
+
this.unshift(value);
|
|
175
157
|
}
|
|
176
158
|
/**
|
|
177
159
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
@@ -187,7 +169,7 @@ class SinglyLinkedList {
|
|
|
187
169
|
for (let i = 0; i < index; i++) {
|
|
188
170
|
current = current.next;
|
|
189
171
|
}
|
|
190
|
-
return current.
|
|
172
|
+
return current.value;
|
|
191
173
|
}
|
|
192
174
|
/**
|
|
193
175
|
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
@@ -221,7 +203,7 @@ class SinglyLinkedList {
|
|
|
221
203
|
const removedNode = prevNode.next;
|
|
222
204
|
prevNode.next = removedNode.next;
|
|
223
205
|
this._length--;
|
|
224
|
-
return removedNode.
|
|
206
|
+
return removedNode.value;
|
|
225
207
|
}
|
|
226
208
|
/**
|
|
227
209
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
@@ -235,24 +217,24 @@ class SinglyLinkedList {
|
|
|
235
217
|
return false;
|
|
236
218
|
let value;
|
|
237
219
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
238
|
-
value = valueOrNode.
|
|
220
|
+
value = valueOrNode.value;
|
|
239
221
|
}
|
|
240
222
|
else {
|
|
241
223
|
value = valueOrNode;
|
|
242
224
|
}
|
|
243
225
|
let current = this.head, prev = null;
|
|
244
226
|
while (current) {
|
|
245
|
-
if (current.
|
|
227
|
+
if (current.value === value) {
|
|
246
228
|
if (prev === null) {
|
|
247
|
-
this.
|
|
229
|
+
this._head = current.next;
|
|
248
230
|
if (current === this.tail) {
|
|
249
|
-
this.
|
|
231
|
+
this._tail = null;
|
|
250
232
|
}
|
|
251
233
|
}
|
|
252
234
|
else {
|
|
253
235
|
prev.next = current.next;
|
|
254
236
|
if (current === this.tail) {
|
|
255
|
-
this.
|
|
237
|
+
this._tail = prev;
|
|
256
238
|
}
|
|
257
239
|
}
|
|
258
240
|
this._length--;
|
|
@@ -267,23 +249,23 @@ class SinglyLinkedList {
|
|
|
267
249
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
268
250
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
269
251
|
* linked list. It is of type number.
|
|
270
|
-
* @param {E}
|
|
252
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
271
253
|
* specified index.
|
|
272
254
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
273
255
|
* if the index is out of bounds.
|
|
274
256
|
*/
|
|
275
|
-
insertAt(index,
|
|
257
|
+
insertAt(index, value) {
|
|
276
258
|
if (index < 0 || index > this.length)
|
|
277
259
|
return false;
|
|
278
260
|
if (index === 0) {
|
|
279
|
-
this.unshift(
|
|
261
|
+
this.unshift(value);
|
|
280
262
|
return true;
|
|
281
263
|
}
|
|
282
264
|
if (index === this.length) {
|
|
283
|
-
this.push(
|
|
265
|
+
this.push(value);
|
|
284
266
|
return true;
|
|
285
267
|
}
|
|
286
|
-
const newNode = new SinglyLinkedListNode(
|
|
268
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
287
269
|
const prevNode = this.getNodeAt(index - 1);
|
|
288
270
|
newNode.next = prevNode.next;
|
|
289
271
|
prevNode.next = newNode;
|
|
@@ -314,7 +296,7 @@ class SinglyLinkedList {
|
|
|
314
296
|
const array = [];
|
|
315
297
|
let current = this.head;
|
|
316
298
|
while (current) {
|
|
317
|
-
array.push(current.
|
|
299
|
+
array.push(current.value);
|
|
318
300
|
current = current.next;
|
|
319
301
|
}
|
|
320
302
|
return array;
|
|
@@ -335,7 +317,7 @@ class SinglyLinkedList {
|
|
|
335
317
|
prev = current;
|
|
336
318
|
current = next;
|
|
337
319
|
}
|
|
338
|
-
[this.
|
|
320
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
339
321
|
}
|
|
340
322
|
/**
|
|
341
323
|
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
@@ -347,8 +329,8 @@ class SinglyLinkedList {
|
|
|
347
329
|
find(callback) {
|
|
348
330
|
let current = this.head;
|
|
349
331
|
while (current) {
|
|
350
|
-
if (callback(current.
|
|
351
|
-
return current.
|
|
332
|
+
if (callback(current.value)) {
|
|
333
|
+
return current.value;
|
|
352
334
|
}
|
|
353
335
|
current = current.next;
|
|
354
336
|
}
|
|
@@ -364,7 +346,7 @@ class SinglyLinkedList {
|
|
|
364
346
|
let index = 0;
|
|
365
347
|
let current = this.head;
|
|
366
348
|
while (current) {
|
|
367
|
-
if (current.
|
|
349
|
+
if (current.value === value) {
|
|
368
350
|
return index;
|
|
369
351
|
}
|
|
370
352
|
index++;
|
|
@@ -382,7 +364,7 @@ class SinglyLinkedList {
|
|
|
382
364
|
getNode(value) {
|
|
383
365
|
let current = this.head;
|
|
384
366
|
while (current) {
|
|
385
|
-
if (current.
|
|
367
|
+
if (current.value === value) {
|
|
386
368
|
return current;
|
|
387
369
|
}
|
|
388
370
|
current = current.next;
|
|
@@ -402,18 +384,18 @@ class SinglyLinkedList {
|
|
|
402
384
|
return false;
|
|
403
385
|
let existingValue;
|
|
404
386
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
405
|
-
existingValue = existingValueOrNode.
|
|
387
|
+
existingValue = existingValueOrNode.value;
|
|
406
388
|
}
|
|
407
389
|
else {
|
|
408
390
|
existingValue = existingValueOrNode;
|
|
409
391
|
}
|
|
410
|
-
if (this.head.
|
|
392
|
+
if (this.head.value === existingValue) {
|
|
411
393
|
this.unshift(newValue);
|
|
412
394
|
return true;
|
|
413
395
|
}
|
|
414
396
|
let current = this.head;
|
|
415
397
|
while (current.next) {
|
|
416
|
-
if (current.next.
|
|
398
|
+
if (current.next.value === existingValue) {
|
|
417
399
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
418
400
|
newNode.next = current.next;
|
|
419
401
|
current.next = newNode;
|
|
@@ -445,7 +427,7 @@ class SinglyLinkedList {
|
|
|
445
427
|
newNode.next = existingNode.next;
|
|
446
428
|
existingNode.next = newNode;
|
|
447
429
|
if (existingNode === this.tail) {
|
|
448
|
-
this.
|
|
430
|
+
this._tail = newNode;
|
|
449
431
|
}
|
|
450
432
|
this._length++;
|
|
451
433
|
return true;
|
|
@@ -461,7 +443,7 @@ class SinglyLinkedList {
|
|
|
461
443
|
let count = 0;
|
|
462
444
|
let current = this.head;
|
|
463
445
|
while (current) {
|
|
464
|
-
if (current.
|
|
446
|
+
if (current.value === value) {
|
|
465
447
|
count++;
|
|
466
448
|
}
|
|
467
449
|
current = current.next;
|
|
@@ -470,7 +452,7 @@ class SinglyLinkedList {
|
|
|
470
452
|
}
|
|
471
453
|
/**
|
|
472
454
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
473
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
455
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
474
456
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
475
457
|
* current node in the linked list.
|
|
476
458
|
*/
|
|
@@ -478,7 +460,7 @@ class SinglyLinkedList {
|
|
|
478
460
|
let current = this.head;
|
|
479
461
|
let index = 0;
|
|
480
462
|
while (current) {
|
|
481
|
-
callback(current.
|
|
463
|
+
callback(current.value, index);
|
|
482
464
|
current = current.next;
|
|
483
465
|
index++;
|
|
484
466
|
}
|
|
@@ -495,7 +477,7 @@ class SinglyLinkedList {
|
|
|
495
477
|
const mappedList = new SinglyLinkedList();
|
|
496
478
|
let current = this.head;
|
|
497
479
|
while (current) {
|
|
498
|
-
mappedList.push(callback(current.
|
|
480
|
+
mappedList.push(callback(current.value));
|
|
499
481
|
current = current.next;
|
|
500
482
|
}
|
|
501
483
|
return mappedList;
|
|
@@ -511,8 +493,8 @@ class SinglyLinkedList {
|
|
|
511
493
|
const filteredList = new SinglyLinkedList();
|
|
512
494
|
let current = this.head;
|
|
513
495
|
while (current) {
|
|
514
|
-
if (callback(current.
|
|
515
|
-
filteredList.push(current.
|
|
496
|
+
if (callback(current.value)) {
|
|
497
|
+
filteredList.push(current.value);
|
|
516
498
|
}
|
|
517
499
|
current = current.next;
|
|
518
500
|
}
|
|
@@ -521,7 +503,7 @@ class SinglyLinkedList {
|
|
|
521
503
|
/**
|
|
522
504
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
523
505
|
* single value.
|
|
524
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
506
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
525
507
|
* used to perform a specific operation on each element of the linked list.
|
|
526
508
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
527
509
|
* point for the reduction operation.
|
|
@@ -532,7 +514,7 @@ class SinglyLinkedList {
|
|
|
532
514
|
let accumulator = initialValue;
|
|
533
515
|
let current = this.head;
|
|
534
516
|
while (current) {
|
|
535
|
-
accumulator = callback(accumulator, current.
|
|
517
|
+
accumulator = callback(accumulator, current.value);
|
|
536
518
|
current = current.next;
|
|
537
519
|
}
|
|
538
520
|
return accumulator;
|
|
@@ -543,7 +525,7 @@ class SinglyLinkedList {
|
|
|
543
525
|
*[Symbol.iterator]() {
|
|
544
526
|
let current = this.head;
|
|
545
527
|
while (current) {
|
|
546
|
-
yield current.
|
|
528
|
+
yield current.value;
|
|
547
529
|
current = current.next;
|
|
548
530
|
}
|
|
549
531
|
}
|
|
@@ -20,18 +20,14 @@ export declare class SkipList<K, V> {
|
|
|
20
20
|
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
21
21
|
*/
|
|
22
22
|
constructor(maxLevel?: number, probability?: number);
|
|
23
|
-
|
|
23
|
+
protected _head: SkipListNode<K, V>;
|
|
24
24
|
get head(): SkipListNode<K, V>;
|
|
25
|
-
|
|
26
|
-
private _level;
|
|
25
|
+
protected _level: number;
|
|
27
26
|
get level(): number;
|
|
28
|
-
|
|
29
|
-
private _maxLevel;
|
|
27
|
+
protected _maxLevel: number;
|
|
30
28
|
get maxLevel(): number;
|
|
31
|
-
|
|
32
|
-
private _probability;
|
|
29
|
+
protected _probability: number;
|
|
33
30
|
get probability(): number;
|
|
34
|
-
set probability(value: number);
|
|
35
31
|
/**
|
|
36
32
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
37
33
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
@@ -46,6 +42,7 @@ export declare class SkipList<K, V> {
|
|
|
46
42
|
* otherwise it returns `undefined`.
|
|
47
43
|
*/
|
|
48
44
|
get(key: K): V | undefined;
|
|
45
|
+
has(key: K): boolean;
|
|
49
46
|
/**
|
|
50
47
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
51
48
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
@@ -54,8 +51,30 @@ export declare class SkipList<K, V> {
|
|
|
54
51
|
*/
|
|
55
52
|
delete(key: K): boolean;
|
|
56
53
|
/**
|
|
57
|
-
*
|
|
54
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
55
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
56
|
+
*/
|
|
57
|
+
getFirst(): V | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
60
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
61
|
+
*/
|
|
62
|
+
getLast(): V | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
65
|
+
* @param key - the given key.
|
|
66
|
+
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
67
|
+
*/
|
|
68
|
+
higher(key: K): V | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Get the value of the last element in the Skip List that is less than the given key.
|
|
71
|
+
* @param key - the given key.
|
|
72
|
+
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
73
|
+
*/
|
|
74
|
+
lower(key: K): V | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
58
77
|
* @returns the level, which is a number.
|
|
59
78
|
*/
|
|
60
|
-
|
|
79
|
+
protected _randomLevel(): number;
|
|
61
80
|
}
|