data-structure-typed 1.53.1 → 1.53.3

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 (235) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +6 -0
  3. package/dist/cjs/data-structures/heap/heap.d.ts +165 -0
  4. package/dist/cjs/data-structures/heap/heap.js +165 -0
  5. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  6. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +426 -0
  7. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +430 -2
  8. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  9. package/dist/mjs/constants/index.js +1 -0
  10. package/dist/mjs/constants/index.js.map +1 -0
  11. package/dist/mjs/data-structures/base/index.js +1 -0
  12. package/dist/mjs/data-structures/base/index.js.map +1 -0
  13. package/dist/mjs/data-structures/base/iterable-element-base.js +1 -0
  14. package/dist/mjs/data-structures/base/iterable-element-base.js.map +1 -0
  15. package/dist/mjs/data-structures/base/iterable-entry-base.js +1 -0
  16. package/dist/mjs/data-structures/base/iterable-entry-base.js.map +1 -0
  17. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  18. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  19. package/dist/mjs/data-structures/binary-tree/avl-tree.js +1 -0
  20. package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -0
  21. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +1 -0
  22. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
  23. package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -0
  24. package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -0
  25. package/dist/mjs/data-structures/binary-tree/bst.js +1 -0
  26. package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -0
  27. package/dist/mjs/data-structures/binary-tree/index.js +1 -0
  28. package/dist/mjs/data-structures/binary-tree/index.js.map +1 -0
  29. package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -0
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.js.map +1 -0
  31. package/dist/mjs/data-structures/binary-tree/segment-tree.js +1 -0
  32. package/dist/mjs/data-structures/binary-tree/segment-tree.js.map +1 -0
  33. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  34. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  35. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -0
  36. package/dist/mjs/data-structures/graph/abstract-graph.js.map +1 -0
  37. package/dist/mjs/data-structures/graph/directed-graph.js +1 -0
  38. package/dist/mjs/data-structures/graph/directed-graph.js.map +1 -0
  39. package/dist/mjs/data-structures/graph/index.js +1 -0
  40. package/dist/mjs/data-structures/graph/index.js.map +1 -0
  41. package/dist/mjs/data-structures/graph/map-graph.js +1 -0
  42. package/dist/mjs/data-structures/graph/map-graph.js.map +1 -0
  43. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -0
  44. package/dist/mjs/data-structures/graph/undirected-graph.js.map +1 -0
  45. package/dist/mjs/data-structures/hash/hash-map.js +1 -0
  46. package/dist/mjs/data-structures/hash/hash-map.js.map +1 -0
  47. package/dist/mjs/data-structures/hash/index.js +1 -0
  48. package/dist/mjs/data-structures/hash/index.js.map +1 -0
  49. package/dist/mjs/data-structures/heap/heap.d.ts +165 -0
  50. package/dist/mjs/data-structures/heap/heap.js +166 -0
  51. package/dist/mjs/data-structures/heap/heap.js.map +1 -0
  52. package/dist/mjs/data-structures/heap/index.js +1 -0
  53. package/dist/mjs/data-structures/heap/index.js.map +1 -0
  54. package/dist/mjs/data-structures/heap/max-heap.js +1 -0
  55. package/dist/mjs/data-structures/heap/max-heap.js.map +1 -0
  56. package/dist/mjs/data-structures/heap/min-heap.js +1 -0
  57. package/dist/mjs/data-structures/heap/min-heap.js.map +1 -0
  58. package/dist/mjs/data-structures/index.js +1 -0
  59. package/dist/mjs/data-structures/index.js.map +1 -0
  60. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +426 -0
  61. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +431 -2
  62. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -0
  63. package/dist/mjs/data-structures/linked-list/index.js +1 -0
  64. package/dist/mjs/data-structures/linked-list/index.js.map +1 -0
  65. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -0
  66. package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -0
  67. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +1 -0
  68. package/dist/mjs/data-structures/linked-list/skip-linked-list.js.map +1 -0
  69. package/dist/mjs/data-structures/matrix/index.js +1 -0
  70. package/dist/mjs/data-structures/matrix/index.js.map +1 -0
  71. package/dist/mjs/data-structures/matrix/matrix.js +1 -0
  72. package/dist/mjs/data-structures/matrix/matrix.js.map +1 -0
  73. package/dist/mjs/data-structures/matrix/navigator.js +1 -0
  74. package/dist/mjs/data-structures/matrix/navigator.js.map +1 -0
  75. package/dist/mjs/data-structures/priority-queue/index.js +1 -0
  76. package/dist/mjs/data-structures/priority-queue/index.js.map +1 -0
  77. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +1 -0
  78. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js.map +1 -0
  79. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +1 -0
  80. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js.map +1 -0
  81. package/dist/mjs/data-structures/priority-queue/priority-queue.js +1 -0
  82. package/dist/mjs/data-structures/priority-queue/priority-queue.js.map +1 -0
  83. package/dist/mjs/data-structures/queue/deque.js +1 -0
  84. package/dist/mjs/data-structures/queue/deque.js.map +1 -0
  85. package/dist/mjs/data-structures/queue/index.js +1 -0
  86. package/dist/mjs/data-structures/queue/index.js.map +1 -0
  87. package/dist/mjs/data-structures/queue/queue.js +1 -0
  88. package/dist/mjs/data-structures/queue/queue.js.map +1 -0
  89. package/dist/mjs/data-structures/stack/index.js +1 -0
  90. package/dist/mjs/data-structures/stack/index.js.map +1 -0
  91. package/dist/mjs/data-structures/stack/stack.js +1 -0
  92. package/dist/mjs/data-structures/stack/stack.js.map +1 -0
  93. package/dist/mjs/data-structures/tree/index.js +1 -0
  94. package/dist/mjs/data-structures/tree/index.js.map +1 -0
  95. package/dist/mjs/data-structures/tree/tree.js +1 -0
  96. package/dist/mjs/data-structures/tree/tree.js.map +1 -0
  97. package/dist/mjs/data-structures/trie/index.js +1 -0
  98. package/dist/mjs/data-structures/trie/index.js.map +1 -0
  99. package/dist/mjs/data-structures/trie/trie.js +1 -0
  100. package/dist/mjs/data-structures/trie/trie.js.map +1 -0
  101. package/dist/mjs/index.js +1 -0
  102. package/dist/mjs/index.js.map +1 -0
  103. package/dist/mjs/interfaces/binary-tree.js +1 -0
  104. package/dist/mjs/interfaces/binary-tree.js.map +1 -0
  105. package/dist/mjs/interfaces/doubly-linked-list.js +1 -0
  106. package/dist/mjs/interfaces/doubly-linked-list.js.map +1 -0
  107. package/dist/mjs/interfaces/graph.js +1 -0
  108. package/dist/mjs/interfaces/graph.js.map +1 -0
  109. package/dist/mjs/interfaces/heap.js +1 -0
  110. package/dist/mjs/interfaces/heap.js.map +1 -0
  111. package/dist/mjs/interfaces/index.js +1 -0
  112. package/dist/mjs/interfaces/index.js.map +1 -0
  113. package/dist/mjs/interfaces/navigator.js +1 -0
  114. package/dist/mjs/interfaces/navigator.js.map +1 -0
  115. package/dist/mjs/interfaces/priority-queue.js +1 -0
  116. package/dist/mjs/interfaces/priority-queue.js.map +1 -0
  117. package/dist/mjs/interfaces/segment-tree.js +1 -0
  118. package/dist/mjs/interfaces/segment-tree.js.map +1 -0
  119. package/dist/mjs/interfaces/singly-linked-list.js +1 -0
  120. package/dist/mjs/interfaces/singly-linked-list.js.map +1 -0
  121. package/dist/mjs/types/common.js +1 -0
  122. package/dist/mjs/types/common.js.map +1 -0
  123. package/dist/mjs/types/data-structures/base/base.js +1 -0
  124. package/dist/mjs/types/data-structures/base/base.js.map +1 -0
  125. package/dist/mjs/types/data-structures/base/index.js +1 -0
  126. package/dist/mjs/types/data-structures/base/index.js.map +1 -0
  127. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  128. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  129. package/dist/mjs/types/data-structures/binary-tree/avl-tree.js +1 -0
  130. package/dist/mjs/types/data-structures/binary-tree/avl-tree.js.map +1 -0
  131. package/dist/mjs/types/data-structures/binary-tree/binary-indexed-tree.js +1 -0
  132. package/dist/mjs/types/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
  133. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -0
  134. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js.map +1 -0
  135. package/dist/mjs/types/data-structures/binary-tree/bst.js +1 -0
  136. package/dist/mjs/types/data-structures/binary-tree/bst.js.map +1 -0
  137. package/dist/mjs/types/data-structures/binary-tree/index.js +1 -0
  138. package/dist/mjs/types/data-structures/binary-tree/index.js.map +1 -0
  139. package/dist/mjs/types/data-structures/binary-tree/rb-tree.js +1 -0
  140. package/dist/mjs/types/data-structures/binary-tree/rb-tree.js.map +1 -0
  141. package/dist/mjs/types/data-structures/binary-tree/segment-tree.js +1 -0
  142. package/dist/mjs/types/data-structures/binary-tree/segment-tree.js.map +1 -0
  143. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js +1 -0
  144. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  145. package/dist/mjs/types/data-structures/graph/abstract-graph.js +1 -0
  146. package/dist/mjs/types/data-structures/graph/abstract-graph.js.map +1 -0
  147. package/dist/mjs/types/data-structures/graph/directed-graph.js +1 -0
  148. package/dist/mjs/types/data-structures/graph/directed-graph.js.map +1 -0
  149. package/dist/mjs/types/data-structures/graph/index.js +1 -0
  150. package/dist/mjs/types/data-structures/graph/index.js.map +1 -0
  151. package/dist/mjs/types/data-structures/graph/map-graph.js +1 -0
  152. package/dist/mjs/types/data-structures/graph/map-graph.js.map +1 -0
  153. package/dist/mjs/types/data-structures/graph/undirected-graph.js +1 -0
  154. package/dist/mjs/types/data-structures/graph/undirected-graph.js.map +1 -0
  155. package/dist/mjs/types/data-structures/hash/hash-map.js +1 -0
  156. package/dist/mjs/types/data-structures/hash/hash-map.js.map +1 -0
  157. package/dist/mjs/types/data-structures/hash/index.js +1 -0
  158. package/dist/mjs/types/data-structures/hash/index.js.map +1 -0
  159. package/dist/mjs/types/data-structures/heap/heap.js +1 -0
  160. package/dist/mjs/types/data-structures/heap/heap.js.map +1 -0
  161. package/dist/mjs/types/data-structures/heap/index.js +1 -0
  162. package/dist/mjs/types/data-structures/heap/index.js.map +1 -0
  163. package/dist/mjs/types/data-structures/heap/max-heap.js +1 -0
  164. package/dist/mjs/types/data-structures/heap/max-heap.js.map +1 -0
  165. package/dist/mjs/types/data-structures/heap/min-heap.js +1 -0
  166. package/dist/mjs/types/data-structures/heap/min-heap.js.map +1 -0
  167. package/dist/mjs/types/data-structures/index.js +1 -0
  168. package/dist/mjs/types/data-structures/index.js.map +1 -0
  169. package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.js +1 -0
  170. package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.js.map +1 -0
  171. package/dist/mjs/types/data-structures/linked-list/index.js +1 -0
  172. package/dist/mjs/types/data-structures/linked-list/index.js.map +1 -0
  173. package/dist/mjs/types/data-structures/linked-list/singly-linked-list.js +1 -0
  174. package/dist/mjs/types/data-structures/linked-list/singly-linked-list.js.map +1 -0
  175. package/dist/mjs/types/data-structures/linked-list/skip-linked-list.js +1 -0
  176. package/dist/mjs/types/data-structures/linked-list/skip-linked-list.js.map +1 -0
  177. package/dist/mjs/types/data-structures/matrix/index.js +1 -0
  178. package/dist/mjs/types/data-structures/matrix/index.js.map +1 -0
  179. package/dist/mjs/types/data-structures/matrix/matrix.js +1 -0
  180. package/dist/mjs/types/data-structures/matrix/matrix.js.map +1 -0
  181. package/dist/mjs/types/data-structures/matrix/navigator.js +1 -0
  182. package/dist/mjs/types/data-structures/matrix/navigator.js.map +1 -0
  183. package/dist/mjs/types/data-structures/priority-queue/index.js +1 -0
  184. package/dist/mjs/types/data-structures/priority-queue/index.js.map +1 -0
  185. package/dist/mjs/types/data-structures/priority-queue/max-priority-queue.js +1 -0
  186. package/dist/mjs/types/data-structures/priority-queue/max-priority-queue.js.map +1 -0
  187. package/dist/mjs/types/data-structures/priority-queue/min-priority-queue.js +1 -0
  188. package/dist/mjs/types/data-structures/priority-queue/min-priority-queue.js.map +1 -0
  189. package/dist/mjs/types/data-structures/priority-queue/priority-queue.js +1 -0
  190. package/dist/mjs/types/data-structures/priority-queue/priority-queue.js.map +1 -0
  191. package/dist/mjs/types/data-structures/queue/deque.js +1 -0
  192. package/dist/mjs/types/data-structures/queue/deque.js.map +1 -0
  193. package/dist/mjs/types/data-structures/queue/index.js +1 -0
  194. package/dist/mjs/types/data-structures/queue/index.js.map +1 -0
  195. package/dist/mjs/types/data-structures/queue/queue.js +1 -0
  196. package/dist/mjs/types/data-structures/queue/queue.js.map +1 -0
  197. package/dist/mjs/types/data-structures/stack/index.js +1 -0
  198. package/dist/mjs/types/data-structures/stack/index.js.map +1 -0
  199. package/dist/mjs/types/data-structures/stack/stack.js +1 -0
  200. package/dist/mjs/types/data-structures/stack/stack.js.map +1 -0
  201. package/dist/mjs/types/data-structures/tree/index.js +1 -0
  202. package/dist/mjs/types/data-structures/tree/index.js.map +1 -0
  203. package/dist/mjs/types/data-structures/tree/tree.js +1 -0
  204. package/dist/mjs/types/data-structures/tree/tree.js.map +1 -0
  205. package/dist/mjs/types/data-structures/trie/index.js +1 -0
  206. package/dist/mjs/types/data-structures/trie/index.js.map +1 -0
  207. package/dist/mjs/types/data-structures/trie/trie.js +1 -0
  208. package/dist/mjs/types/data-structures/trie/trie.js.map +1 -0
  209. package/dist/mjs/types/index.js +1 -0
  210. package/dist/mjs/types/index.js.map +1 -0
  211. package/dist/mjs/types/utils/index.js +1 -0
  212. package/dist/mjs/types/utils/index.js.map +1 -0
  213. package/dist/mjs/types/utils/utils.js +1 -0
  214. package/dist/mjs/types/utils/utils.js.map +1 -0
  215. package/dist/mjs/types/utils/validate-type.js +1 -0
  216. package/dist/mjs/types/utils/validate-type.js.map +1 -0
  217. package/dist/mjs/utils/index.js +1 -0
  218. package/dist/mjs/utils/index.js.map +1 -0
  219. package/dist/mjs/utils/number.js +1 -0
  220. package/dist/mjs/utils/number.js.map +1 -0
  221. package/dist/mjs/utils/utils.js +1 -0
  222. package/dist/mjs/utils/utils.js.map +1 -0
  223. package/dist/umd/data-structure-typed.js +2 -2
  224. package/dist/umd/data-structure-typed.min.js +1 -1
  225. package/dist/umd/data-structure-typed.min.js.map +1 -1
  226. package/package.json +13 -11
  227. package/src/data-structures/heap/heap.ts +165 -0
  228. package/src/data-structures/linked-list/doubly-linked-list.ts +428 -2
  229. package/test/unit/data-structures/graph/directed-graph.test.ts +14 -0
  230. package/test/unit/data-structures/heap/heap.test.ts +173 -0
  231. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +434 -0
  232. package/test/utils/array.ts +1 -1
  233. package/test/utils/string.ts +49 -1
  234. package/testToExample.ts +215 -0
  235. package/tsconfig-mjs.json +1 -1
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.53.1",
3
+ "version": "1.53.3",
4
4
  "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
