data-structure-typed 1.12.11 → 1.15.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.
Files changed (81) hide show
  1. package/README.md +278 -179
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +14 -5
  3. package/dist/data-structures/binary-tree/avl-tree.js +15 -6
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +11 -2
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +11 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +72 -18
  7. package/dist/data-structures/binary-tree/binary-tree.js +139 -62
  8. package/dist/data-structures/binary-tree/bst.d.ts +92 -5
  9. package/dist/data-structures/binary-tree/bst.js +89 -5
  10. package/dist/data-structures/binary-tree/segment-tree.d.ts +70 -2
  11. package/dist/data-structures/binary-tree/segment-tree.js +86 -2
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +34 -3
  13. package/dist/data-structures/binary-tree/tree-multiset.js +35 -4
  14. package/dist/data-structures/graph/abstract-graph.d.ts +45 -5
  15. package/dist/data-structures/graph/abstract-graph.js +59 -11
  16. package/dist/data-structures/graph/directed-graph.d.ts +26 -4
  17. package/dist/data-structures/graph/directed-graph.js +38 -39
  18. package/dist/data-structures/graph/undirected-graph.d.ts +23 -0
  19. package/dist/data-structures/graph/undirected-graph.js +41 -3
  20. package/dist/data-structures/hash/coordinate-map.d.ts +12 -3
  21. package/dist/data-structures/hash/coordinate-map.js +21 -2
  22. package/dist/data-structures/hash/coordinate-set.d.ts +12 -3
  23. package/dist/data-structures/hash/coordinate-set.js +21 -2
  24. package/dist/data-structures/heap/heap.d.ts +25 -6
  25. package/dist/data-structures/heap/heap.js +46 -8
  26. package/dist/data-structures/heap/max-heap.d.ts +5 -2
  27. package/dist/data-structures/heap/max-heap.js +5 -2
  28. package/dist/data-structures/heap/min-heap.d.ts +5 -2
  29. package/dist/data-structures/heap/min-heap.js +5 -2
  30. package/dist/data-structures/index.d.ts +1 -0
  31. package/dist/data-structures/index.js +1 -0
  32. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +32 -9
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +75 -8
  34. package/dist/data-structures/linked-list/singly-linked-list.d.ts +267 -330
  35. package/dist/data-structures/linked-list/singly-linked-list.js +263 -275
  36. package/dist/data-structures/matrix/matrix.d.ts +5 -2
  37. package/dist/data-structures/matrix/matrix.js +5 -2
  38. package/dist/data-structures/matrix/matrix2d.d.ts +5 -2
  39. package/dist/data-structures/matrix/matrix2d.js +5 -2
  40. package/dist/data-structures/matrix/navigator.d.ts +5 -2
  41. package/dist/data-structures/matrix/vector2d.d.ts +5 -2
  42. package/dist/data-structures/matrix/vector2d.js +5 -2
  43. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +5 -2
  44. package/dist/data-structures/priority-queue/max-priority-queue.js +5 -2
  45. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +5 -2
  46. package/dist/data-structures/priority-queue/min-priority-queue.js +5 -2
  47. package/dist/data-structures/priority-queue/priority-queue.d.ts +14 -5
  48. package/dist/data-structures/priority-queue/priority-queue.js +20 -4
  49. package/dist/data-structures/queue/deque.d.ts +30 -16
  50. package/dist/data-structures/queue/deque.js +62 -12
  51. package/dist/data-structures/queue/queue.d.ts +4 -4
  52. package/dist/data-structures/queue/queue.js +4 -4
  53. package/dist/data-structures/stack/stack.d.ts +1 -1
  54. package/dist/data-structures/stack/stack.js +1 -1
  55. package/dist/data-structures/trie/trie.d.ts +6 -3
  56. package/dist/data-structures/trie/trie.js +7 -4
  57. package/dist/data-structures/types/abstract-graph.d.ts +2 -2
  58. package/dist/utils/index.d.ts +1 -0
  59. package/dist/utils/index.js +1 -0
  60. package/dist/utils/types/utils.d.ts +8 -10
  61. package/dist/utils/types/utils.js +0 -1
  62. package/dist/utils/utils.d.ts +18 -8
  63. package/dist/utils/utils.js +93 -47
  64. package/package.json +2 -2
  65. package/src/data-structures/binary-tree/aa-tree.ts +1 -1
  66. package/src/data-structures/binary-tree/binary-tree.ts +84 -14
  67. package/src/data-structures/binary-tree/segment-tree.ts +45 -13
  68. package/src/data-structures/graph/abstract-graph.ts +58 -15
  69. package/src/data-structures/graph/directed-graph.ts +14 -5
  70. package/src/data-structures/graph/undirected-graph.ts +23 -6
  71. package/src/data-structures/hash/coordinate-map.ts +13 -1
  72. package/src/data-structures/hash/coordinate-set.ts +13 -1
  73. package/src/data-structures/heap/heap.ts +31 -0
  74. package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
  75. package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
  76. package/src/data-structures/priority-queue/priority-queue.ts +15 -2
  77. package/src/data-structures/queue/deque.ts +38 -8
  78. package/src/data-structures/types/abstract-graph.ts +3 -3
  79. package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
  80. package/dist/utils/trampoline.d.ts +0 -14
  81. package/dist/utils/trampoline.js +0 -130
