heap-typed 1.53.5 → 1.53.7
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 +136 -171
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +23 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/data-structures/binary-tree/bst.js +185 -66
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +6 -6
- package/dist/data-structures/heap/heap.js +6 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +49 -34
- 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/dist/data-structures/trie/trie.d.ts +104 -4
- package/dist/data-structures/trie/trie.js +116 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -121
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
|
@@ -55,13 +55,13 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
55
55
|
set prev(value: DoublyLinkedListNode<E> | undefined);
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
*1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
59
59
|
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
|
60
60
|
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
61
61
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
62
62
|
* @example
|
|
63
63
|
* // text editor operation history
|
|
64
|
-
*
|
|
64
|
+
* const actions = [
|
|
65
65
|
* { type: 'insert', content: 'first line of text' },
|
|
66
66
|
* { type: 'insert', content: 'second line of text' },
|
|
67
67
|
* { type: 'delete', content: 'delete the first line' }
|
|
@@ -73,7 +73,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
73
73
|
* console.log(editorHistory.last?.type); // 'insert'
|
|
74
74
|
* @example
|
|
75
75
|
* // Browser history
|
|
76
|
-
*
|
|
76
|
+
* const browserHistory = new DoublyLinkedList<string>();
|
|
77
77
|
*
|
|
78
78
|
* browserHistory.push('home page');
|
|
79
79
|
* browserHistory.push('search page');
|
|
@@ -84,7 +84,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
84
84
|
* console.log(browserHistory.last); // 'search page'
|
|
85
85
|
* @example
|
|
86
86
|
* // Use DoublyLinkedList to implement music player
|
|
87
|
-
*
|
|
87
|
+
* // Define the Song interface
|
|
88
88
|
* interface Song {
|
|
89
89
|
* title: string;
|
|
90
90
|
* artist: string;
|
|
@@ -209,7 +209,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
209
209
|
* // ]
|
|
210
210
|
* @example
|
|
211
211
|
* // Use DoublyLinkedList to implement LRU cache
|
|
212
|
-
*
|
|
212
|
+
* interface CacheEntry<K, V> {
|
|
213
213
|
* key: K;
|
|
214
214
|
* value: V;
|
|
215
215
|
* }
|
|
@@ -371,7 +371,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
371
371
|
* console.log(cache.isEmpty); // true
|
|
372
372
|
* @example
|
|
373
373
|
* // finding lyrics by timestamp in Coldplay's "Fix You"
|
|
374
|
-
*
|
|
374
|
+
* // Create a DoublyLinkedList to store song lyrics with timestamps
|
|
375
375
|
* const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
|
|
376
376
|
*
|
|
377
377
|
* // Detailed lyrics with precise timestamps (in milliseconds)
|
|
@@ -411,7 +411,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
411
411
|
* console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
412
412
|
* @example
|
|
413
413
|
* // cpu process schedules
|
|
414
|
-
*
|
|
414
|
+
* class Process {
|
|
415
415
|
* constructor(
|
|
416
416
|
* public id: number,
|
|
417
417
|
* public priority: number
|
|
@@ -487,6 +487,16 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
487
487
|
* console.log(scheduler.listProcesses()); // []
|
|
488
488
|
*/
|
|
489
489
|
export declare class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
|
|
490
|
+
/**
|
|
491
|
+
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
492
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
493
|
+
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
494
|
+
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
495
|
+
* empty iterable.
|
|
496
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
497
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
498
|
+
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
499
|
+
*/
|
|
490
500
|
constructor(elements?: Iterable<E> | Iterable<R>, options?: DoublyLinkedListOptions<E, R>);
|
|
491
501
|
protected _head: DoublyLinkedListNode<E> | undefined;
|
|
492
502
|
/**
|
|
@@ -709,22 +719,18 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
709
719
|
* Time Complexity: O(n)
|
|
710
720
|
* Space Complexity: O(1)
|
|
711
721
|
*
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
*
|
|
717
|
-
*
|
|
718
|
-
* list. If the element or node is found in the list, the method returns the index of that element or
|
|
719
|
-
* node. If the element or node is not found in the list, the method returns -1.
|
|
722
|
+
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
|
|
723
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
724
|
+
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
|
|
725
|
+
* can be one of the following:
|
|
726
|
+
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
|
|
727
|
+
* matches the provided element, node, or predicate. If no match is found, it returns -1.
|
|
720
728
|
*/
|
|
721
|
-
indexOf(
|
|
729
|
+
indexOf(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number;
|
|
722
730
|
/**
|
|
723
731
|
* Time Complexity: O(n)
|
|
724
732
|
* Space Complexity: O(1)
|
|
725
733
|
*
|
|
726
|
-
*/
|
|
727
|
-
/**
|
|
728
734
|
* This function retrieves an element from a doubly linked list based on a given element
|
|
729
735
|
* node or predicate.
|
|
730
736
|
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
@@ -733,7 +739,7 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
733
739
|
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
734
740
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
735
741
|
*/
|
|
736
|
-
|
|
742
|
+
search(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
|
|
737
743
|
/**
|
|
738
744
|
* Time Complexity: O(n)
|
|
739
745
|
* Space Complexity: O(1)
|
|
@@ -820,6 +826,12 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
820
826
|
* @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
|
|
821
827
|
*/
|
|
822
828
|
map<EM, RM>(callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): DoublyLinkedList<EM, RM>;
|
|
829
|
+
/**
|
|
830
|
+
* Time Complexity: O(n)
|
|
831
|
+
* Space Complexity: O(1)
|
|
832
|
+
*
|
|
833
|
+
*/
|
|
834
|
+
countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number;
|
|
823
835
|
/**
|
|
824
836
|
* Time Complexity: O(n)
|
|
825
837
|
* Space Complexity: O(n)
|
|
@@ -64,13 +64,13 @@ class DoublyLinkedListNode {
|
|
|
64
64
|
}
|
|
65
65
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
66
66
|
/**
|
|
67
|
-
*
|
|
67
|
+
*1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
68
68
|
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
|
69
69
|
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
70
70
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
71
71
|
* @example
|
|
72
72
|
* // text editor operation history
|
|
73
|
-
*
|
|
73
|
+
* const actions = [
|
|
74
74
|
* { type: 'insert', content: 'first line of text' },
|
|
75
75
|
* { type: 'insert', content: 'second line of text' },
|
|
76
76
|
* { type: 'delete', content: 'delete the first line' }
|
|
@@ -82,7 +82,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
82
82
|
* console.log(editorHistory.last?.type); // 'insert'
|
|
83
83
|
* @example
|
|
84
84
|
* // Browser history
|
|
85
|
-
*
|
|
85
|
+
* const browserHistory = new DoublyLinkedList<string>();
|
|
86
86
|
*
|
|
87
87
|
* browserHistory.push('home page');
|
|
88
88
|
* browserHistory.push('search page');
|
|
@@ -93,7 +93,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
93
93
|
* console.log(browserHistory.last); // 'search page'
|
|
94
94
|
* @example
|
|
95
95
|
* // Use DoublyLinkedList to implement music player
|
|
96
|
-
*
|
|
96
|
+
* // Define the Song interface
|
|
97
97
|
* interface Song {
|
|
98
98
|
* title: string;
|
|
99
99
|
* artist: string;
|
|
@@ -218,7 +218,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
218
218
|
* // ]
|
|
219
219
|
* @example
|
|
220
220
|
* // Use DoublyLinkedList to implement LRU cache
|
|
221
|
-
*
|
|
221
|
+
* interface CacheEntry<K, V> {
|
|
222
222
|
* key: K;
|
|
223
223
|
* value: V;
|
|
224
224
|
* }
|
|
@@ -380,7 +380,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
380
380
|
* console.log(cache.isEmpty); // true
|
|
381
381
|
* @example
|
|
382
382
|
* // finding lyrics by timestamp in Coldplay's "Fix You"
|
|
383
|
-
*
|
|
383
|
+
* // Create a DoublyLinkedList to store song lyrics with timestamps
|
|
384
384
|
* const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
|
|
385
385
|
*
|
|
386
386
|
* // Detailed lyrics with precise timestamps (in milliseconds)
|
|
@@ -420,7 +420,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
420
420
|
* console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
421
421
|
* @example
|
|
422
422
|
* // cpu process schedules
|
|
423
|
-
*
|
|
423
|
+
* class Process {
|
|
424
424
|
* constructor(
|
|
425
425
|
* public id: number,
|
|
426
426
|
* public priority: number
|
|
@@ -496,6 +496,16 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
496
496
|
* console.log(scheduler.listProcesses()); // []
|
|
497
497
|
*/
|
|
498
498
|
class DoublyLinkedList extends base_1.IterableElementBase {
|
|
499
|
+
/**
|
|
500
|
+
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
501
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
502
|
+
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
503
|
+
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
504
|
+
* empty iterable.
|
|
505
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
506
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
507
|
+
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
508
|
+
*/
|
|
499
509
|
constructor(elements = [], options) {
|
|
500
510
|
super(options);
|
|
501
511
|
this._head = undefined;
|
|
@@ -784,13 +794,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
784
794
|
* node was not found.
|
|
785
795
|
*/
|
|
786
796
|
addBefore(existingElementOrNode, newElementOrNode) {
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
}
|
|
791
|
-
else {
|
|
792
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
793
|
-
}
|
|
797
|
+
const existingNode = this.isNode(existingElementOrNode)
|
|
798
|
+
? existingElementOrNode
|
|
799
|
+
: this.getNode(existingElementOrNode);
|
|
794
800
|
if (existingNode) {
|
|
795
801
|
const newNode = this._ensureNode(newElementOrNode);
|
|
796
802
|
newNode.prev = existingNode.prev;
|
|
@@ -824,13 +830,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
824
830
|
* was not found in the linked list.
|
|
825
831
|
*/
|
|
826
832
|
addAfter(existingElementOrNode, newElementOrNode) {
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
}
|
|
831
|
-
else {
|
|
832
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
833
|
-
}
|
|
833
|
+
const existingNode = this.isNode(existingElementOrNode)
|
|
834
|
+
? existingElementOrNode
|
|
835
|
+
: this.getNode(existingElementOrNode);
|
|
834
836
|
if (existingNode) {
|
|
835
837
|
const newNode = this._ensureNode(newElementOrNode);
|
|
836
838
|
newNode.next = existingNode.next;
|
|
@@ -936,17 +938,15 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
936
938
|
* Time Complexity: O(n)
|
|
937
939
|
* Space Complexity: O(1)
|
|
938
940
|
*
|
|
939
|
-
*
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
*
|
|
943
|
-
*
|
|
944
|
-
*
|
|
945
|
-
* list. If the element or node is found in the list, the method returns the index of that element or
|
|
946
|
-
* node. If the element or node is not found in the list, the method returns -1.
|
|
941
|
+
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
|
|
942
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
943
|
+
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
|
|
944
|
+
* can be one of the following:
|
|
945
|
+
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
|
|
946
|
+
* matches the provided element, node, or predicate. If no match is found, it returns -1.
|
|
947
947
|
*/
|
|
948
|
-
indexOf(
|
|
949
|
-
const predicate = this._ensurePredicate(
|
|
948
|
+
indexOf(elementNodeOrPredicate) {
|
|
949
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
950
950
|
let index = 0;
|
|
951
951
|
let current = this.head;
|
|
952
952
|
while (current) {
|
|
@@ -962,8 +962,6 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
962
962
|
* Time Complexity: O(n)
|
|
963
963
|
* Space Complexity: O(1)
|
|
964
964
|
*
|
|
965
|
-
*/
|
|
966
|
-
/**
|
|
967
965
|
* This function retrieves an element from a doubly linked list based on a given element
|
|
968
966
|
* node or predicate.
|
|
969
967
|
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
@@ -972,7 +970,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
972
970
|
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
973
971
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
974
972
|
*/
|
|
975
|
-
|
|
973
|
+
search(elementNodeOrPredicate) {
|
|
976
974
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
977
975
|
let current = this.head;
|
|
978
976
|
while (current) {
|
|
@@ -1122,6 +1120,23 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1122
1120
|
}
|
|
1123
1121
|
return mappedList;
|
|
1124
1122
|
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Time Complexity: O(n)
|
|
1125
|
+
* Space Complexity: O(1)
|
|
1126
|
+
*
|
|
1127
|
+
*/
|
|
1128
|
+
countOccurrences(elementOrNode) {
|
|
1129
|
+
const predicate = this._ensurePredicate(elementOrNode);
|
|
1130
|
+
let count = 0;
|
|
1131
|
+
let current = this.head;
|
|
1132
|
+
while (current) {
|
|
1133
|
+
if (predicate(current)) {
|
|
1134
|
+
count++;
|
|
1135
|
+
}
|
|
1136
|
+
current = current.next;
|
|
1137
|
+
}
|
|
1138
|
+
return count;
|
|
1139
|
+
}
|
|
1125
1140
|
/**
|
|
1126
1141
|
* Time Complexity: O(n)
|
|
1127
1142
|
* Space Complexity: O(n)
|
|
@@ -72,26 +72,16 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
72
72
|
* @returns The size of the object, which is a number.
|
|
73
73
|
*/
|
|
74
74
|
get size(): number;
|
|
75
|
-
/**
|
|
76
|
-
* Time Complexity: O(n)
|
|
77
|
-
* Space Complexity: O(n)
|
|
78
|
-
*
|
|
79
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
80
|
-
* array.
|
|
81
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
82
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
83
|
-
*/
|
|
84
|
-
static fromArray<E>(data: E[]): SinglyLinkedList<E, any>;
|
|
85
75
|
/**
|
|
86
76
|
* Time Complexity: O(1)
|
|
87
77
|
* Space Complexity: O(1)
|
|
88
78
|
*
|
|
89
|
-
* The push function adds a new element to the end of a singly linked list.
|
|
90
|
-
* @param {E}
|
|
91
|
-
*
|
|
92
|
-
* @returns The `push` method is returning a boolean value, `true`.
|
|
79
|
+
* The `push` function adds a new element or node to the end of a singly linked list.
|
|
80
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
|
|
81
|
+
* method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
|
|
82
|
+
* @returns The `push` method is returning a boolean value, specifically `true`.
|
|
93
83
|
*/
|
|
94
|
-
push(
|
|
84
|
+
push(elementOrNode: E | SinglyLinkedListNode<E>): boolean;
|
|
95
85
|
/**
|
|
96
86
|
* Time Complexity: O(n)
|
|
97
87
|
* Space Complexity: O(1)
|
|
@@ -113,12 +103,27 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
113
103
|
* Time Complexity: O(1)
|
|
114
104
|
* Space Complexity: O(1)
|
|
115
105
|
*
|
|
116
|
-
* The unshift function adds a new element to the beginning of a singly linked list
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
106
|
+
* The unshift function adds a new element or node to the beginning of a singly linked list in
|
|
107
|
+
* TypeScript.
|
|
108
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
109
|
+
* `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
|
|
110
|
+
* element of type `E`.
|
|
111
|
+
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
112
|
+
*/
|
|
113
|
+
unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Time Complexity: O(n)
|
|
116
|
+
* Space Complexity: O(1)
|
|
117
|
+
*
|
|
118
|
+
* This function searches for a specific element in a singly linked list based on a given node or
|
|
119
|
+
* predicate.
|
|
120
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
121
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
|
|
122
|
+
* the following types:
|
|
123
|
+
* @returns The `get` method returns the value of the first node in the singly linked list that
|
|
124
|
+
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
120
125
|
*/
|
|
121
|
-
|
|
126
|
+
search(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): E | undefined;
|
|
122
127
|
/**
|
|
123
128
|
* Time Complexity: O(n)
|
|
124
129
|
* Space Complexity: O(1)
|
|
@@ -130,6 +135,20 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
130
135
|
* `undefined` if the index is out of bounds.
|
|
131
136
|
*/
|
|
132
137
|
at(index: number): E | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
*
|
|
142
|
+
* The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
|
|
143
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
144
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
|
|
145
|
+
* one of the following types:
|
|
146
|
+
* @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
|
|
147
|
+
* instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
|
|
148
|
+
* parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
|
|
149
|
+
* the function returns `false`.
|
|
150
|
+
*/
|
|
151
|
+
isNode(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is SinglyLinkedListNode<E>;
|
|
133
152
|
/**
|
|
134
153
|
* Time Complexity: O(n)
|
|
135
154
|
* Space Complexity: O(1)
|
|
@@ -157,25 +176,28 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
157
176
|
* Space Complexity: O(1)
|
|
158
177
|
*
|
|
159
178
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
160
|
-
* @param {E | SinglyLinkedListNode<E>}
|
|
179
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
|
|
161
180
|
* or a `SinglyLinkedListNode<E>` object.
|
|
162
181
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
163
182
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
164
183
|
*/
|
|
165
|
-
delete(
|
|
184
|
+
delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean;
|
|
166
185
|
/**
|
|
167
186
|
* Time Complexity: O(n)
|
|
168
187
|
* Space Complexity: O(1)
|
|
169
188
|
*
|
|
170
|
-
* The `addAt` function inserts a
|
|
171
|
-
* @param {number} index - The index parameter represents the position at which
|
|
172
|
-
* linked list. It is
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
|
|
178
|
-
|
|
189
|
+
* The `addAt` function inserts a new element or node at a specified index in a singly linked list.
|
|
190
|
+
* @param {number} index - The `index` parameter represents the position at which you want to add a
|
|
191
|
+
* new element or node in the linked list. It is a number that indicates the index where the new
|
|
192
|
+
* element or node should be inserted.
|
|
193
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
194
|
+
* `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
|
|
195
|
+
* parameter represents the element or node that you want to add to the linked list at the specified
|
|
196
|
+
* index.
|
|
197
|
+
* @returns The `addAt` method returns a boolean value - `true` if the element or node was
|
|
198
|
+
* successfully added at the specified index, and `false` if the index is out of bounds.
|
|
199
|
+
*/
|
|
200
|
+
addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
|
|
179
201
|
/**
|
|
180
202
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
181
203
|
* whether it is empty or not.
|
|
@@ -206,56 +228,76 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
206
228
|
* Time Complexity: O(n)
|
|
207
229
|
* Space Complexity: O(1)
|
|
208
230
|
*
|
|
209
|
-
* The `indexOf` function
|
|
210
|
-
*
|
|
211
|
-
* @
|
|
212
|
-
*
|
|
231
|
+
* The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
|
|
232
|
+
* list and returns its index if found.
|
|
233
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
234
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
|
|
235
|
+
* of the following types:
|
|
236
|
+
* @returns The `indexOf` method returns the index of the first occurrence of the element that
|
|
237
|
+
* matches the provided predicate in the singly linked list. If no matching element is found, it
|
|
238
|
+
* returns -1.
|
|
213
239
|
*/
|
|
214
|
-
indexOf(
|
|
240
|
+
indexOf(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number;
|
|
215
241
|
/**
|
|
216
242
|
* Time Complexity: O(n)
|
|
217
243
|
* Space Complexity: O(1)
|
|
218
244
|
*
|
|
219
|
-
* The function
|
|
220
|
-
*
|
|
221
|
-
* @param {E
|
|
222
|
-
*
|
|
223
|
-
*
|
|
245
|
+
* The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
|
|
246
|
+
* element, node, or predicate.
|
|
247
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
|
|
248
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
|
|
249
|
+
* of the following types:
|
|
250
|
+
* @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
|
|
251
|
+
* found based on the provided predicate, or it returns `undefined` if no matching node is found or
|
|
252
|
+
* if the input parameter is `undefined`.
|
|
224
253
|
*/
|
|
225
|
-
getNode(
|
|
254
|
+
getNode(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined): SinglyLinkedListNode<E> | undefined;
|
|
226
255
|
/**
|
|
227
256
|
* Time Complexity: O(n)
|
|
228
257
|
* Space Complexity: O(1)
|
|
229
258
|
*
|
|
230
|
-
* The `addBefore`
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
* @
|
|
235
|
-
*
|
|
236
|
-
|
|
237
|
-
|
|
259
|
+
* The function `addBefore` in TypeScript adds a new element or node before an existing element or
|
|
260
|
+
* node in a singly linked list.
|
|
261
|
+
* @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
|
|
262
|
+
* element or node in the linked list before which you want to add a new element or node.
|
|
263
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
264
|
+
* `addBefore` method represents the element or node that you want to insert before the existing
|
|
265
|
+
* element or node in the linked list. This new element can be of type `E` or a
|
|
266
|
+
* `SinglyLinkedListNode<E>`.
|
|
267
|
+
* @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
|
|
268
|
+
* successfully added before the existing element or node, and `false` if the operation was
|
|
269
|
+
* unsuccessful.
|
|
270
|
+
*/
|
|
271
|
+
addBefore(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
|
|
238
272
|
/**
|
|
239
273
|
* Time Complexity: O(n)
|
|
240
274
|
* Space Complexity: O(1)
|
|
241
275
|
*
|
|
242
|
-
* The `addAfter` function
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* @
|
|
247
|
-
*
|
|
248
|
-
|
|
249
|
-
|
|
276
|
+
* The `addAfter` function in TypeScript adds a new element or node after an existing element or node
|
|
277
|
+
* in a singly linked list.
|
|
278
|
+
* @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
|
|
279
|
+
* an element of type E or a SinglyLinkedListNode of type E.
|
|
280
|
+
* @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
281
|
+
* `addAfter` method represents the element or node that you want to add after the existing element
|
|
282
|
+
* or node in a singly linked list. This parameter can be either the value of the new element or a
|
|
283
|
+
* reference to a `SinglyLinkedListNode` containing
|
|
284
|
+
* @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
|
|
285
|
+
* successfully added after the existing element or node, and `false` if the existing element or node
|
|
286
|
+
* was not found.
|
|
287
|
+
*/
|
|
288
|
+
addAfter(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
|
|
250
289
|
/**
|
|
251
290
|
* Time Complexity: O(n)
|
|
252
291
|
* Space Complexity: O(1)
|
|
253
292
|
*
|
|
254
|
-
* The function
|
|
255
|
-
*
|
|
256
|
-
* @
|
|
293
|
+
* The function `countOccurrences` iterates through a singly linked list and counts the occurrences
|
|
294
|
+
* of a specified element or nodes that satisfy a given predicate.
|
|
295
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
|
|
296
|
+
* - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
|
|
297
|
+
* @returns The `countOccurrences` method returns the number of occurrences of the specified element,
|
|
298
|
+
* node, or predicate function in the singly linked list.
|
|
257
299
|
*/
|
|
258
|
-
countOccurrences(
|
|
300
|
+
countOccurrences(elementOrNode: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number;
|
|
259
301
|
/**
|
|
260
302
|
* Time Complexity: O(n)
|
|
261
303
|
* Space Complexity: O(n)
|
|
@@ -309,4 +351,44 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
309
351
|
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
|
|
310
352
|
*/
|
|
311
353
|
protected _getIterator(): IterableIterator<E>;
|
|
354
|
+
/**
|
|
355
|
+
* Time Complexity: O(n)
|
|
356
|
+
* Space Complexity: O(n)
|
|
357
|
+
*
|
|
358
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
359
|
+
* array.
|
|
360
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
361
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
362
|
+
*/
|
|
363
|
+
static fromArray<E>(data: E[]): SinglyLinkedList<E, any>;
|
|
364
|
+
/**
|
|
365
|
+
* The _isPredicate function in TypeScript checks if the input is a function that takes a
|
|
366
|
+
* SinglyLinkedListNode as an argument and returns a boolean.
|
|
367
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
368
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
|
|
369
|
+
* @returns The _isPredicate method is returning a boolean value based on whether the
|
|
370
|
+
* elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
|
|
371
|
+
* function, the method will return true, indicating that it is a predicate function. If it is not a
|
|
372
|
+
* function, the method will return false.
|
|
373
|
+
*/
|
|
374
|
+
protected _isPredicate(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is (node: SinglyLinkedListNode<E>) => boolean;
|
|
375
|
+
/**
|
|
376
|
+
* The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
|
|
377
|
+
* node if necessary.
|
|
378
|
+
* @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
|
|
379
|
+
* an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
|
|
380
|
+
* @returns A SinglyLinkedListNode<E> object is being returned.
|
|
381
|
+
*/
|
|
382
|
+
protected _ensureNode(elementOrNode: E | SinglyLinkedListNode<E>): SinglyLinkedListNode<E>;
|
|
383
|
+
/**
|
|
384
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
|
|
385
|
+
* function, or a value to compare with the node's value.
|
|
386
|
+
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
387
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
|
|
388
|
+
* @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
|
|
389
|
+
* function is returned that checks if a given node is equal to the input node. If the input is a
|
|
390
|
+
* predicate function, it is returned as is. If the input is neither a node nor a predicate function,
|
|
391
|
+
* a function is returned that checks if a given node's value is equal to the input
|
|
392
|
+
*/
|
|
393
|
+
protected _ensurePredicate(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): (node: SinglyLinkedListNode<E>) => boolean;
|
|
312
394
|
}
|