7
+ "browser": "dist/umd/data-structure-typed.min.js",
7
8
  "types": "dist/mjs/index.d.ts",
8
9
  "umd:main": "dist/umd/data-structure-typed.min.js",
9
10
  "exports": {
@@ -17,10 +18,11 @@
17
18
  "build:mjs": "rm -rf dist/mjs && tsc -p tsconfig-mjs.json",
18
19
  "build:cjs": "rm -rf dist/cjs && tsc -p tsconfig-cjs.json",
19
20
  "build:umd": "tsup",
20
- "build:docs": "typedoc --out docs ./src",
21
- "build:docs-class": "typedoc --out docs ./src/data-structures",
22
- "test:unit": "jest --runInBand",
23
- "test": "npm run test:unit",
21
+ "build:docs": "npm run gen:examples && typedoc --out docs ./src",
22
+ "build:docs-class": "npm run gen:examples && typedoc --out docs ./src/data-structures",
23
+ "gen:examples": "ts-node testToExample.ts",
24
+ "test:in-band": "jest --runInBand",
25
+ "test": "npm run test:in-band",
24
26
  "test:integration": "npm run update:subs && jest --config jest.integration.config.js",
25
27
  "test:perf": "npm run build:cjs && npm run build:mjs && ts-node test/performance/reportor.ts",
26
28
  "check": "tsc --noEmit",
@@ -33,7 +35,7 @@
33
35
  "format:test": "prettier --write 'test/**/*.{js,ts}'",
34
36
  "format": "npm run format:src && npm run format:test",
35
37
  "check:exist-latest": "sh scripts/check_exist_remotely.sh",
36
- "ci": "env && git fetch --tags && npm run update:subs && npm run inspect && npm run lint && npm run build && npm run test:unit && npm run changelog",
38
+ "ci": "env && git fetch --tags && npm run update:subs && npm run inspect && npm run lint && npm run build && npm run test && npm run changelog",
37
39
  "update:subs": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed data-structure-typed --save-dev",
38
40
  "install:all-subs": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multimap-typed trie-typed undirected-graph-typed queue-typed --save-dev",
39
41
  "changelog": "auto-changelog",
@@ -68,11 +70,11 @@
68
70
  "@typescript-eslint/eslint-plugin": "^8.12.1",
69
71
  "@typescript-eslint/parser": "^8.12.1",
70
72
  "auto-changelog": "^2.5.0",
71
- "avl-tree-typed": "^1.53.0",
73
+ "avl-tree-typed": "^1.53.2",
72
74
  "benchmark": "^2.1.4",
73
- "binary-tree-typed": "^1.53.0",
74
- "bst-typed": "^1.53.0",
75
- "data-structure-typed": "^1.53.0",
75
+ "binary-tree-typed": "^1.53.2",
76
+ "bst-typed": "^1.53.2",
77
+ "data-structure-typed": "^1.53.2",
76
78
  "dependency-cruiser": "^16.5.0",
77
79
  "doctoc": "^2.2.1",
78
80
  "eslint": "^9.13.0",
@@ -81,7 +83,7 @@
81
83
  "eslint-import-resolver-typescript": "^3.6.3",
82
84
  "eslint-plugin-import": "^2.31.0",
83
85
  "fast-glob": "^3.3.2",
84
- "heap-typed": "^1.53.0",
86
+ "heap-typed": "^1.53.2",
85
87
  "istanbul-badges-readme": "^1.9.0",
86
88
  "jest": "^29.7.0",
87
89
  "js-sdsl": "^4.4.2",
@@ -19,6 +19,171 @@ import { IterableElementBase } from '../base';
19
19
  * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
20
20
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
21
21
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
22
+ * @example
23
+ * // Use Heap to sort an array
24
+ * function heapSort(arr: number[]): number[] {
25
+ * const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
26
+ * const sorted: number[] = [];
27
+ * while (!heap.isEmpty()) {
28
+ * sorted.push(heap.poll()!); // Poll minimum element
29
+ * }
30
+ * return sorted;
31
+ * }
32
+ *
33
+ * const array = [5, 3, 8, 4, 1, 2];
34
+ * console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
35
+ * @example
36
+ * // Use Heap to solve top k problems
37
+ * function topKElements(arr: number[], k: number): number[] {
38
+ * const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
39
+ * arr.forEach(num => {
40
+ * heap.add(num);
41
+ * if (heap.size > k) heap.poll(); // Keep the heap size at K
42
+ * });
43
+ * return heap.toArray();
44
+ * }
45
+ *
46
+ * const numbers = [10, 30, 20, 5, 15, 25];
47
+ * console.log(topKElements(numbers, 3)); // [15, 10, 5]
48
+ * @example
49
+ * // Use Heap to merge sorted sequences
50
+ * function mergeSortedSequences(sequences: number[][]): number[] {
51
+ * const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
52
+ * comparator: (a, b) => a.value - b.value // Min heap
53
+ * });
54
+ *
55
+ * // Initialize heap
56
+ * sequences.forEach((seq, seqIndex) => {
57
+ * if (seq.length) {
58
+ * heap.add({ value: seq[0], seqIndex, itemIndex: 0 });
59
+ * }
60
+ * });
61
+ *
62
+ * const merged: number[] = [];
63
+ * while (!heap.isEmpty()) {
64
+ * const { value, seqIndex, itemIndex } = heap.poll()!;
65
+ * merged.push(value);
66
+ *
67
+ * if (itemIndex + 1 < sequences[seqIndex].length) {
68
+ * heap.add({
69
+ * value: sequences[seqIndex][itemIndex + 1],
70
+ * seqIndex,
71
+ * itemIndex: itemIndex + 1
72
+ * });
73
+ * }
74
+ * }
75
+ *
76
+ * return merged;
77
+ * }
78
+ *
79
+ * const sequences = [
80
+ * [1, 4, 7],
81
+ * [2, 5, 8],
82
+ * [3, 6, 9]
83
+ * ];
84
+ * console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
85
+ * @example
86
+ * // Use Heap to dynamically maintain the median
87
+ * class MedianFinder {
88
+ * private low: MaxHeap<number>; // Max heap, stores the smaller half
89
+ * private high: MinHeap<number>; // Min heap, stores the larger half
90
+ *
91
+ * constructor() {
92
+ * this.low = new MaxHeap<number>([]);
93
+ * this.high = new MinHeap<number>([]);
94
+ * }
95
+ *
96
+ * addNum(num: number): void {
97
+ * if (this.low.isEmpty() || num <= this.low.peek()!) this.low.add(num);
98
+ * else this.high.add(num);
99
+ *
100
+ * // Balance heaps
101
+ * if (this.low.size > this.high.size + 1) this.high.add(this.low.poll()!);
102
+ * else if (this.high.size > this.low.size) this.low.add(this.high.poll()!);
103
+ * }
104
+ *
105
+ * findMedian(): number {
106
+ * if (this.low.size === this.high.size) return (this.low.peek()! + this.high.peek()!) / 2;
107
+ * return this.low.peek()!;
108
+ * }
109
+ * }
110
+ *
111
+ * const medianFinder = new MedianFinder();
112
+ * medianFinder.addNum(10);
113
+ * console.log(medianFinder.findMedian()); // 10
114
+ * medianFinder.addNum(20);
115
+ * console.log(medianFinder.findMedian()); // 15
116
+ * medianFinder.addNum(30);
117
+ * console.log(medianFinder.findMedian()); // 20
118
+ * medianFinder.addNum(40);
119
+ * console.log(medianFinder.findMedian()); // 25
120
+ * medianFinder.addNum(50);
121
+ * console.log(medianFinder.findMedian()); // 30
122
+ * @example
123
+ * // Use Heap for load balancing
124
+ * function loadBalance(requests: number[], servers: number): number[] {
125
+ * const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
126
+ * const serverLoads = new Array(servers).fill(0);
127
+ *
128
+ * for (let i = 0; i < servers; i++) {
129
+ * serverHeap.add({ id: i, load: 0 });
130
+ * }
131
+ *
132
+ * requests.forEach(req => {
133
+ * const server = serverHeap.poll()!;
134
+ * serverLoads[server.id] += req;
135
+ * server.load += req;
136
+ * serverHeap.add(server); // The server after updating the load is re-entered into the heap
137
+ * });
138
+ *
139
+ * return serverLoads;
140
+ * }
141
+ *
142
+ * const requests = [5, 2, 8, 3, 7];
143
+ * console.log(loadBalance(requests, 3)); // [12, 8, 5]
144
+ * @example
145
+ * // Use Heap to schedule tasks
146
+ * type Task = [string, number];
147
+ *
148
+ * function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
149
+ * const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
150
+ * const allocation = new Map<number, Task[]>();
151
+ *
152
+ * // Initialize the load on each machine
153
+ * for (let i = 0; i < machines; i++) {
154
+ * machineHeap.add({ id: i, load: 0 });
155
+ * allocation.set(i, []);
156
+ * }
157
+ *
158
+ * // Assign tasks
159
+ * tasks.forEach(([task, load]) => {
160
+ * const machine = machineHeap.poll()!;
161
+ * allocation.get(machine.id)!.push([task, load]);
162
+ * machine.load += load;
163
+ * machineHeap.add(machine); // The machine after updating the load is re-entered into the heap
164
+ * });
165
+ *
166
+ * return allocation;
167
+ * }
168
+ *
169
+ * const tasks: Task[] = [
170
+ * ['Task1', 3],
171
+ * ['Task2', 1],
172
+ * ['Task3', 2],
173
+ * ['Task4', 5],
174
+ * ['Task5', 4]
175
+ * ];
176
+ * const expectedMap = new Map<number, Task[]>();
177
+ * expectedMap.set(0, [
178
+ * ['Task1', 3],
179
+ * ['Task4', 5]
180
+ * ]);
181
+ * expectedMap.set(1, [
182
+ * ['Task2', 1],
183
+ * ['Task3', 2],
184
+ * ['Task5', 4]
185
+ * ]);
186
+ * console.log(scheduleTasks(tasks, 2)); // expectedMap
22
187
  */