@@ -10,19 +10,25 @@ import {PriorityQueue} from '../priority-queue';
10
10
  import type {DijkstraResult, IGraph, VertexId} from '../types';
11
11
 
12
12
  export class AbstractVertex {
13
- constructor(id: VertexId) {
14
- this._id = id;
15
- }
16
-
17
13
  protected _id: VertexId;
14
+ get id(): VertexId {
15
+ return this._id;
16
+ }
18
17
 
19
- public get id(): VertexId {
18
+ /**
19
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
20
+ */
21
+ getId(): VertexId {
20
22
  return this._id;
21
23
  }
22
24
 
23
- public set id(v: VertexId) {
25
+ set id(v: VertexId) {
24
26
  this._id = v;
25
27
  }
28
+
29
+ constructor(id: VertexId) {
30
+ this._id = id;
31
+ }
26
32
  }
27
33
 
28
34
  export abstract class AbstractEdge {
@@ -41,21 +47,33 @@ export abstract class AbstractEdge {
41
47
  }
42
48
 
43
49
  private _weight: number;
44
-
45
50
  get weight(): number {
46
51
  return this._weight;
47
52
  }
48
53
 
54
+ /**
55
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
56
+ */
57
+ getWeight(): number {
58
+ return this._weight;
59
+ }
60
+
49
61
  set weight(v: number) {
50
62
  this._weight = v;
51
63
  }
52
64
 
53
65
  private _hashCode: string;
54
-
55
66
  get hashCode(): string {
56
67
  return this._hashCode;
57
68
  }
58
69
 
70
+ /**
71
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
72
+ */
73
+ getHashCode(): string {
74
+ return this._hashCode;
75
+ }
76
+
59
77
  set hashCode(v: string) {
60
78
  this._hashCode = v;
61
79
  }
@@ -97,9 +115,9 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
97
115
  * The function checks if a vertex exists in a graph.
98
116
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
99
117
  * (`VertexId`).
100
- * @returns The method `containsVertex` returns a boolean value.
118
+ * @returns The method `hasVertex` returns a boolean value.
101
119
  */
102
- containsVertex(vertexOrId: V | VertexId): boolean {
120
+ hasVertex(vertexOrId: V | VertexId): boolean {
103
121
  return this._vertices.has(this.getVertexId(vertexOrId));
104
122
  }
105
123
 
@@ -120,7 +138,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
120
138
  * false. Otherwise, it will add the newVertex to the graph and return true.
121
139
  */
122
140
  addVertex(newVertex: V): boolean {
123
- if (this.containsVertex(newVertex)) {
141
+ if (this.hasVertex(newVertex)) {
124
142
  return false;
125
143
  }
126
144
  this._vertices.set(newVertex.id, newVertex);
@@ -165,10 +183,10 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
165
183
  * a vertex in a graph, while V represents the type of the vertex itself.
166
184
  * @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
167
185
  * or a `V` type.
168
- * @returns The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
186
+ * @returns The function `hasEdge` returns a boolean value. It returns `true` if there is an edge between the
169
187
  * vertices `v1` and `v2`, and `false` otherwise.
170
188
  */
171
- containsEdge(v1: VertexId | V, v2: VertexId | V): boolean {
189
+ hasEdge(v1: VertexId | V, v2: VertexId | V): boolean {
172
190
  const edge = this.getEdge(v1, v2);
173
191
  return !!edge;
174
192
  }
@@ -499,8 +517,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
499
517
  return {distMap, preMap, seen, paths, minDist, minPath};
500
518
  }
501
519
 
520
+ /**
521
+ * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
522
+ * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
523
+ * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
524
+ */
525
+
526
+ /**
527
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
528
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
529
+ */
530
+
502
531
  /**
503
532
  * Dijkstra algorithm time: O(logVE) space: O(V + E)
533
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
504
534
  * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
505
535
  * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
506
536
  * @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
@@ -625,10 +655,17 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
625
655
 
626
656
  abstract getEndsOfEdge(edge: E): [V, V] | null;
627
657
 
658
+ /**
659
+ * BellmanFord time:O(VE) space:O(V)
660
+ * one to rest pairs
661
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
662
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
663
+ */
628
664
 
629
665
  /**
630
666
  * BellmanFord time:O(VE) space:O(V)
631
667
  * one to rest pairs
668
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
632
669
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
633
670
  * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
634
671
  * @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
@@ -732,6 +769,13 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
732
769
  /**
733
770
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
734
771
  * all pairs
772
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
773
+ */
774
+
775
+ /**
776
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
777
+ * all pairs
778
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
735
779
  * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
736
780
  * graph.
737
781
  * @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
@@ -903,8 +947,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
903
947
  }
904
948
 
905
949
 
906
- // unionFind() {
907
- // }
950
+ // unionFind() {}
908
951
 
909
952
  /**--- end find cycles --- */
910
953
 
@@ -40,17 +40,26 @@ export class DirectedEdge extends AbstractEdge {
40
40
  get src(): VertexId {
41
41
  return this._src;
42
42
  }
43
-
43
+ /**
44
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
45
+ */
46
+ getSrc(): VertexId {
47
+ return this._src;
48
+ }
44
49
  set src(v: VertexId) {
45
50
  this._src = v;
46
51
  }
47
52
 
48
-
49
53
  private _dest: VertexId;
50
54
  get dest(): VertexId {
51
55
  return this._dest;
52
56
  }
53
-
57
+ /**
58
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
59
+ */
60
+ getDest(): VertexId {
61
+ return this._dest;
62
+ }
54
63
  set dest(v: VertexId) {
55
64
  this._dest = v;
56
65
  }
@@ -101,7 +110,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
101
110
  * graph, and `false` if either the source or destination vertices of the edge are not present in the graph.
102
111
  */
103
112
  addEdge(edge: E): boolean {
104
- if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
113
+ if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
105
114
  return false;
106
115
  }
107
116
 
@@ -401,7 +410,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
401
410
  * returns null.
402
411
  */
403
412
  getEndsOfEdge(edge: E): [V, V] | null {
404
- if (!this.containsEdge(edge.src, edge.dest)) {
413
+ if (!this.hasEdge(edge.src, edge.dest)) {
405
414
  return null;
406
415
  }
407
416
  const v1 = this.getVertex(edge.src);
@@ -35,21 +35,38 @@ export class UndirectedEdge extends AbstractEdge {
35
35
  }
36
36
 
37
37
  private _vertices: [VertexId, VertexId];
38
-
39
- public get vertices() {
38
+ get vertices() {
40
39
  return this._vertices;
41
40
  }
42
-
43
- public set vertices(v: [VertexId, VertexId]) {
41
+ /**
42
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
43
+ */
44
+ getVertices() {
45
+ return this._vertices;
46
+ }
47
+ set vertices(v: [VertexId, VertexId]) {
44
48
  this._vertices = v;
45
49
  }
46
50
  }
47
51
 
48
52
  export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
49
- protected _edges: Map<V, E[]> = new Map();
53
+ protected _edges: Map<V, E[]>;
54
+ get edges(): Map<V, E[]> {
55
+ return this._edges;
56
+ }
57
+ /**
58
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
59
+ */
60
+ getEdges(): Map<V, E[]> {
61
+ return this._edges;
62
+ }
63
+ protected set edges(v: Map<V, E[]>) {
64
+ this._edges = v;
65
+ }
50
66
 
51
67
  constructor() {
52
68
  super();
69
+ this._edges = new Map<V, E[]>();
53
70
  }
54
71
 
55
72
  /**
@@ -223,7 +240,7 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
223
240
  * `null`.
224
241
  */
225
242
  getEndsOfEdge(edge: E): [V, V] | null {
226
- if (!this.containsEdge(edge.vertices[0], edge.vertices[1])) {
243
+ if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
227
244
  return null;
228
245
  }
229
246
  const v1 = this.getVertex(edge.vertices[0]);
@@ -6,7 +6,19 @@
6
6
  * @license MIT License
7
7
  */
8
8
  export class CoordinateMap<V> extends Map<any, V> {
9
- private readonly _joint: string = '_';
9
+ protected _joint: string = '_';
10
+ get joint(): string {
11
+ return this._joint;
12
+ }
13
+ /**
14
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
15
+ */
16
+ getJoint(): string {
17
+ return this._joint;
18
+ }
19
+ protected set joint(v: string) {
20
+ this._joint = v;
21
+ }
10
22
 
11
23
  constructor(joint?: string) {
12
24
  super();
@@ -6,7 +6,19 @@
6
6
  * @license MIT License
7
7
  */
8
8
  export class CoordinateSet extends Set {
9
- private readonly _joint: string = '_';
9
+ protected _joint: string = '_';
10
+ get joint(): string {
11
+ return this._joint;
12
+ }
13
+ /**
14
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
15
+ */
16
+ getJoint(): string {
17
+ return this._joint;
18
+ }
19
+ protected set joint(v: string) {
20
+ this._joint = v;
21
+ }
10
22
 
11
23
  constructor(joint?: string) {
12
24
  super();
@@ -10,7 +10,32 @@ import type {HeapItem, HeapOptions} from '../types';
10
10
 
11
11
  export abstract class Heap<T> {
12
12
  protected abstract _pq: PriorityQueue<HeapItem<T>>;
13
+ get pq() {
14
+ return this._pq;
15
+ }
16
+ /**
17
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
18
+ */
19
+ getPq() {
20
+ return this._pq;
21
+ }
22
+ protected set pq(v: PriorityQueue<HeapItem<T>>) {
23
+ this._pq = v;
24
+ }
25
+
13
26
  protected _priorityCb: (element: T) => number;
27
+ get priorityCb() {
28
+ return this._priorityCb;
29
+ }
30
+ /**
31
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
32
+ */
33
+ getPriorityCb() {
34
+ return this._priorityCb;
35
+ }
36
+ protected set priorityCb(v: (element: T) => number) {
37
+ this._priorityCb = v;
38
+ }
14
39
 
15
40
  /**
16
41
  * The function is a constructor for a class that initializes a priority callback function based on the
@@ -36,6 +61,12 @@ export abstract class Heap<T> {
36
61
  get size(): number {
37
62
  return this._pq.size;
38
63
  }
64
+ /**
65
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
66
+ */
67
+ getSize(): number {
68
+ return this._pq.size;
69
+ }
39
70
 
40
71
  /**
41
72
  * The function checks if a priority queue is empty.
@@ -8,29 +8,86 @@
8
8
  import type {DoublyLinkedListGetBy} from '../types';
9
9
 
10
10
  export class DoublyLinkedListNode<T> {
11
- val: T;
12
- next: DoublyLinkedListNode<T> | null;
13
- prev: DoublyLinkedListNode<T> | null;
11
+ protected _val: T;
12
+ get val(): T {
13
+ return this._val;
14
+ }
15
+ set val(v: T) {
16
+ this._val = v;
17
+ }
18
+
19
+ protected _next: DoublyLinkedListNode<T> | null;
20
+ get next(): DoublyLinkedListNode<T> | null {
21
+ return this._next;
22
+ }
23
+ set next(v: DoublyLinkedListNode<T> | null) {
24
+ this._next = v;
25
+ }
26
+
27
+ protected _prev: DoublyLinkedListNode<T> | null;
28
+ get prev(): DoublyLinkedListNode<T> | null {
29
+ return this._prev;
30
+ }
31
+ set prev(v: DoublyLinkedListNode<T> | null) {
32
+ this._prev = v;
33
+ }
14
34
 
15
35
  constructor(nodeValue: T) {
16
- this.val = nodeValue;
17
- this.next = null;
18
- this.prev = null;
36
+ this._val = nodeValue;
37
+ this._next = null;
38
+ this._prev = null;
19
39
  }
20
40
  }
21
41
 
22
42
  export class DoublyLinkedList<T> {
23
- private _first: DoublyLinkedListNode<T> | null = null;
24
- private _last: DoublyLinkedListNode<T> | null = null;
25
- private _size = 0;
43
+ protected _first: DoublyLinkedListNode<T> | null;
44
+ get first(): DoublyLinkedListNode<T> | null {
45
+ return this._first;
46
+ }
47
+ /**
48
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
49
+ */
50
+ getFirst(): DoublyLinkedListNode<T> | null {
51
+ return this._first;
52
+ }
53
+ protected set first(v: DoublyLinkedListNode<T> | null) {
54
+ this._first = v;
55
+ }
56
+
57
+ protected _last: DoublyLinkedListNode<T> | null;
58
+ get last(): DoublyLinkedListNode<T> | null {
59
+ return this._last;
60
+ }
61
+ /**
62
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
63
+ */
64
+ getLast(): DoublyLinkedListNode<T> | null {
65
+ return this._last;
66
+ }
67
+ protected set last(v: DoublyLinkedListNode<T> | null) {
68
+ this._last = v;
69
+ }
70
+
71
+ protected _size: number;
26
72
  get size(): number {
27
73
  return this._size;
28
74
  }
29
-
30
- set size(v: number) {
75
+ /**
76
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
77
+ */
78
+ getSize(): number {
79
+ return this._size;
80
+ }
81
+ protected set size(v: number) {
31
82
  this._size = v;
32
83
  }
33
84
 
85
+ constructor() {
86
+ this._first = null;
87
+ this._last = null;
88
+ this._size = 0;
89
+ }
90
+
34
91
  /**
35
92
  * The function adds a new node with a given value to the beginning of a doubly linked list.
36
93
  * @param {T} val - The `val` parameter represents the value of the element that you want to add to the beginning of