deque-typed 1.53.4 → 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 +2 -13
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +163 -76
- package/dist/data-structures/linked-list/doubly-linked-list.js +211 -104
- 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 +232 -105
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
|
@@ -422,19 +422,19 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
422
422
|
* // Test different scenarios of lyric synchronization
|
|
423
423
|
*
|
|
424
424
|
* // 1. Find lyric at exact timestamp
|
|
425
|
-
* const exactTimeLyric = lyricsList.
|
|
425
|
+
* const exactTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 36000);
|
|
426
426
|
* console.log(exactTimeLyric?.text); // 'And ignite your bones'
|
|
427
427
|
*
|
|
428
428
|
* // 2. Find lyric between timestamps
|
|
429
|
-
* const betweenTimeLyric = lyricsList.
|
|
429
|
+
* const betweenTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 22000);
|
|
430
430
|
* console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
|
|
431
431
|
*
|
|
432
432
|
* // 3. Find first lyric when timestamp is less than first entry
|
|
433
|
-
* const earlyTimeLyric = lyricsList.
|
|
433
|
+
* const earlyTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= -1000);
|
|
434
434
|
* console.log(earlyTimeLyric); // undefined
|
|
435
435
|
*
|
|
436
436
|
* // 4. Find last lyric when timestamp is after last entry
|
|
437
|
-
* const lateTimeLyric = lyricsList.
|
|
437
|
+
* const lateTimeLyric = lyricsList.getBackward(lyric => lyric.value.time <= 50000);
|
|
438
438
|
* console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
439
439
|
* @example
|
|
440
440
|
* // cpu process schedules
|
|
@@ -582,29 +582,36 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
582
582
|
}
|
|
583
583
|
|
|
584
584
|
/**
|
|
585
|
-
* Time Complexity: O(
|
|
586
|
-
* Space Complexity: O(
|
|
585
|
+
* Time Complexity: O(1)
|
|
586
|
+
* Space Complexity: O(1)
|
|
587
587
|
*
|
|
588
|
-
* The `
|
|
589
|
-
*
|
|
590
|
-
* @param {E
|
|
591
|
-
*
|
|
588
|
+
* The function `isNode` in TypeScript checks if a given input is an instance of
|
|
589
|
+
* `DoublyLinkedListNode`.
|
|
590
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
591
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
|
|
592
|
+
* be one of the following types:
|
|
593
|
+
* @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
|
|
594
|
+
* instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
|
|
595
|
+
* parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
|
|
596
|
+
* the function returns `false`.
|
|
592
597
|
*/
|
|
593
|
-
|
|
594
|
-
|
|
598
|
+
isNode(
|
|
599
|
+
elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
600
|
+
): elementNodeOrPredicate is DoublyLinkedListNode<E> {
|
|
601
|
+
return elementNodeOrPredicate instanceof DoublyLinkedListNode;
|
|
595
602
|
}
|
|
596
603
|
|
|
597
604
|
/**
|
|
598
605
|
* Time Complexity: O(1)
|
|
599
606
|
* Space Complexity: O(1)
|
|
600
607
|
*
|
|
601
|
-
* The push function adds a new element to the end of a doubly linked list.
|
|
602
|
-
* @param {E}
|
|
603
|
-
*
|
|
604
|
-
* @returns The `push` method is returning a boolean value, `true`.
|
|
608
|
+
* The `push` function adds a new element or node to the end of a doubly linked list.
|
|
609
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
|
|
610
|
+
* method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
611
|
+
* @returns The `push` method is returning a boolean value, specifically `true`.
|
|
605
612
|
*/
|
|
606
|
-
push(
|
|
607
|
-
const newNode =
|
|
613
|
+
push(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
614
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
608
615
|
if (!this.head) {
|
|
609
616
|
this._head = newNode;
|
|
610
617
|
this._tail = newNode;
|
|
@@ -663,13 +670,14 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
663
670
|
* Time Complexity: O(1)
|
|
664
671
|
* Space Complexity: O(1)
|
|
665
672
|
*
|
|
666
|
-
* The unshift function adds a new element to the beginning of a doubly linked list.
|
|
667
|
-
* @param {E}
|
|
668
|
-
*
|
|
669
|
-
*
|
|
673
|
+
* The unshift function adds a new element or node to the beginning of a doubly linked list.
|
|
674
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
675
|
+
* `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
|
|
676
|
+
* element of type `E`.
|
|
677
|
+
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
670
678
|
*/
|
|
671
|
-
unshift(
|
|
672
|
-
const newNode =
|
|
679
|
+
unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
680
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
673
681
|
if (!this.head) {
|
|
674
682
|
this._head = newNode;
|
|
675
683
|
this._tail = newNode;
|
|
@@ -725,17 +733,29 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
725
733
|
* Time Complexity: O(n)
|
|
726
734
|
* Space Complexity: O(1)
|
|
727
735
|
*
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
* @param {E
|
|
731
|
-
*
|
|
732
|
-
*
|
|
736
|
+
* This TypeScript function searches for a node in a doubly linked list based on a given element node
|
|
737
|
+
* or predicate.
|
|
738
|
+
* @param {| E
|
|
739
|
+
* | DoublyLinkedListNode<E>
|
|
740
|
+
* | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
741
|
+
* | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
|
|
742
|
+
* node in a doubly linked list based on a given element, node, or predicate function. The
|
|
743
|
+
* `elementNodeOrPredicate` parameter can be one of the following:
|
|
744
|
+
* @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
|
|
745
|
+
* input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
|
|
746
|
+
* Otherwise, it iterates through the linked list starting from the head node and applies the
|
|
747
|
+
* provided predicate function to each node. If a node satisfies the predicate, that node is
|
|
748
|
+
* returned. If
|
|
733
749
|
*/
|
|
734
|
-
getNode(
|
|
750
|
+
getNode(
|
|
751
|
+
elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean) | undefined
|
|
752
|
+
): DoublyLinkedListNode<E> | undefined {
|
|
753
|
+
if (elementNodeOrPredicate === undefined) return;
|
|
754
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
735
755
|
let current = this.head;
|
|
736
756
|
|
|
737
757
|
while (current) {
|
|
738
|
-
if (current
|
|
758
|
+
if (predicate(current)) {
|
|
739
759
|
return current;
|
|
740
760
|
}
|
|
741
761
|
current = current.next;
|
|
@@ -748,26 +768,28 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
748
768
|
* Time Complexity: O(n)
|
|
749
769
|
* Space Complexity: O(1)
|
|
750
770
|
*
|
|
751
|
-
* The `
|
|
752
|
-
* @param {number} index - The index parameter
|
|
753
|
-
*
|
|
754
|
-
*
|
|
755
|
-
*
|
|
756
|
-
*
|
|
757
|
-
* if the
|
|
771
|
+
* The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
|
|
772
|
+
* @param {number} index - The `index` parameter in the `addAt` method represents the position at
|
|
773
|
+
* which you want to add a new element or node in the doubly linked list. It indicates the location
|
|
774
|
+
* where the new element or node should be inserted.
|
|
775
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
776
|
+
* `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
777
|
+
* @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
|
|
778
|
+
* successfully added at the specified index, and `false` if the index is out of bounds (less than 0
|
|
779
|
+
* or greater than the size of the list).
|
|
758
780
|
*/
|
|
759
|
-
addAt(index: number,
|
|
781
|
+
addAt(index: number, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
760
782
|
if (index < 0 || index > this._size) return false;
|
|
761
783
|
if (index === 0) {
|
|
762
|
-
this.unshift(
|
|
784
|
+
this.unshift(newElementOrNode);
|
|
763
785
|
return true;
|
|
764
786
|
}
|
|
765
787
|
if (index === this._size) {
|
|
766
|
-
this.push(
|
|
788
|
+
this.push(newElementOrNode);
|
|
767
789
|
return true;
|
|
768
790
|
}
|
|
769
791
|
|
|
770
|
-
const newNode =
|
|
792
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
771
793
|
const prevNode = this.getNodeAt(index - 1);
|
|
772
794
|
const nextNode = prevNode!.next;
|
|
773
795
|
newNode.prev = prevNode;
|
|
@@ -782,26 +804,25 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
782
804
|
* Time Complexity: O(1) or O(n)
|
|
783
805
|
* Space Complexity: O(1)
|
|
784
806
|
*
|
|
785
|
-
* The `addBefore` function
|
|
786
|
-
*
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
* @param {E}
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
*
|
|
807
|
+
* The `addBefore` function in TypeScript adds a new element or node before an existing element or
|
|
808
|
+
* node in a doubly linked list.
|
|
809
|
+
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
|
|
810
|
+
* in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
|
|
811
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
|
|
812
|
+
* represents the element or node that you want to add before the `existingElementOrNode` in a doubly
|
|
813
|
+
* linked list.
|
|
814
|
+
* @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
|
|
815
|
+
* successfully added before the existing element or node, and `false` if the existing element or
|
|
816
|
+
* node was not found.
|
|
793
817
|
*/
|
|
794
|
-
addBefore(
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
} else {
|
|
800
|
-
existingNode = this.getNode(existingValueOrNode);
|
|
801
|
-
}
|
|
818
|
+
addBefore(
|
|
819
|
+
existingElementOrNode: E | DoublyLinkedListNode<E>,
|
|
820
|
+
newElementOrNode: E | DoublyLinkedListNode<E>
|
|
821
|
+
): boolean {
|
|
822
|
+
const existingNode: DoublyLinkedListNode<E> | undefined = this.getNode(existingElementOrNode);
|
|
802
823
|
|
|
803
824
|
if (existingNode) {
|
|
804
|
-
const newNode =
|
|
825
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
805
826
|
newNode.prev = existingNode.prev;
|
|
806
827
|
if (existingNode.prev) {
|
|
807
828
|
existingNode.prev.next = newNode;
|
|
@@ -822,25 +843,23 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
822
843
|
* Time Complexity: O(1) or O(n)
|
|
823
844
|
* Space Complexity: O(1)
|
|
824
845
|
*
|
|
825
|
-
* The `addAfter` function
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
* @param {E}
|
|
830
|
-
*
|
|
831
|
-
*
|
|
846
|
+
* The `addAfter` function in TypeScript adds a new element or node after an existing element or node
|
|
847
|
+
* in a doubly linked list.
|
|
848
|
+
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
|
|
849
|
+
* element or node in the doubly linked list after which you want to add a new element or node.
|
|
850
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
851
|
+
* `addAfter` method represents the element or node that you want to add after the existing element
|
|
852
|
+
* or node in a doubly linked list. This parameter can be either an element value or a
|
|
853
|
+
* `DoublyLinkedListNode` object that you want to insert
|
|
854
|
+
* @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
|
|
855
|
+
* successfully added after the existing element or node, and `false` if the existing element or node
|
|
856
|
+
* was not found in the linked list.
|
|
832
857
|
*/
|
|
833
|
-
addAfter(
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
837
|
-
existingNode = existingValueOrNode;
|
|
838
|
-
} else {
|
|
839
|
-
existingNode = this.getNode(existingValueOrNode);
|
|
840
|
-
}
|
|
858
|
+
addAfter(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
859
|
+
const existingNode: DoublyLinkedListNode<E> | undefined = this.getNode(existingElementOrNode);
|
|
841
860
|
|
|
842
861
|
if (existingNode) {
|
|
843
|
-
const newNode =
|
|
862
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
844
863
|
newNode.next = existingNode.next;
|
|
845
864
|
if (existingNode.next) {
|
|
846
865
|
existingNode.next.prev = newNode;
|
|
@@ -891,20 +910,17 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
891
910
|
* Time Complexity: O(1) or O(n)
|
|
892
911
|
* Space Complexity: O(1)
|
|
893
912
|
*
|
|
894
|
-
* The `delete` function removes a node from a doubly linked list
|
|
895
|
-
* @param {E | DoublyLinkedListNode<E>}
|
|
896
|
-
* a `DoublyLinkedListNode
|
|
897
|
-
*
|
|
898
|
-
*
|
|
913
|
+
* The `delete` function removes a specified element or node from a doubly linked list if it exists.
|
|
914
|
+
* @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
|
|
915
|
+
* the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
|
|
916
|
+
* can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
|
|
917
|
+
* doubly linked list
|
|
918
|
+
* @returns The `delete` method returns a boolean value - `true` if the element or node was
|
|
919
|
+
* successfully deleted from the doubly linked list, and `false` if the element or node was not found
|
|
920
|
+
* in the list.
|
|
899
921
|
*/
|
|
900
|
-
delete(
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
904
|
-
node = valOrNode;
|
|
905
|
-
} else {
|
|
906
|
-
node = this.getNode(valOrNode);
|
|
907
|
-
}
|
|
922
|
+
delete(elementOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
|
|
923
|
+
const node: DoublyLinkedListNode<E> | undefined = this.getNode(elementOrNode);
|
|
908
924
|
|
|
909
925
|
if (node) {
|
|
910
926
|
if (node === this.head) {
|
|
@@ -950,17 +966,19 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
950
966
|
* Time Complexity: O(n)
|
|
951
967
|
* Space Complexity: O(1)
|
|
952
968
|
*
|
|
953
|
-
*
|
|
954
|
-
* @param {E
|
|
955
|
-
*
|
|
956
|
-
*
|
|
957
|
-
*
|
|
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.
|
|
958
975
|
*/
|
|
959
|
-
indexOf(
|
|
976
|
+
indexOf(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number {
|
|
977
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
960
978
|
let index = 0;
|
|
961
979
|
let current = this.head;
|
|
962
980
|
while (current) {
|
|
963
|
-
if (current
|
|
981
|
+
if (predicate(current)) {
|
|
964
982
|
return index;
|
|
965
983
|
}
|
|
966
984
|
index++;
|
|
@@ -973,19 +991,46 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
973
991
|
* Time Complexity: O(n)
|
|
974
992
|
* Space Complexity: O(1)
|
|
975
993
|
*
|
|
976
|
-
*
|
|
977
|
-
*
|
|
978
|
-
* @param
|
|
979
|
-
*
|
|
980
|
-
*
|
|
981
|
-
*
|
|
994
|
+
* This function retrieves an element from a doubly linked list based on a given element
|
|
995
|
+
* node or predicate.
|
|
996
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
997
|
+
* elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
|
|
998
|
+
* which can be one of the following types:
|
|
999
|
+
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
1000
|
+
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
982
1001
|
*/
|
|
983
|
-
|
|
1002
|
+
get(
|
|
1003
|
+
elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
1004
|
+
): E | undefined {
|
|
1005
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1006
|
+
let current = this.head;
|
|
1007
|
+
while (current) {
|
|
1008
|
+
if (predicate(current)) return current.value;
|
|
1009
|
+
current = current.next;
|
|
1010
|
+
}
|
|
1011
|
+
return undefined;
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
/**
|
|
1015
|
+
* Time Complexity: O(n)
|
|
1016
|
+
* Space Complexity: O(1)
|
|
1017
|
+
*
|
|
1018
|
+
* The `getBackward` function searches for a specific element in a doubly linked list starting from
|
|
1019
|
+
* the tail and moving backwards.
|
|
1020
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1021
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
|
|
1022
|
+
* function can be one of the following types:
|
|
1023
|
+
* @returns The `getBackward` method returns the value of the element node that matches the provided
|
|
1024
|
+
* predicate when traversing the doubly linked list backwards. If no matching element is found, it
|
|
1025
|
+
* returns `undefined`.
|
|
1026
|
+
*/
|
|
1027
|
+
getBackward(
|
|
1028
|
+
elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
1029
|
+
): E | undefined {
|
|
1030
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
984
1031
|
let current = this.tail;
|
|
985
1032
|
while (current) {
|
|
986
|
-
if (
|
|
987
|
-
return current.value;
|
|
988
|
-
}
|
|
1033
|
+
if (predicate(current)) return current.value;
|
|
989
1034
|
current = current.prev;
|
|
990
1035
|
}
|
|
991
1036
|
return undefined;
|
|
@@ -1119,6 +1164,39 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
1119
1164
|
return mappedList;
|
|
1120
1165
|
}
|
|
1121
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
|
+
|
|
1187
|
+
/**
|
|
1188
|
+
* Time Complexity: O(n)
|
|
1189
|
+
* Space Complexity: O(n)
|
|
1190
|
+
*
|
|
1191
|
+
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
1192
|
+
* given array.
|
|
1193
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
1194
|
+
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
1195
|
+
*/
|
|
1196
|
+
static fromArray<E>(data: E[]) {
|
|
1197
|
+
return new DoublyLinkedList<E>(data);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1122
1200
|
/**
|
|
1123
1201
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
1124
1202
|
*/
|
|
@@ -1130,4 +1208,53 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
1130
1208
|
current = current.next;
|
|
1131
1209
|
}
|
|
1132
1210
|
}
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
|
|
1214
|
+
* as an argument and returns a boolean.
|
|
1215
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1216
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
1217
|
+
* types:
|
|
1218
|
+
* @returns The _isPredicate method is returning a boolean value indicating whether the
|
|
1219
|
+
* elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
|
|
1220
|
+
* function, the method will return true, indicating that it is a predicate function.
|
|
1221
|
+
*/
|
|
1222
|
+
protected _isPredicate(
|
|
1223
|
+
elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
1224
|
+
): elementNodeOrPredicate is (node: DoublyLinkedListNode<E>) => boolean {
|
|
1225
|
+
return typeof elementNodeOrPredicate === 'function';
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
/**
|
|
1229
|
+
* The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
|
|
1230
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
|
|
1231
|
+
* an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
|
|
1232
|
+
* @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
|
|
1233
|
+
* as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
|
|
1234
|
+
* value and returned.
|
|
1235
|
+
*/
|
|
1236
|
+
protected _ensureNode(elementOrNode: E | DoublyLinkedListNode<E>) {
|
|
1237
|
+
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
1238
|
+
|
|
1239
|
+
return new DoublyLinkedListNode<E>(elementOrNode);
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
/**
|
|
1243
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
|
|
1244
|
+
* function, or a value to compare with the node's value.
|
|
1245
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1246
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
1247
|
+
* types:
|
|
1248
|
+
* @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
|
|
1249
|
+
* returns a boolean value based on the conditions specified in the code.
|
|
1250
|
+
*/
|
|
1251
|
+
protected _ensurePredicate(
|
|
1252
|
+
elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
1253
|
+
) {
|
|
1254
|
+
if (this.isNode(elementNodeOrPredicate)) return (node: DoublyLinkedListNode<E>) => node === elementNodeOrPredicate;
|
|
1255
|
+
|
|
1256
|
+
if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
1257
|
+
|
|
1258
|
+
return (node: DoublyLinkedListNode<E>) => node.value === elementNodeOrPredicate;
|
|
1259
|
+
}
|
|
1133
1260
|
}
|