graph-typed 1.53.5 → 1.53.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/README.md +4 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +13 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +27 -26
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +144 -62
- package/dist/data-structures/linked-list/singly-linked-list.js +201 -97
- package/package.json +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +30 -26
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
|
@@ -99,33 +99,17 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
99
99
|
get size() {
|
|
100
100
|
return this._size;
|
|
101
101
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Time Complexity: O(n)
|
|
104
|
-
* Space Complexity: O(n)
|
|
105
|
-
*
|
|
106
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
107
|
-
* array.
|
|
108
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
109
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
110
|
-
*/
|
|
111
|
-
static fromArray(data) {
|
|
112
|
-
const singlyLinkedList = new SinglyLinkedList();
|
|
113
|
-
for (const item of data) {
|
|
114
|
-
singlyLinkedList.push(item);
|
|
115
|
-
}
|
|
116
|
-
return singlyLinkedList;
|
|
117
|
-
}
|
|
118
102
|
/**
|
|
119
103
|
* Time Complexity: O(1)
|
|
120
104
|
* Space Complexity: O(1)
|
|
121
105
|
*
|
|
122
|
-
* The push function adds a new element to the end of a singly linked list.
|
|
123
|
-
* @param {E}
|
|
124
|
-
*
|
|
125
|
-
* @returns The `push` method is returning a boolean value, `true`.
|
|
106
|
+
* The `push` function adds a new element or node to the end of a singly linked list.
|
|
107
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
|
|
108
|
+
* method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
|
|
109
|
+
* @returns The `push` method is returning a boolean value, specifically `true`.
|
|
126
110
|
*/
|
|
127
|
-
push(
|
|
128
|
-
const newNode =
|
|
111
|
+
push(elementOrNode) {
|
|
112
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
129
113
|
if (!this.head) {
|
|
130
114
|
this._head = newNode;
|
|
131
115
|
this._tail = newNode;
|
|
@@ -184,13 +168,15 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
184
168
|
* Time Complexity: O(1)
|
|
185
169
|
* Space Complexity: O(1)
|
|
186
170
|
*
|
|
187
|
-
* The unshift function adds a new element to the beginning of a singly linked list
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
171
|
+
* The unshift function adds a new element or node to the beginning of a singly linked list in
|
|
172
|
+
* TypeScript.
|
|
173
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
174
|
+
* `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
|
|
175
|
+
* element of type `E`.
|
|
176
|
+
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
177
|
+
*/
|
|
178
|
+
unshift(elementOrNode) {
|
|
179
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
194
180
|
if (!this.head) {
|
|
195
181
|
this._head = newNode;
|
|
196
182
|
this._tail = newNode;
|
|
@@ -202,6 +188,28 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
202
188
|
this._size++;
|
|
203
189
|
return true;
|
|
204
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Time Complexity: O(n)
|
|
193
|
+
* Space Complexity: O(1)
|
|
194
|
+
*
|
|
195
|
+
* This function searches for a specific element in a singly linked list based on a given node or
|
|
196
|
+
* predicate.
|
|
197
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
198
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
|
|
199
|
+
* the following types:
|
|
200
|
+
* @returns The `get` method returns the value of the first node in the singly linked list that
|
|
201
|
+
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
202
|
+
*/
|
|
203
|
+
get(elementNodeOrPredicate) {
|
|
204
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
205
|
+
let current = this.head;
|
|
206
|
+
while (current) {
|
|
207
|
+
if (predicate(current))
|
|
208
|
+
return current.value;
|
|
209
|
+
current = current.next;
|
|
210
|
+
}
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
205
213
|
/**
|
|
206
214
|
* Time Complexity: O(n)
|
|
207
215
|
* Space Complexity: O(1)
|
|
@@ -221,6 +229,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
221
229
|
}
|
|
222
230
|
return current.value;
|
|
223
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Time Complexity: O(1)
|
|
234
|
+
* Space Complexity: O(1)
|
|
235
|
+
*
|
|
236
|
+
* The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
|
|
237
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
238
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
|
|
239
|
+
* one of the following types:
|
|
240
|
+
* @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
|
|
241
|
+
* instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
|
|
242
|
+
* parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
|
|
243
|
+
* the function returns `false`.
|
|
244
|
+
*/
|
|
245
|
+
isNode(elementNodeOrPredicate) {
|
|
246
|
+
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
247
|
+
}
|
|
224
248
|
/**
|
|
225
249
|
* Time Complexity: O(n)
|
|
226
250
|
* Space Complexity: O(1)
|
|
@@ -270,20 +294,20 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
270
294
|
* Space Complexity: O(1)
|
|
271
295
|
*
|
|
272
296
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
273
|
-
* @param {E | SinglyLinkedListNode<E>}
|
|
297
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
|
|
274
298
|
* or a `SinglyLinkedListNode<E>` object.
|
|
275
299
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
276
300
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
277
301
|
*/
|
|
278
|
-
delete(
|
|
279
|
-
if (
|
|
302
|
+
delete(elementOrNode) {
|
|
303
|
+
if (elementOrNode === undefined)
|
|
280
304
|
return false;
|
|
281
305
|
let value;
|
|
282
|
-
if (
|
|
283
|
-
value =
|
|
306
|
+
if (elementOrNode instanceof SinglyLinkedListNode) {
|
|
307
|
+
value = elementOrNode.value;
|
|
284
308
|
}
|
|
285
309
|
else {
|
|
286
|
-
value =
|
|
310
|
+
value = elementOrNode;
|
|
287
311
|
}
|
|
288
312
|
let current = this.head, prev = undefined;
|
|
289
313
|
while (current) {
|
|
@@ -312,26 +336,29 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
312
336
|
* Time Complexity: O(n)
|
|
313
337
|
* Space Complexity: O(1)
|
|
314
338
|
*
|
|
315
|
-
* The `addAt` function inserts a
|
|
316
|
-
* @param {number} index - The index parameter represents the position at which
|
|
317
|
-
* linked list. It is
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
|
|
323
|
-
|
|
339
|
+
* The `addAt` function inserts a new element or node at a specified index in a singly linked list.
|
|
340
|
+
* @param {number} index - The `index` parameter represents the position at which you want to add a
|
|
341
|
+
* new element or node in the linked list. It is a number that indicates the index where the new
|
|
342
|
+
* element or node should be inserted.
|
|
343
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
344
|
+
* `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
|
|
345
|
+
* parameter represents the element or node that you want to add to the linked list at the specified
|
|
346
|
+
* index.
|
|
347
|
+
* @returns The `addAt` method returns a boolean value - `true` if the element or node was
|
|
348
|
+
* successfully added at the specified index, and `false` if the index is out of bounds.
|
|
349
|
+
*/
|
|
350
|
+
addAt(index, newElementOrNode) {
|
|
324
351
|
if (index < 0 || index > this._size)
|
|
325
352
|
return false;
|
|
326
353
|
if (index === 0) {
|
|
327
|
-
this.unshift(
|
|
354
|
+
this.unshift(newElementOrNode);
|
|
328
355
|
return true;
|
|
329
356
|
}
|
|
330
357
|
if (index === this._size) {
|
|
331
|
-
this.push(
|
|
358
|
+
this.push(newElementOrNode);
|
|
332
359
|
return true;
|
|
333
360
|
}
|
|
334
|
-
const newNode =
|
|
361
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
335
362
|
const prevNode = this.getNodeAt(index - 1);
|
|
336
363
|
newNode.next = prevNode.next;
|
|
337
364
|
prevNode.next = newNode;
|
|
@@ -396,16 +423,21 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
396
423
|
* Time Complexity: O(n)
|
|
397
424
|
* Space Complexity: O(1)
|
|
398
425
|
*
|
|
399
|
-
* The `indexOf` function
|
|
400
|
-
*
|
|
401
|
-
* @
|
|
402
|
-
*
|
|
403
|
-
|
|
404
|
-
|
|
426
|
+
* The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
|
|
427
|
+
* list and returns its index if found.
|
|
428
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
429
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
|
|
430
|
+
* of the following types:
|
|
431
|
+
* @returns The `indexOf` method returns the index of the first occurrence of the element that
|
|
432
|
+
* matches the provided predicate in the singly linked list. If no matching element is found, it
|
|
433
|
+
* returns -1.
|
|
434
|
+
*/
|
|
435
|
+
indexOf(elementNodeOrPredicate) {
|
|
436
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
405
437
|
let index = 0;
|
|
406
438
|
let current = this.head;
|
|
407
439
|
while (current) {
|
|
408
|
-
if (current
|
|
440
|
+
if (predicate(current)) {
|
|
409
441
|
return index;
|
|
410
442
|
}
|
|
411
443
|
index++;
|
|
@@ -417,16 +449,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
417
449
|
* Time Complexity: O(n)
|
|
418
450
|
* Space Complexity: O(1)
|
|
419
451
|
*
|
|
420
|
-
* The function
|
|
421
|
-
*
|
|
422
|
-
* @param {E
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
|
|
426
|
-
|
|
452
|
+
* The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
|
|
453
|
+
* element, node, or predicate.
|
|
454
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
|
|
455
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
|
|
456
|
+
* of the following types:
|
|
457
|
+
* @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
|
|
458
|
+
* found based on the provided predicate, or it returns `undefined` if no matching node is found or
|
|
459
|
+
* if the input parameter is `undefined`.
|
|
460
|
+
*/
|
|
461
|
+
getNode(elementNodeOrPredicate) {
|
|
462
|
+
if (elementNodeOrPredicate === undefined)
|
|
463
|
+
return;
|
|
464
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
427
465
|
let current = this.head;
|
|
428
466
|
while (current) {
|
|
429
|
-
if (current
|
|
467
|
+
if (predicate(current)) {
|
|
430
468
|
return current;
|
|
431
469
|
}
|
|
432
470
|
current = current.next;
|
|
@@ -437,31 +475,36 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
437
475
|
* Time Complexity: O(n)
|
|
438
476
|
* Space Complexity: O(1)
|
|
439
477
|
*
|
|
440
|
-
* The `addBefore`
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
* @
|
|
445
|
-
*
|
|
446
|
-
|
|
447
|
-
|
|
478
|
+
* The function `addBefore` in TypeScript adds a new element or node before an existing element or
|
|
479
|
+
* node in a singly linked list.
|
|
480
|
+
* @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
|
|
481
|
+
* element or node in the linked list before which you want to add a new element or node.
|
|
482
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
483
|
+
* `addBefore` method represents the element or node that you want to insert before the existing
|
|
484
|
+
* element or node in the linked list. This new element can be of type `E` or a
|
|
485
|
+
* `SinglyLinkedListNode<E>`.
|
|
486
|
+
* @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
|
|
487
|
+
* successfully added before the existing element or node, and `false` if the operation was
|
|
488
|
+
* unsuccessful.
|
|
489
|
+
*/
|
|
490
|
+
addBefore(existingElementOrNode, newElementOrNode) {
|
|
448
491
|
if (!this.head)
|
|
449
492
|
return false;
|
|
450
493
|
let existingValue;
|
|
451
|
-
if (
|
|
452
|
-
existingValue =
|
|
494
|
+
if (this.isNode(existingElementOrNode)) {
|
|
495
|
+
existingValue = existingElementOrNode.value;
|
|
453
496
|
}
|
|
454
497
|
else {
|
|
455
|
-
existingValue =
|
|
498
|
+
existingValue = existingElementOrNode;
|
|
456
499
|
}
|
|
457
500
|
if (this.head.value === existingValue) {
|
|
458
|
-
this.unshift(
|
|
501
|
+
this.unshift(newElementOrNode);
|
|
459
502
|
return true;
|
|
460
503
|
}
|
|
461
504
|
let current = this.head;
|
|
462
505
|
while (current.next) {
|
|
463
506
|
if (current.next.value === existingValue) {
|
|
464
|
-
const newNode =
|
|
507
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
465
508
|
newNode.next = current.next;
|
|
466
509
|
current.next = newNode;
|
|
467
510
|
this._size++;
|
|
@@ -475,23 +518,22 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
475
518
|
* Time Complexity: O(n)
|
|
476
519
|
* Space Complexity: O(1)
|
|
477
520
|
*
|
|
478
|
-
* The `addAfter` function
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* @
|
|
483
|
-
*
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
}
|
|
521
|
+
* The `addAfter` function in TypeScript adds a new element or node after an existing element or node
|
|
522
|
+
* in a singly linked list.
|
|
523
|
+
* @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
|
|
524
|
+
* an element of type E or a SinglyLinkedListNode of type E.
|
|
525
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
526
|
+
* `addAfter` method represents the element or node that you want to add after the existing element
|
|
527
|
+
* or node in a singly linked list. This parameter can be either the value of the new element or a
|
|
528
|
+
* reference to a `SinglyLinkedListNode` containing
|
|
529
|
+
* @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
|
|
530
|
+
* successfully added after the existing element or node, and `false` if the existing element or node
|
|
531
|
+
* was not found.
|
|
532
|
+
*/
|
|
533
|
+
addAfter(existingElementOrNode, newElementOrNode) {
|
|
534
|
+
const existingNode = this.getNode(existingElementOrNode);
|
|
493
535
|
if (existingNode) {
|
|
494
|
-
const newNode =
|
|
536
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
495
537
|
newNode.next = existingNode.next;
|
|
496
538
|
existingNode.next = newNode;
|
|
497
539
|
if (existingNode === this.tail) {
|
|
@@ -506,15 +548,19 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
506
548
|
* Time Complexity: O(n)
|
|
507
549
|
* Space Complexity: O(1)
|
|
508
550
|
*
|
|
509
|
-
* The function
|
|
510
|
-
*
|
|
511
|
-
* @
|
|
512
|
-
|
|
513
|
-
|
|
551
|
+
* The function `countOccurrences` iterates through a singly linked list and counts the occurrences
|
|
552
|
+
* of a specified element or nodes that satisfy a given predicate.
|
|
553
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
|
|
554
|
+
* - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
|
|
555
|
+
* @returns The `countOccurrences` method returns the number of occurrences of the specified element,
|
|
556
|
+
* node, or predicate function in the singly linked list.
|
|
557
|
+
*/
|
|
558
|
+
countOccurrences(elementOrNode) {
|
|
559
|
+
const predicate = this._ensurePredicate(elementOrNode);
|
|
514
560
|
let count = 0;
|
|
515
561
|
let current = this.head;
|
|
516
562
|
while (current) {
|
|
517
|
-
if (current
|
|
563
|
+
if (predicate(current)) {
|
|
518
564
|
count++;
|
|
519
565
|
}
|
|
520
566
|
current = current.next;
|
|
@@ -600,5 +646,63 @@ class SinglyLinkedList extends base_1.IterableElementBase {
|
|
|
600
646
|
current = current.next;
|
|
601
647
|
}
|
|
602
648
|
}
|
|
649
|
+
/**
|
|
650
|
+
* Time Complexity: O(n)
|
|
651
|
+
* Space Complexity: O(n)
|
|
652
|
+
*
|
|
653
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
654
|
+
* array.
|
|
655
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
656
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
657
|
+
*/
|
|
658
|
+
static fromArray(data) {
|
|
659
|
+
const singlyLinkedList = new SinglyLinkedList();
|
|
660
|
+
for (const item of data) {
|
|
661
|
+
singlyLinkedList.push(item);
|
|
662
|
+
}
|
|
663
|
+
return singlyLinkedList;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* The _isPredicate function in TypeScript checks if the input is a function that takes a
|
|
667
|
+
* SinglyLinkedListNode as an argument and returns a boolean.
|
|
668
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
669
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
|
|
670
|
+
* @returns The _isPredicate method is returning a boolean value based on whether the
|
|
671
|
+
* elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
|
|
672
|
+
* function, the method will return true, indicating that it is a predicate function. If it is not a
|
|
673
|
+
* function, the method will return false.
|
|
674
|
+
*/
|
|
675
|
+
_isPredicate(elementNodeOrPredicate) {
|
|
676
|
+
return typeof elementNodeOrPredicate === 'function';
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
|
|
680
|
+
* node if necessary.
|
|
681
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
|
|
682
|
+
* an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
|
|
683
|
+
* @returns A SinglyLinkedListNode<E> object is being returned.
|
|
684
|
+
*/
|
|
685
|
+
_ensureNode(elementOrNode) {
|
|
686
|
+
if (this.isNode(elementOrNode))
|
|
687
|
+
return elementOrNode;
|
|
688
|
+
return new SinglyLinkedListNode(elementOrNode);
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
|
|
692
|
+
* function, or a value to compare with the node's value.
|
|
693
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
694
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
|
|
695
|
+
* @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
|
|
696
|
+
* function is returned that checks if a given node is equal to the input node. If the input is a
|
|
697
|
+
* predicate function, it is returned as is. If the input is neither a node nor a predicate function,
|
|
698
|
+
* a function is returned that checks if a given node's value is equal to the input
|
|
699
|
+
*/
|
|
700
|
+
_ensurePredicate(elementNodeOrPredicate) {
|
|
701
|
+
if (this.isNode(elementNodeOrPredicate))
|
|
702
|
+
return (node) => node === elementNodeOrPredicate;
|
|
703
|
+
if (this._isPredicate(elementNodeOrPredicate))
|
|
704
|
+
return elementNodeOrPredicate;
|
|
705
|
+
return (node) => node.value === elementNodeOrPredicate;
|
|
706
|
+
}
|
|
603
707
|
}
|
|
604
708
|
exports.SinglyLinkedList = SinglyLinkedList;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graph-typed",
|
|
3
|
-
"version": "1.53.
|
|
3
|
+
"version": "1.53.6",
|
|
4
4
|
"description": "Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -136,6 +136,6 @@
|
|
|
136
136
|
"typescript": "^4.9.5"
|
|
137
137
|
},
|
|
138
138
|
"dependencies": {
|
|
139
|
-
"data-structure-typed": "^1.53.
|
|
139
|
+
"data-structure-typed": "^1.53.6"
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -819,13 +819,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
819
819
|
existingElementOrNode: E | DoublyLinkedListNode<E>,
|
|
820
820
|
newElementOrNode: E | DoublyLinkedListNode<E>
|
|
821
821
|
): boolean {
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
825
|
-
existingNode = existingElementOrNode;
|
|
826
|
-
} else {
|
|
827
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
828
|
-
}
|
|
822
|
+
const existingNode: DoublyLinkedListNode<E> | undefined = this.getNode(existingElementOrNode);
|
|
829
823
|
|
|
830
824
|
if (existingNode) {
|
|
831
825
|
const newNode = this._ensureNode(newElementOrNode);
|
|
@@ -862,13 +856,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
862
856
|
* was not found in the linked list.
|
|
863
857
|
*/
|
|
864
858
|
addAfter(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
868
|
-
existingNode = existingElementOrNode;
|
|
869
|
-
} else {
|
|
870
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
871
|
-
}
|
|
859
|
+
const existingNode: DoublyLinkedListNode<E> | undefined = this.getNode(existingElementOrNode);
|
|
872
860
|
|
|
873
861
|
if (existingNode) {
|
|
874
862
|
const newNode = this._ensureNode(newElementOrNode);
|
|
@@ -978,17 +966,15 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
978
966
|
* Time Complexity: O(n)
|
|
979
967
|
* Space Complexity: O(1)
|
|
980
968
|
*
|
|
981
|
-
*
|
|
982
|
-
*
|
|
983
|
-
*
|
|
984
|
-
*
|
|
985
|
-
*
|
|
986
|
-
*
|
|
987
|
-
* list. If the element or node is found in the list, the method returns the index of that element or
|
|
988
|
-
* node. If the element or node is not found in the list, the method returns -1.
|
|
969
|
+
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
|
|
970
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
971
|
+
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
|
|
972
|
+
* can be one of the following:
|
|
973
|
+
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
|
|
974
|
+
* matches the provided element, node, or predicate. If no match is found, it returns -1.
|
|
989
975
|
*/
|
|
990
|
-
indexOf(
|
|
991
|
-
const predicate = this._ensurePredicate(
|
|
976
|
+
indexOf(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number {
|
|
977
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
992
978
|
let index = 0;
|
|
993
979
|
let current = this.head;
|
|
994
980
|
while (current) {
|
|
@@ -1005,8 +991,6 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
1005
991
|
* Time Complexity: O(n)
|
|
1006
992
|
* Space Complexity: O(1)
|
|
1007
993
|
*
|
|
1008
|
-
*/
|
|
1009
|
-
/**
|
|
1010
994
|
* This function retrieves an element from a doubly linked list based on a given element
|
|
1011
995
|
* node or predicate.
|
|
1012
996
|
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
@@ -1180,6 +1164,26 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
1180
1164
|
return mappedList;
|
|
1181
1165
|
}
|
|
1182
1166
|
|
|
1167
|
+
/**
|
|
1168
|
+
* Time Complexity: O(n)
|
|
1169
|
+
* Space Complexity: O(1)
|
|
1170
|
+
*
|
|
1171
|
+
*/
|
|
1172
|
+
countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number {
|
|
1173
|
+
const predicate = this._ensurePredicate(elementOrNode);
|
|
1174
|
+
let count = 0;
|
|
1175
|
+
let current = this.head;
|
|
1176
|
+
|
|
1177
|
+
while (current) {
|
|
1178
|
+
if (predicate(current)) {
|
|
1179
|
+
count++;
|
|
1180
|
+
}
|
|
1181
|
+
current = current.next;
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
return count;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1183
1187
|
/**
|
|
1184
1188
|
* Time Complexity: O(n)
|
|
1185
1189
|
* Space Complexity: O(n)
|