23
188
  export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>> {
24
189
  /**
@@ -86,6 +86,432 @@ export class DoublyLinkedListNode<E = any> {
86
86
  * 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.
87
87
  * 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.
88
88
  * 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.
89
+ * @example
90
+ * // text editor operation history
91
+ * const actions = [
92
+ * { type: 'insert', content: 'first line of text' },
93
+ * { type: 'insert', content: 'second line of text' },
94
+ * { type: 'delete', content: 'delete the first line' }
95
+ * ];
96
+ * const editorHistory = new DoublyLinkedList<{ type: string; content: string }>(actions);
97
+ *
98
+ * console.log(editorHistory.last?.type); // 'delete'
99
+ * console.log(editorHistory.pop()?.content); // 'delete the first line'
100
+ * console.log(editorHistory.last?.type); // 'insert'
101
+ * @example
102
+ * // Browser history
103
+ * const browserHistory = new DoublyLinkedList<string>();
104
+ *
105
+ * browserHistory.push('home page');
106
+ * browserHistory.push('search page');
107
+ * browserHistory.push('details page');
108
+ *
109
+ * console.log(browserHistory.last); // 'details page'
110
+ * console.log(browserHistory.pop()); // 'details page'
111
+ * console.log(browserHistory.last); // 'search page'
112
+ * @example
113
+ * // Use DoublyLinkedList to implement music player
114
+ * // Define the Song interface
115
+ * interface Song {
116
+ * title: string;
117
+ * artist: string;
118
+ * duration: number; // duration in seconds
119
+ * }
120
+ *
121
+ * class Player {
122
+ * private playlist: DoublyLinkedList<Song>;
123
+ * private currentSong: ReturnType<typeof this.playlist.getNodeAt> | undefined;
124
+ *
125
+ * constructor(songs: Song[]) {
126
+ * this.playlist = new DoublyLinkedList<Song>();
127
+ * songs.forEach(song => this.playlist.push(song));
128
+ * this.currentSong = this.playlist.head;
129
+ * }
130
+ *
131
+ * // Play the next song in the playlist
132
+ * playNext(): Song | undefined {
133
+ * if (!this.currentSong?.next) {
134
+ * this.currentSong = this.playlist.head; // Loop to the first song
135
+ * } else {
136
+ * this.currentSong = this.currentSong.next;
137
+ * }
138
+ * return this.currentSong?.value;
139
+ * }
140
+ *
141
+ * // Play the previous song in the playlist
142
+ * playPrevious(): Song | undefined {
143
+ * if (!this.currentSong?.prev) {
144
+ * this.currentSong = this.playlist.tail; // Loop to the last song
145
+ * } else {
146
+ * this.currentSong = this.currentSong.prev;
147
+ * }
148
+ * return this.currentSong?.value;
149
+ * }
150
+ *
151
+ * // Get the current song
152
+ * getCurrentSong(): Song | undefined {
153
+ * return this.currentSong?.value;
154
+ * }
155
+ *
156
+ * // Loop through the playlist twice
157
+ * loopThroughPlaylist(): Song[] {
158
+ * const playedSongs: Song[] = [];
159
+ * const initialNode = this.currentSong;
160
+ *
161
+ * // Loop through the playlist twice
162
+ * for (let i = 0; i < this.playlist.size * 2; i++) {
163
+ * playedSongs.push(this.currentSong!.value);
164
+ * this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed
165
+ * }
166
+ *
167
+ * // Reset the current song to the initial song
168
+ * this.currentSong = initialNode;
169
+ * return playedSongs;
170
+ * }
171
+ * }
172
+ *
173
+ * const songs = [
174
+ * { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
175
+ * { title: 'Hotel California', artist: 'Eagles', duration: 391 },
176
+ * { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
177
+ * { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
178
+ * ];
179
+ * let player = new Player(songs);
180
+ * // should play the next song
181
+ * player = new Player(songs);
182
+ * const firstSong = player.getCurrentSong();
183
+ * const nextSong = player.playNext();
184
+ *
185
+ * // Expect the next song to be "Hotel California by Eagles"
186
+ * console.log(nextSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
187
+ * console.log(firstSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
188
+ *
189
+ * // should play the previous song
190
+ * player = new Player(songs);
191
+ * player.playNext(); // Move to the second song
192
+ * const currentSong = player.getCurrentSong();
193
+ * const previousSong = player.playPrevious();
194
+ *
195
+ * // Expect the previous song to be "Bohemian Rhapsody by Queen"
196
+ * console.log(previousSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
197
+ * console.log(currentSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
198
+ *
199
+ * // should loop to the first song when playing next from the last song
200
+ * player = new Player(songs);
201
+ * player.playNext(); // Move to the second song
202
+ * player.playNext(); // Move to the third song
203
+ * player.playNext(); // Move to the fourth song
204
+ *
205
+ * const nextSongToFirst = player.playNext(); // Should loop to the first song
206
+ *
207
+ * // Expect the next song to be "Bohemian Rhapsody by Queen"
208
+ * console.log(nextSongToFirst); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
209
+ *
210
+ * // should loop to the last song when playing previous from the first song
211
+ * player = new Player(songs);
212
+ * player.playNext(); // Move to the first song
213
+ * player.playNext(); // Move to the second song
214
+ * player.playNext(); // Move to the third song
215
+ * player.playNext(); // Move to the fourth song
216
+ *
217
+ * const previousToLast = player.playPrevious(); // Should loop to the last song
218
+ *
219
+ * // Expect the previous song to be "Billie Jean by Michael Jackson"
220
+ * console.log(previousToLast); // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
221
+ *
222
+ * // should loop through the entire playlist
223
+ * player = new Player(songs);
224
+ * const playedSongs = player.loopThroughPlaylist();
225
+ *
226
+ * // The expected order of songs for two loops
227
+ * console.log(playedSongs); // [
228
+ * // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
229
+ * // { title: 'Hotel California', artist: 'Eagles', duration: 391 },
230
+ * // { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
231
+ * // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 },
232
+ * // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
233
+ * // { title: 'Hotel California', artist: 'Eagles', duration: 391 },
234
+ * // { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
235
+ * // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
236
+ * // ]
237
+ * @example
238
+ * // Use DoublyLinkedList to implement LRU cache
239
+ * interface CacheEntry<K, V> {
240
+ * key: K;
241
+ * value: V;
242
+ * }
243
+ *
244
+ * class LRUCache<K = string, V = any> {
245
+ * private readonly capacity: number;
246
+ * private list: DoublyLinkedList<CacheEntry<K, V>>;
247
+ * private map: Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>;
248
+ *
249
+ * constructor(capacity: number) {
250
+ * if (capacity <= 0) {
251
+ * throw new Error('lru cache capacity must be greater than 0');
252
+ * }
253
+ * this.capacity = capacity;
254
+ * this.list = new DoublyLinkedList<CacheEntry<K, V>>();
255
+ * this.map = new Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>();
256
+ * }
257
+ *
258
+ * // Get cached value
259
+ * get(key: K): V | undefined {
260
+ * const node = this.map.get(key);
261
+ *
262
+ * if (!node) return undefined;
263
+ *
264
+ * // Move the visited node to the head of the linked list (most recently used)
265
+ * this.moveToFront(node);
266
+ *
267
+ * return node.value.value;
268
+ * }
269
+ *
270
+ * // Set cache value
271
+ * set(key: K, value: V): void {
272
+ * // Check if it already exists
273
+ * const node = this.map.get(key);
274
+ *
275
+ * if (node) {
276
+ * // Update value and move to head
277
+ * node.value.value = value;
278
+ * this.moveToFront(node);
279
+ * return;
280
+ * }
281
+ *
282
+ * // Check capacity
283
+ * if (this.list.size >= this.capacity) {
284
+ * // Delete the least recently used element (the tail of the linked list)
285
+ * const removedNode = this.list.tail;
286
+ * if (removedNode) {
287
+ * this.map.delete(removedNode.value.key);
288
+ * this.list.pop();
289
+ * }
290
+ * }
291
+ *
292
+ * // Create new node and add to head
293
+ * const newEntry: CacheEntry<K, V> = { key, value };
294
+ * this.list.unshift(newEntry);
295
+ *
296
+ * // Save node reference in map
297
+ * const newNode = this.list.head;
298
+ * if (newNode) {
299
+ * this.map.set(key, newNode);
300
+ * }
301
+ * }
302
+ *
303
+ * // Move the node to the head of the linked list
304
+ * private moveToFront(node: DoublyLinkedListNode<CacheEntry<K, V>>): void {
305
+ * this.list.delete(node);
306
+ * this.list.unshift(node.value);
307
+ * }
308
+ *
309
+ * // Delete specific key
310
+ * delete(key: K): boolean {
311
+ * const node = this.map.get(key);
312
+ * if (!node) return false;
313
+ *
314
+ * // Remove from linked list
315
+ * this.list.delete(node);
316
+ * // Remove from map
317
+ * this.map.delete(key);
318
+ *
319
+ * return true;
320
+ * }
321
+ *
322
+ * // Clear cache
323
+ * clear(): void {
324
+ * this.list.clear();
325
+ * this.map.clear();
326
+ * }
327
+ *
328
+ * // Get the current cache size
329
+ * get size(): number {
330
+ * return this.list.size;
331
+ * }
332
+ *
333
+ * // Check if it is empty
334
+ * get isEmpty(): boolean {
335
+ * return this.list.isEmpty();
336
+ * }
337
+ * }
338
+ *
339
+ * // should set and get values correctly
340
+ * const cache = new LRUCache<string, number>(3);
341
+ * cache.set('a', 1);
342
+ * cache.set('b', 2);
343
+ * cache.set('c', 3);
344
+ *
345
+ * console.log(cache.get('a')); // 1
346
+ * console.log(cache.get('b')); // 2
347
+ * console.log(cache.get('c')); // 3
348
+ *
349
+ * // The least recently used element should be evicted when capacity is exceeded
350
+ * cache.clear();
351
+ * cache.set('a', 1);
352
+ * cache.set('b', 2);
353
+ * cache.set('c', 3);
354
+ * cache.set('d', 4); // This will eliminate 'a'
355
+ *
356
+ * console.log(cache.get('a')).toBeUndefined();
357
+ * expect(cache.get('b')); // 2
358
+ * console.log(cache.get('c')); // 3
359
+ * console.log(cache.get('d')); // 4
360
+ *
361
+ * // The priority of an element should be updated when it is accessed
362
+ * cache.clear();
363
+ * cache.set('a', 1);
364
+ * cache.set('b', 2);
365
+ * cache.set('c', 3);
366
+ *
367
+ * cache.get('a'); // access 'a'
368
+ * cache.set('d', 4); // This will eliminate 'b'
369
+ *
370
+ * console.log(cache.get('a')); // 1
371
+ * console.log(cache.get('b')).toBeUndefined();
372
+ * expect(cache.get('c')); // 3
373
+ * console.log(cache.get('d')); // 4
374
+ *
375
+ * // Should support updating existing keys
376
+ * cache.clear();
377
+ * cache.set('a', 1);
378
+ * cache.set('a', 10);
379
+ *
380
+ * console.log(cache.get('a')); // 10
381
+ *
382
+ * // Should support deleting specified keys
383
+ * cache.clear();
384
+ * cache.set('a', 1);
385
+ * cache.set('b', 2);
386
+ *
387
+ * console.log(cache.delete('a')); // true
388
+ * console.log(cache.get('a')).toBeUndefined();
389
+ * expect(cache.size); // 1
390
+ *
391
+ * // Should support clearing cache
392
+ * cache.clear();
393
+ * cache.set('a', 1);
394
+ * cache.set('b', 2);
395
+ * cache.clear();
396
+ *
397
+ * console.log(cache.size); // 0
398
+ * console.log(cache.isEmpty); // true
399
+ * @example
400
+ * // finding lyrics by timestamp in Coldplay's "Fix You"
401
+ * // Create a DoublyLinkedList to store song lyrics with timestamps
402
+ * const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
403
+ *
404
+ * // Detailed lyrics with precise timestamps (in milliseconds)
405
+ * const lyrics = [
406
+ * { time: 0, text: "When you try your best, but you don't succeed" },
407
+ * { time: 4000, text: 'When you get what you want, but not what you need' },
408
+ * { time: 8000, text: "When you feel so tired, but you can't sleep" },
409
+ * { time: 12000, text: 'Stuck in reverse' },
410
+ * { time: 16000, text: 'And the tears come streaming down your face' },
411
+ * { time: 20000, text: "When you lose something you can't replace" },
412
+ * { time: 24000, text: 'When you love someone, but it goes to waste' },
413
+ * { time: 28000, text: 'Could it be worse?' },
414
+ * { time: 32000, text: 'Lights will guide you home' },
415
+ * { time: 36000, text: 'And ignite your bones' },
416
+ * { time: 40000, text: 'And I will try to fix you' }
417
+ * ];
418
+ *
419
+ * // Populate the DoublyLinkedList with lyrics
420
+ * lyrics.forEach(lyric => lyricsList.push(lyric));
421
+ *
422
+ * // Test different scenarios of lyric synchronization
423
+ *
424
+ * // 1. Find lyric at exact timestamp
425
+ * const exactTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 36000);
426
+ * console.log(exactTimeLyric?.text); // 'And ignite your bones'
427
+ *
428
+ * // 2. Find lyric between timestamps
429
+ * const betweenTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 22000);
430
+ * console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
431
+ *
432
+ * // 3. Find first lyric when timestamp is less than first entry
433
+ * const earlyTimeLyric = lyricsList.findBackward(lyric => lyric.time <= -1000);
434
+ * console.log(earlyTimeLyric).toBeUndefined();
435
+ *
436
+ * // 4. Find last lyric when timestamp is after last entry
437
+ * const lateTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 50000);
438
+ * expect(lateTimeLyric?.text); // 'And I will try to fix you'
439
+ * @example
440
+ * // cpu process schedules
441
+ * class Process {
442
+ * constructor(
443
+ * public id: number,
444
+ * public priority: number
445
+ * ) {}
446
+ *
447
+ * execute(): string {
448
+ * return `Process ${this.id} executed.`;
449
+ * }
450
+ * }
451
+ *
452
+ * class Scheduler {
453
+ * private queue: DoublyLinkedList<Process>;
454
+ *
455
+ * constructor() {
456
+ * this.queue = new DoublyLinkedList<Process>();
457
+ * }
458
+ *
459
+ * addProcess(process: Process): void {
460
+ * // Insert processes into a queue based on priority, keeping priority in descending order
461
+ * let current = this.queue.head;
462
+ * while (current && current.value.priority >= process.priority) {
463
+ * current = current.next;
464
+ * }
465
+ *
466
+ * if (!current) {
467
+ * this.queue.push(process);
468
+ * } else {
469
+ * this.queue.addBefore(current, process);
470
+ * }
471
+ * }
472
+ *
473
+ * executeNext(): string | undefined {
474
+ * // Execute tasks at the head of the queue in order
475
+ * const process = this.queue.shift();
476
+ * return process ? process.execute() : undefined;
477
+ * }
478
+ *
479
+ * listProcesses(): string[] {
480
+ * return this.queue.toArray().map(process => `Process ${process.id} (Priority: ${process.priority})`);
481
+ * }
482
+ *
483
+ * clear(): void {
484
+ * this.queue.clear();
485
+ * }
486
+ * }
487
+ *
488
+ * // should add processes based on priority
489
+ * let scheduler = new Scheduler();
490
+ * scheduler.addProcess(new Process(1, 10));
491
+ * scheduler.addProcess(new Process(2, 20));
492
+ * scheduler.addProcess(new Process(3, 15));
493
+ *
494
+ * console.log(scheduler.listProcesses()); // [
495
+ * // 'Process 2 (Priority: 20)',
496
+ * // 'Process 3 (Priority: 15)',
497
+ * // 'Process 1 (Priority: 10)'
498
+ * // ]
499
+ *
500
+ * // should execute the highest priority process
501
+ * scheduler = new Scheduler();
502
+ * scheduler.addProcess(new Process(1, 10));
503
+ * scheduler.addProcess(new Process(2, 20));
504
+ *
505
+ * console.log(scheduler.executeNext()); // 'Process 2 executed.'
506
+ * console.log(scheduler.listProcesses()); // ['Process 1 (Priority: 10)']
507
+ *
508
+ * // should clear all processes
509
+ * scheduler = new Scheduler();
510
+ * scheduler.addProcess(new Process(1, 10));
511
+ * scheduler.addProcess(new Process(2, 20));
512
+ *
513
+ * scheduler.clear();
514
+ * console.log(scheduler.listProcesses()); // []
89
515
  */
90
516
  export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
91
517
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: DoublyLinkedListOptions<E, R>) {
@@ -488,8 +914,8 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
488
914
  } else {
489
915
  const prevNode = node.prev;
490
916
  const nextNode = node.next;
491
- prevNode!.next = nextNode;
492
- nextNode!.prev = prevNode;
917
+ if (prevNode) prevNode.next = nextNode;
918
+ if (nextNode) nextNode.prev = prevNode;
493
919
  this._size--;
494
920
  }
495
921
  return true;