singly-linked-list-typed 2.4.4 → 2.5.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/README.md +15 -73
- package/dist/cjs/index.cjs +368 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +368 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +368 -76
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +368 -76
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/singly-linked-list-typed.js +368 -75
- package/dist/umd/singly-linked-list-typed.js.map +1 -1
- package/dist/umd/singly-linked-list-typed.min.js +1 -1
- package/dist/umd/singly-linked-list-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +425 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +343 -68
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +215 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
package/README.md
CHANGED
|
@@ -34,79 +34,6 @@ yarn add singly-linked-list-typed
|
|
|
34
34
|
|
|
35
35
|
[//]: # (No deletion!!! Start of Example Replace Section)
|
|
36
36
|
|
|
37
|
-
### basic SinglyLinkedList creation and push operation
|
|
38
|
-
```typescript
|
|
39
|
-
// Create a simple SinglyLinkedList with initial values
|
|
40
|
-
const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
41
|
-
|
|
42
|
-
// Verify the list maintains insertion order
|
|
43
|
-
console.log([...list]); // [1, 2, 3, 4, 5];
|
|
44
|
-
|
|
45
|
-
// Check length
|
|
46
|
-
console.log(list.length); // 5;
|
|
47
|
-
|
|
48
|
-
// Push a new element to the end
|
|
49
|
-
list.push(6);
|
|
50
|
-
console.log(list.length); // 6;
|
|
51
|
-
console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### SinglyLinkedList pop and shift operations
|
|
55
|
-
```typescript
|
|
56
|
-
const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
57
|
-
|
|
58
|
-
// Pop removes from the end
|
|
59
|
-
const last = list.pop();
|
|
60
|
-
console.log(last); // 50;
|
|
61
|
-
|
|
62
|
-
// Shift removes from the beginning
|
|
63
|
-
const first = list.shift();
|
|
64
|
-
console.log(first); // 10;
|
|
65
|
-
|
|
66
|
-
// Verify remaining elements
|
|
67
|
-
console.log([...list]); // [20, 30, 40];
|
|
68
|
-
console.log(list.length); // 3;
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### SinglyLinkedList unshift and forward traversal
|
|
72
|
-
```typescript
|
|
73
|
-
const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
74
|
-
|
|
75
|
-
// Unshift adds to the beginning
|
|
76
|
-
list.unshift(10);
|
|
77
|
-
console.log([...list]); // [10, 20, 30, 40];
|
|
78
|
-
|
|
79
|
-
// Access elements (forward traversal only for singly linked)
|
|
80
|
-
const second = list.at(1);
|
|
81
|
-
console.log(second); // 20;
|
|
82
|
-
|
|
83
|
-
// SinglyLinkedList allows forward iteration only
|
|
84
|
-
const elements: number[] = [];
|
|
85
|
-
for (const item of list) {
|
|
86
|
-
elements.push(item);
|
|
87
|
-
}
|
|
88
|
-
console.log(elements); // [10, 20, 30, 40];
|
|
89
|
-
|
|
90
|
-
console.log(list.length); // 4;
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### SinglyLinkedList filter and map operations
|
|
94
|
-
```typescript
|
|
95
|
-
const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
96
|
-
|
|
97
|
-
// Filter even numbers
|
|
98
|
-
const filtered = list.filter(value => value % 2 === 0);
|
|
99
|
-
console.log(filtered.length); // 2;
|
|
100
|
-
|
|
101
|
-
// Map to double values
|
|
102
|
-
const doubled = list.map(value => value * 2);
|
|
103
|
-
console.log(doubled.length); // 5;
|
|
104
|
-
|
|
105
|
-
// Use reduce to sum
|
|
106
|
-
const sum = list.reduce((acc, value) => acc + value, 0);
|
|
107
|
-
console.log(sum); // 15;
|
|
108
|
-
```
|
|
109
|
-
|
|
110
37
|
### SinglyLinkedList for sequentially processed data stream
|
|
111
38
|
```typescript
|
|
112
39
|
interface LogEntry {
|
|
@@ -228,6 +155,21 @@ yarn add singly-linked-list-typed
|
|
|
228
155
|
console.log(editor.getText()); // 'Haello';
|
|
229
156
|
```
|
|
230
157
|
|
|
158
|
+
### Find first matching element
|
|
159
|
+
```typescript
|
|
160
|
+
const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
161
|
+
console.log(list.find(n => n > 3)); // 4;
|
|
162
|
+
console.log(list.find(n => n > 10)); // undefined;
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Iterate over elements
|
|
166
|
+
```typescript
|
|
167
|
+
const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
168
|
+
const result: number[] = [];
|
|
169
|
+
list.forEach(n => result.push(n));
|
|
170
|
+
console.log(result); // [10, 20, 30];
|
|
171
|
+
```
|
|
172
|
+
|
|
231
173
|
[//]: # (No deletion!!! End of Example Replace Section)
|
|
232
174
|
|
|
233
175
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -725,11 +725,37 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
725
725
|
return list;
|
|
726
726
|
}
|
|
727
727
|
/**
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
728
|
+
* Append an element/node to the tail.
|
|
729
|
+
* @remarks Time O(1), Space O(1)
|
|
730
|
+
* @param elementOrNode - Element or node to append.
|
|
731
|
+
* @returns True when appended.
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
* @example
|
|
744
|
+
* // basic SinglyLinkedList creation and push operation
|
|
745
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
746
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
747
|
+
*
|
|
748
|
+
* // Verify the list maintains insertion order
|
|
749
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
750
|
+
*
|
|
751
|
+
* // Check length
|
|
752
|
+
* console.log(list.length); // 5;
|
|
753
|
+
*
|
|
754
|
+
* // Push a new element to the end
|
|
755
|
+
* list.push(6);
|
|
756
|
+
* console.log(list.length); // 6;
|
|
757
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
758
|
+
*/
|
|
733
759
|
push(elementOrNode) {
|
|
734
760
|
const newNode = this._ensureNode(elementOrNode);
|
|
735
761
|
if (!this.head) {
|
|
@@ -743,10 +769,36 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
743
769
|
return true;
|
|
744
770
|
}
|
|
745
771
|
/**
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
772
|
+
* Remove and return the tail element.
|
|
773
|
+
* @remarks Time O(N), Space O(1)
|
|
774
|
+
* @returns Removed element or undefined.
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
* @example
|
|
787
|
+
* // SinglyLinkedList pop and shift operations
|
|
788
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
789
|
+
*
|
|
790
|
+
* // Pop removes from the end
|
|
791
|
+
* const last = list.pop();
|
|
792
|
+
* console.log(last); // 50;
|
|
793
|
+
*
|
|
794
|
+
* // Shift removes from the beginning
|
|
795
|
+
* const first = list.shift();
|
|
796
|
+
* console.log(first); // 10;
|
|
797
|
+
*
|
|
798
|
+
* // Verify remaining elements
|
|
799
|
+
* console.log([...list]); // [20, 30, 40];
|
|
800
|
+
* console.log(list.length); // 3;
|
|
801
|
+
*/
|
|
750
802
|
pop() {
|
|
751
803
|
if (!this.head) return void 0;
|
|
752
804
|
if (this.head === this.tail) {
|
|
@@ -765,10 +817,26 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
765
817
|
return value;
|
|
766
818
|
}
|
|
767
819
|
/**
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
820
|
+
* Remove and return the head element.
|
|
821
|
+
* @remarks Time O(1), Space O(1)
|
|
822
|
+
* @returns Removed element or undefined.
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
* @example
|
|
835
|
+
* // Remove from the front
|
|
836
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
837
|
+
* console.log(list.shift()); // 10;
|
|
838
|
+
* console.log(list.length); // 2;
|
|
839
|
+
*/
|
|
772
840
|
shift() {
|
|
773
841
|
if (!this.head) return void 0;
|
|
774
842
|
const removed = this.head;
|
|
@@ -778,11 +846,42 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
778
846
|
return removed.value;
|
|
779
847
|
}
|
|
780
848
|
/**
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
849
|
+
* Prepend an element/node to the head.
|
|
850
|
+
* @remarks Time O(1), Space O(1)
|
|
851
|
+
* @param elementOrNode - Element or node to prepend.
|
|
852
|
+
* @returns True when prepended.
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
* @example
|
|
865
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
866
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
867
|
+
*
|
|
868
|
+
* // Unshift adds to the beginning
|
|
869
|
+
* list.unshift(10);
|
|
870
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
871
|
+
*
|
|
872
|
+
* // Access elements (forward traversal only for singly linked)
|
|
873
|
+
* const second = list.at(1);
|
|
874
|
+
* console.log(second); // 20;
|
|
875
|
+
*
|
|
876
|
+
* // SinglyLinkedList allows forward iteration only
|
|
877
|
+
* const elements: number[] = [];
|
|
878
|
+
* for (const item of list) {
|
|
879
|
+
* elements.push(item);
|
|
880
|
+
* }
|
|
881
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
882
|
+
*
|
|
883
|
+
* console.log(list.length); // 4;
|
|
884
|
+
*/
|
|
786
885
|
unshift(elementOrNode) {
|
|
787
886
|
const newNode = this._ensureNode(elementOrNode);
|
|
788
887
|
if (!this.head) {
|
|
@@ -838,11 +937,28 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
838
937
|
return void 0;
|
|
839
938
|
}
|
|
840
939
|
/**
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
940
|
+
* Get the element at a given index.
|
|
941
|
+
* @remarks Time O(N), Space O(1)
|
|
942
|
+
* @param index - Zero-based index.
|
|
943
|
+
* @returns Element or undefined.
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
* @example
|
|
956
|
+
* // Access element by index
|
|
957
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
958
|
+
* console.log(list.at(0)); // 'a';
|
|
959
|
+
* console.log(list.at(2)); // 'c';
|
|
960
|
+
* console.log(list.at(3)); // 'd';
|
|
961
|
+
*/
|
|
846
962
|
at(index) {
|
|
847
963
|
if (index < 0 || index >= this._length) return void 0;
|
|
848
964
|
let current = this.head;
|
|
@@ -859,11 +975,23 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
859
975
|
return elementNodeOrPredicate instanceof SinglyLinkedListNode;
|
|
860
976
|
}
|
|
861
977
|
/**
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
978
|
+
* Get the node reference at a given index.
|
|
979
|
+
* @remarks Time O(N), Space O(1)
|
|
980
|
+
* @param index - Zero-based index.
|
|
981
|
+
* @returns Node or undefined.
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
* @example
|
|
991
|
+
* // Get node at index
|
|
992
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
993
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
994
|
+
*/
|
|
867
995
|
getNodeAt(index) {
|
|
868
996
|
if (index < 0 || index >= this._length) return void 0;
|
|
869
997
|
let current = this.head;
|
|
@@ -871,11 +999,24 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
871
999
|
return current;
|
|
872
1000
|
}
|
|
873
1001
|
/**
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
1002
|
+
* Delete the element at an index.
|
|
1003
|
+
* @remarks Time O(N), Space O(1)
|
|
1004
|
+
* @param index - Zero-based index.
|
|
1005
|
+
* @returns Removed element or undefined.
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
* @example
|
|
1015
|
+
* // Remove by index
|
|
1016
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
1017
|
+
* list.deleteAt(1);
|
|
1018
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
1019
|
+
*/
|
|
879
1020
|
deleteAt(index) {
|
|
880
1021
|
if (index < 0 || index >= this._length) return void 0;
|
|
881
1022
|
if (index === 0) return this.shift();
|
|
@@ -888,11 +1029,24 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
888
1029
|
return value;
|
|
889
1030
|
}
|
|
890
1031
|
/**
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
1032
|
+
* Delete the first match by value/node.
|
|
1033
|
+
* @remarks Time O(N), Space O(1)
|
|
1034
|
+
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
1035
|
+
* @returns True if removed.
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
* @example
|
|
1045
|
+
* // Remove first occurrence
|
|
1046
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
1047
|
+
* list.delete(2);
|
|
1048
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
1049
|
+
*/
|
|
896
1050
|
delete(elementOrNode) {
|
|
897
1051
|
if (elementOrNode === void 0 || !this.head) return false;
|
|
898
1052
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
@@ -909,12 +1063,25 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
909
1063
|
return true;
|
|
910
1064
|
}
|
|
911
1065
|
/**
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
1066
|
+
* Insert a new element/node at an index, shifting following nodes.
|
|
1067
|
+
* @remarks Time O(N), Space O(1)
|
|
1068
|
+
* @param index - Zero-based index.
|
|
1069
|
+
* @param newElementOrNode - Element or node to insert.
|
|
1070
|
+
* @returns True if inserted.
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
* @example
|
|
1080
|
+
* // Insert at index
|
|
1081
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
1082
|
+
* list.addAt(1, 2);
|
|
1083
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
1084
|
+
*/
|
|
918
1085
|
addAt(index, newElementOrNode) {
|
|
919
1086
|
if (index < 0 || index > this._length) return false;
|
|
920
1087
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -940,28 +1107,70 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
940
1107
|
return true;
|
|
941
1108
|
}
|
|
942
1109
|
/**
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1110
|
+
* Check whether the list is empty.
|
|
1111
|
+
* @remarks Time O(1), Space O(1)
|
|
1112
|
+
* @returns True if length is 0.
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
* @example
|
|
1123
|
+
* // Check empty
|
|
1124
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1125
|
+
*/
|
|
947
1126
|
isEmpty() {
|
|
948
1127
|
return this._length === 0;
|
|
949
1128
|
}
|
|
950
1129
|
/**
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
1130
|
+
* Remove all nodes and reset length.
|
|
1131
|
+
* @remarks Time O(N), Space O(1)
|
|
1132
|
+
* @returns void
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
* @example
|
|
1143
|
+
* // Remove all
|
|
1144
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1145
|
+
* list.clear();
|
|
1146
|
+
* console.log(list.isEmpty()); // true;
|
|
1147
|
+
*/
|
|
955
1148
|
clear() {
|
|
956
1149
|
this._head = void 0;
|
|
957
1150
|
this._tail = void 0;
|
|
958
1151
|
this._length = 0;
|
|
959
1152
|
}
|
|
960
1153
|
/**
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
1154
|
+
* Reverse the list in place.
|
|
1155
|
+
* @remarks Time O(N), Space O(1)
|
|
1156
|
+
* @returns This list.
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
* @example
|
|
1169
|
+
* // Reverse the list in-place
|
|
1170
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1171
|
+
* list.reverse();
|
|
1172
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1173
|
+
*/
|
|
965
1174
|
reverse() {
|
|
966
1175
|
if (!this.head || this.head === this.tail) return this;
|
|
967
1176
|
let prev;
|
|
@@ -1136,22 +1345,64 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1136
1345
|
return false;
|
|
1137
1346
|
}
|
|
1138
1347
|
/**
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1348
|
+
* Deep clone this list (values are copied by reference).
|
|
1349
|
+
* @remarks Time O(N), Space O(N)
|
|
1350
|
+
* @returns A new list with the same element sequence.
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
* @example
|
|
1361
|
+
* // Deep copy
|
|
1362
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1363
|
+
* const copy = list.clone();
|
|
1364
|
+
* copy.pop();
|
|
1365
|
+
* console.log(list.length); // 3;
|
|
1366
|
+
* console.log(copy.length); // 2;
|
|
1367
|
+
*/
|
|
1143
1368
|
clone() {
|
|
1144
1369
|
const out = this._createInstance();
|
|
1145
1370
|
for (const v of this) out.push(v);
|
|
1146
1371
|
return out;
|
|
1147
1372
|
}
|
|
1148
1373
|
/**
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1374
|
+
* Filter values into a new list of the same class.
|
|
1375
|
+
* @remarks Time O(N), Space O(N)
|
|
1376
|
+
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1377
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1378
|
+
* @returns A new list with kept values.
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
|
|
1386
|
+
|
|
1387
|
+
|
|
1388
|
+
|
|
1389
|
+
|
|
1390
|
+
* @example
|
|
1391
|
+
* // SinglyLinkedList filter and map operations
|
|
1392
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1393
|
+
*
|
|
1394
|
+
* // Filter even numbers
|
|
1395
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1396
|
+
* console.log(filtered.length); // 2;
|
|
1397
|
+
*
|
|
1398
|
+
* // Map to double values
|
|
1399
|
+
* const doubled = list.map(value => value * 2);
|
|
1400
|
+
* console.log(doubled.length); // 5;
|
|
1401
|
+
*
|
|
1402
|
+
* // Use reduce to sum
|
|
1403
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1404
|
+
* console.log(sum); // 15;
|
|
1405
|
+
*/
|
|
1155
1406
|
filter(callback, thisArg) {
|
|
1156
1407
|
const out = this._createInstance();
|
|
1157
1408
|
let index = 0;
|
|
@@ -1175,15 +1426,31 @@ var SinglyLinkedList = class extends LinearLinkedBase {
|
|
|
1175
1426
|
return out;
|
|
1176
1427
|
}
|
|
1177
1428
|
/**
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1429
|
+
* Map values into a new list (possibly different element type).
|
|
1430
|
+
* @remarks Time O(N), Space O(N)
|
|
1431
|
+
* @template EM
|
|
1432
|
+
* @template RM
|
|
1433
|
+
* @param callback - Mapping function (value, index, list) → newElement.
|
|
1434
|
+
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1435
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1436
|
+
* @returns A new SinglyLinkedList with mapped values.
|
|
1437
|
+
|
|
1438
|
+
|
|
1439
|
+
|
|
1440
|
+
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
|
|
1445
|
+
|
|
1446
|
+
|
|
1447
|
+
|
|
1448
|
+
* @example
|
|
1449
|
+
* // Transform elements
|
|
1450
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1451
|
+
* const doubled = list.map(n => n * 2);
|
|
1452
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1453
|
+
*/
|
|
1187
1454
|
map(callback, options, thisArg) {
|
|
1188
1455
|
const out = this._createLike([], { ...options ?? {}, maxLen: this._maxLen });
|
|
1189
1456
|
let index = 0;
|
|
@@ -1318,6 +1585,31 @@ function elementOrPredicate(input, equals) {
|
|
|
1318
1585
|
}
|
|
1319
1586
|
__name(elementOrPredicate, "elementOrPredicate");
|
|
1320
1587
|
|
|
1588
|
+
// src/common/error.ts
|
|
1589
|
+
var ERR = {
|
|
1590
|
+
// Range / index
|
|
1591
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
1592
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
1593
|
+
// Type / argument
|
|
1594
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
1595
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
1596
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
1597
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
1598
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
1599
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
1600
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
1601
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
1602
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
1603
|
+
// State / operation
|
|
1604
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
1605
|
+
// Matrix
|
|
1606
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
1607
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
1608
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
1609
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
1610
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
1611
|
+
};
|
|
1612
|
+
|
|
1321
1613
|
// src/common/index.ts
|
|
1322
1614
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1323
1615
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
@@ -1350,6 +1642,7 @@ var Range = class {
|
|
|
1350
1642
|
*/
|
|
1351
1643
|
|
|
1352
1644
|
exports.DFSOperation = DFSOperation;
|
|
1645
|
+
exports.ERR = ERR;
|
|
1353
1646
|
exports.Range = Range;
|
|
1354
1647
|
exports.SinglyLinkedList = SinglyLinkedList;
|
|
1355
1648
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|