data-structure-typed 1.53.3 → 1.53.5
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +169 -84
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +210 -104
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +169 -84
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +210 -104
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/umd/data-structure-typed.js +193 -92
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +228 -105
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +7 -7
- package/testToExample.ts +7 -7
|
@@ -2177,28 +2177,33 @@ var dataStructureTyped = (() => {
|
|
|
2177
2177
|
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
2178
2178
|
}
|
|
2179
2179
|
/**
|
|
2180
|
-
* Time Complexity: O(
|
|
2181
|
-
* Space Complexity: O(
|
|
2180
|
+
* Time Complexity: O(1)
|
|
2181
|
+
* Space Complexity: O(1)
|
|
2182
2182
|
*
|
|
2183
|
-
* The `
|
|
2184
|
-
*
|
|
2185
|
-
* @param {E
|
|
2186
|
-
*
|
|
2183
|
+
* The function `isNode` in TypeScript checks if a given input is an instance of
|
|
2184
|
+
* `DoublyLinkedListNode`.
|
|
2185
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
2186
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
|
|
2187
|
+
* be one of the following types:
|
|
2188
|
+
* @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
|
|
2189
|
+
* instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
|
|
2190
|
+
* parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
|
|
2191
|
+
* the function returns `false`.
|
|
2187
2192
|
*/
|
|
2188
|
-
|
|
2189
|
-
return
|
|
2193
|
+
isNode(elementNodeOrPredicate) {
|
|
2194
|
+
return elementNodeOrPredicate instanceof DoublyLinkedListNode;
|
|
2190
2195
|
}
|
|
2191
2196
|
/**
|
|
2192
2197
|
* Time Complexity: O(1)
|
|
2193
2198
|
* Space Complexity: O(1)
|
|
2194
2199
|
*
|
|
2195
|
-
* The push function adds a new element to the end of a doubly linked list.
|
|
2196
|
-
* @param {E}
|
|
2197
|
-
*
|
|
2198
|
-
* @returns The `push` method is returning a boolean value, `true`.
|
|
2200
|
+
* The `push` function adds a new element or node to the end of a doubly linked list.
|
|
2201
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
|
|
2202
|
+
* method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
2203
|
+
* @returns The `push` method is returning a boolean value, specifically `true`.
|
|
2199
2204
|
*/
|
|
2200
|
-
push(
|
|
2201
|
-
const newNode =
|
|
2205
|
+
push(elementOrNode) {
|
|
2206
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
2202
2207
|
if (!this.head) {
|
|
2203
2208
|
this._head = newNode;
|
|
2204
2209
|
this._tail = newNode;
|
|
@@ -2254,13 +2259,14 @@ var dataStructureTyped = (() => {
|
|
|
2254
2259
|
* Time Complexity: O(1)
|
|
2255
2260
|
* Space Complexity: O(1)
|
|
2256
2261
|
*
|
|
2257
|
-
* The unshift function adds a new element to the beginning of a doubly linked list.
|
|
2258
|
-
* @param {E}
|
|
2259
|
-
*
|
|
2260
|
-
*
|
|
2262
|
+
* The unshift function adds a new element or node to the beginning of a doubly linked list.
|
|
2263
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
2264
|
+
* `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
|
|
2265
|
+
* element of type `E`.
|
|
2266
|
+
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
2261
2267
|
*/
|
|
2262
|
-
unshift(
|
|
2263
|
-
const newNode =
|
|
2268
|
+
unshift(elementOrNode) {
|
|
2269
|
+
const newNode = this._ensureNode(elementOrNode);
|
|
2264
2270
|
if (!this.head) {
|
|
2265
2271
|
this._head = newNode;
|
|
2266
2272
|
this._tail = newNode;
|
|
@@ -2313,16 +2319,26 @@ var dataStructureTyped = (() => {
|
|
|
2313
2319
|
* Time Complexity: O(n)
|
|
2314
2320
|
* Space Complexity: O(1)
|
|
2315
2321
|
*
|
|
2316
|
-
*
|
|
2317
|
-
*
|
|
2318
|
-
* @param {E
|
|
2319
|
-
*
|
|
2320
|
-
*
|
|
2322
|
+
* This TypeScript function searches for a node in a doubly linked list based on a given element node
|
|
2323
|
+
* or predicate.
|
|
2324
|
+
* @param {| E
|
|
2325
|
+
* | DoublyLinkedListNode<E>
|
|
2326
|
+
* | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
2327
|
+
* | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
|
|
2328
|
+
* node in a doubly linked list based on a given element, node, or predicate function. The
|
|
2329
|
+
* `elementNodeOrPredicate` parameter can be one of the following:
|
|
2330
|
+
* @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
|
|
2331
|
+
* input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
|
|
2332
|
+
* Otherwise, it iterates through the linked list starting from the head node and applies the
|
|
2333
|
+
* provided predicate function to each node. If a node satisfies the predicate, that node is
|
|
2334
|
+
* returned. If
|
|
2321
2335
|
*/
|
|
2322
|
-
getNode(
|
|
2336
|
+
getNode(elementNodeOrPredicate) {
|
|
2337
|
+
if (elementNodeOrPredicate === void 0) return;
|
|
2338
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2323
2339
|
let current = this.head;
|
|
2324
2340
|
while (current) {
|
|
2325
|
-
if (current
|
|
2341
|
+
if (predicate(current)) {
|
|
2326
2342
|
return current;
|
|
2327
2343
|
}
|
|
2328
2344
|
current = current.next;
|
|
@@ -2333,25 +2349,27 @@ var dataStructureTyped = (() => {
|
|
|
2333
2349
|
* Time Complexity: O(n)
|
|
2334
2350
|
* Space Complexity: O(1)
|
|
2335
2351
|
*
|
|
2336
|
-
* The `
|
|
2337
|
-
* @param {number} index - The index parameter
|
|
2338
|
-
*
|
|
2339
|
-
*
|
|
2340
|
-
*
|
|
2341
|
-
*
|
|
2342
|
-
* if the
|
|
2352
|
+
* The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
|
|
2353
|
+
* @param {number} index - The `index` parameter in the `addAt` method represents the position at
|
|
2354
|
+
* which you want to add a new element or node in the doubly linked list. It indicates the location
|
|
2355
|
+
* where the new element or node should be inserted.
|
|
2356
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
2357
|
+
* `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
2358
|
+
* @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
|
|
2359
|
+
* successfully added at the specified index, and `false` if the index is out of bounds (less than 0
|
|
2360
|
+
* or greater than the size of the list).
|
|
2343
2361
|
*/
|
|
2344
|
-
addAt(index,
|
|
2362
|
+
addAt(index, newElementOrNode) {
|
|
2345
2363
|
if (index < 0 || index > this._size) return false;
|
|
2346
2364
|
if (index === 0) {
|
|
2347
|
-
this.unshift(
|
|
2365
|
+
this.unshift(newElementOrNode);
|
|
2348
2366
|
return true;
|
|
2349
2367
|
}
|
|
2350
2368
|
if (index === this._size) {
|
|
2351
|
-
this.push(
|
|
2369
|
+
this.push(newElementOrNode);
|
|
2352
2370
|
return true;
|
|
2353
2371
|
}
|
|
2354
|
-
const newNode =
|
|
2372
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
2355
2373
|
const prevNode = this.getNodeAt(index - 1);
|
|
2356
2374
|
const nextNode = prevNode.next;
|
|
2357
2375
|
newNode.prev = prevNode;
|
|
@@ -2365,24 +2383,26 @@ var dataStructureTyped = (() => {
|
|
|
2365
2383
|
* Time Complexity: O(1) or O(n)
|
|
2366
2384
|
* Space Complexity: O(1)
|
|
2367
2385
|
*
|
|
2368
|
-
* The `addBefore` function
|
|
2369
|
-
*
|
|
2370
|
-
*
|
|
2371
|
-
*
|
|
2372
|
-
* @param {E}
|
|
2373
|
-
*
|
|
2374
|
-
*
|
|
2375
|
-
*
|
|
2386
|
+
* The `addBefore` function in TypeScript adds a new element or node before an existing element or
|
|
2387
|
+
* node in a doubly linked list.
|
|
2388
|
+
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
|
|
2389
|
+
* in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
|
|
2390
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
|
|
2391
|
+
* represents the element or node that you want to add before the `existingElementOrNode` in a doubly
|
|
2392
|
+
* linked list.
|
|
2393
|
+
* @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
|
|
2394
|
+
* successfully added before the existing element or node, and `false` if the existing element or
|
|
2395
|
+
* node was not found.
|
|
2376
2396
|
*/
|
|
2377
|
-
addBefore(
|
|
2397
|
+
addBefore(existingElementOrNode, newElementOrNode) {
|
|
2378
2398
|
let existingNode;
|
|
2379
|
-
if (
|
|
2380
|
-
existingNode =
|
|
2399
|
+
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
2400
|
+
existingNode = existingElementOrNode;
|
|
2381
2401
|
} else {
|
|
2382
|
-
existingNode = this.getNode(
|
|
2402
|
+
existingNode = this.getNode(existingElementOrNode);
|
|
2383
2403
|
}
|
|
2384
2404
|
if (existingNode) {
|
|
2385
|
-
const newNode =
|
|
2405
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
2386
2406
|
newNode.prev = existingNode.prev;
|
|
2387
2407
|
if (existingNode.prev) {
|
|
2388
2408
|
existingNode.prev.next = newNode;
|
|
@@ -2401,23 +2421,27 @@ var dataStructureTyped = (() => {
|
|
|
2401
2421
|
* Time Complexity: O(1) or O(n)
|
|
2402
2422
|
* Space Complexity: O(1)
|
|
2403
2423
|
*
|
|
2404
|
-
* The `addAfter` function
|
|
2405
|
-
*
|
|
2406
|
-
*
|
|
2407
|
-
*
|
|
2408
|
-
* @param {E}
|
|
2409
|
-
*
|
|
2410
|
-
*
|
|
2424
|
+
* The `addAfter` function in TypeScript adds a new element or node after an existing element or node
|
|
2425
|
+
* in a doubly linked list.
|
|
2426
|
+
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
|
|
2427
|
+
* element or node in the doubly linked list after which you want to add a new element or node.
|
|
2428
|
+
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
2429
|
+
* `addAfter` method represents the element or node that you want to add after the existing element
|
|
2430
|
+
* or node in a doubly linked list. This parameter can be either an element value or a
|
|
2431
|
+
* `DoublyLinkedListNode` object that you want to insert
|
|
2432
|
+
* @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
|
|
2433
|
+
* successfully added after the existing element or node, and `false` if the existing element or node
|
|
2434
|
+
* was not found in the linked list.
|
|
2411
2435
|
*/
|
|
2412
|
-
addAfter(
|
|
2436
|
+
addAfter(existingElementOrNode, newElementOrNode) {
|
|
2413
2437
|
let existingNode;
|
|
2414
|
-
if (
|
|
2415
|
-
existingNode =
|
|
2438
|
+
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
2439
|
+
existingNode = existingElementOrNode;
|
|
2416
2440
|
} else {
|
|
2417
|
-
existingNode = this.getNode(
|
|
2441
|
+
existingNode = this.getNode(existingElementOrNode);
|
|
2418
2442
|
}
|
|
2419
2443
|
if (existingNode) {
|
|
2420
|
-
const newNode =
|
|
2444
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
2421
2445
|
newNode.next = existingNode.next;
|
|
2422
2446
|
if (existingNode.next) {
|
|
2423
2447
|
existingNode.next.prev = newNode;
|
|
@@ -2464,19 +2488,17 @@ var dataStructureTyped = (() => {
|
|
|
2464
2488
|
* Time Complexity: O(1) or O(n)
|
|
2465
2489
|
* Space Complexity: O(1)
|
|
2466
2490
|
*
|
|
2467
|
-
* The `delete` function removes a node from a doubly linked list
|
|
2468
|
-
* @param {E | DoublyLinkedListNode<E>}
|
|
2469
|
-
* a `DoublyLinkedListNode
|
|
2470
|
-
*
|
|
2471
|
-
*
|
|
2491
|
+
* The `delete` function removes a specified element or node from a doubly linked list if it exists.
|
|
2492
|
+
* @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
|
|
2493
|
+
* the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
|
|
2494
|
+
* can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
|
|
2495
|
+
* doubly linked list
|
|
2496
|
+
* @returns The `delete` method returns a boolean value - `true` if the element or node was
|
|
2497
|
+
* successfully deleted from the doubly linked list, and `false` if the element or node was not found
|
|
2498
|
+
* in the list.
|
|
2472
2499
|
*/
|
|
2473
|
-
delete(
|
|
2474
|
-
|
|
2475
|
-
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
2476
|
-
node = valOrNode;
|
|
2477
|
-
} else {
|
|
2478
|
-
node = this.getNode(valOrNode);
|
|
2479
|
-
}
|
|
2500
|
+
delete(elementOrNode) {
|
|
2501
|
+
const node = this.getNode(elementOrNode);
|
|
2480
2502
|
if (node) {
|
|
2481
2503
|
if (node === this.head) {
|
|
2482
2504
|
this.shift();
|
|
@@ -2518,17 +2540,21 @@ var dataStructureTyped = (() => {
|
|
|
2518
2540
|
* Time Complexity: O(n)
|
|
2519
2541
|
* Space Complexity: O(1)
|
|
2520
2542
|
*
|
|
2521
|
-
* The function returns the index of
|
|
2522
|
-
*
|
|
2523
|
-
*
|
|
2524
|
-
*
|
|
2525
|
-
*
|
|
2543
|
+
* The indexOf function in TypeScript returns the index of a specified element or node in a Doubly
|
|
2544
|
+
* Linked List.
|
|
2545
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
2546
|
+
* `indexOf` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
|
|
2547
|
+
* element of type `E`.
|
|
2548
|
+
* @returns The `indexOf` method is returning the index of the element or node in the doubly linked
|
|
2549
|
+
* list. If the element or node is found in the list, the method returns the index of that element or
|
|
2550
|
+
* node. If the element or node is not found in the list, the method returns -1.
|
|
2526
2551
|
*/
|
|
2527
|
-
indexOf(
|
|
2552
|
+
indexOf(elementOrNode) {
|
|
2553
|
+
const predicate = this._ensurePredicate(elementOrNode);
|
|
2528
2554
|
let index = 0;
|
|
2529
2555
|
let current = this.head;
|
|
2530
2556
|
while (current) {
|
|
2531
|
-
if (current
|
|
2557
|
+
if (predicate(current)) {
|
|
2532
2558
|
return index;
|
|
2533
2559
|
}
|
|
2534
2560
|
index++;
|
|
@@ -2540,19 +2566,43 @@ var dataStructureTyped = (() => {
|
|
|
2540
2566
|
* Time Complexity: O(n)
|
|
2541
2567
|
* Space Complexity: O(1)
|
|
2542
2568
|
*
|
|
2543
|
-
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
2544
|
-
* value that satisfies the given callback function, or undefined if no value satisfies the callback.
|
|
2545
|
-
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
2546
|
-
* function is used to determine whether a given value satisfies a certain condition.
|
|
2547
|
-
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
2548
|
-
* the callback function. If no value satisfies the condition, it returns `undefined`.
|
|
2549
2569
|
*/
|
|
2550
|
-
|
|
2570
|
+
/**
|
|
2571
|
+
* This function retrieves an element from a doubly linked list based on a given element
|
|
2572
|
+
* node or predicate.
|
|
2573
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
2574
|
+
* elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
|
|
2575
|
+
* which can be one of the following types:
|
|
2576
|
+
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
2577
|
+
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
2578
|
+
*/
|
|
2579
|
+
get(elementNodeOrPredicate) {
|
|
2580
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2581
|
+
let current = this.head;
|
|
2582
|
+
while (current) {
|
|
2583
|
+
if (predicate(current)) return current.value;
|
|
2584
|
+
current = current.next;
|
|
2585
|
+
}
|
|
2586
|
+
return void 0;
|
|
2587
|
+
}
|
|
2588
|
+
/**
|
|
2589
|
+
* Time Complexity: O(n)
|
|
2590
|
+
* Space Complexity: O(1)
|
|
2591
|
+
*
|
|
2592
|
+
* The `getBackward` function searches for a specific element in a doubly linked list starting from
|
|
2593
|
+
* the tail and moving backwards.
|
|
2594
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
2595
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
|
|
2596
|
+
* function can be one of the following types:
|
|
2597
|
+
* @returns The `getBackward` method returns the value of the element node that matches the provided
|
|
2598
|
+
* predicate when traversing the doubly linked list backwards. If no matching element is found, it
|
|
2599
|
+
* returns `undefined`.
|
|
2600
|
+
*/
|
|
2601
|
+
getBackward(elementNodeOrPredicate) {
|
|
2602
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2551
2603
|
let current = this.tail;
|
|
2552
2604
|
while (current) {
|
|
2553
|
-
if (
|
|
2554
|
-
return current.value;
|
|
2555
|
-
}
|
|
2605
|
+
if (predicate(current)) return current.value;
|
|
2556
2606
|
current = current.prev;
|
|
2557
2607
|
}
|
|
2558
2608
|
return void 0;
|
|
@@ -2674,6 +2724,18 @@ var dataStructureTyped = (() => {
|
|
|
2674
2724
|
}
|
|
2675
2725
|
return mappedList;
|
|
2676
2726
|
}
|
|
2727
|
+
/**
|
|
2728
|
+
* Time Complexity: O(n)
|
|
2729
|
+
* Space Complexity: O(n)
|
|
2730
|
+
*
|
|
2731
|
+
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
2732
|
+
* given array.
|
|
2733
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
2734
|
+
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
2735
|
+
*/
|
|
2736
|
+
static fromArray(data) {
|
|
2737
|
+
return new _DoublyLinkedList(data);
|
|
2738
|
+
}
|
|
2677
2739
|
/**
|
|
2678
2740
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
2679
2741
|
*/
|
|
@@ -2684,6 +2746,45 @@ var dataStructureTyped = (() => {
|
|
|
2684
2746
|
current = current.next;
|
|
2685
2747
|
}
|
|
2686
2748
|
}
|
|
2749
|
+
/**
|
|
2750
|
+
* The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
|
|
2751
|
+
* as an argument and returns a boolean.
|
|
2752
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
2753
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
2754
|
+
* types:
|
|
2755
|
+
* @returns The _isPredicate method is returning a boolean value indicating whether the
|
|
2756
|
+
* elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
|
|
2757
|
+
* function, the method will return true, indicating that it is a predicate function.
|
|
2758
|
+
*/
|
|
2759
|
+
_isPredicate(elementNodeOrPredicate) {
|
|
2760
|
+
return typeof elementNodeOrPredicate === "function";
|
|
2761
|
+
}
|
|
2762
|
+
/**
|
|
2763
|
+
* The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
|
|
2764
|
+
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
|
|
2765
|
+
* an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
|
|
2766
|
+
* @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
|
|
2767
|
+
* as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
|
|
2768
|
+
* value and returned.
|
|
2769
|
+
*/
|
|
2770
|
+
_ensureNode(elementOrNode) {
|
|
2771
|
+
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
2772
|
+
return new DoublyLinkedListNode(elementOrNode);
|
|
2773
|
+
}
|
|
2774
|
+
/**
|
|
2775
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
|
|
2776
|
+
* function, or a value to compare with the node's value.
|
|
2777
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
2778
|
+
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
2779
|
+
* types:
|
|
2780
|
+
* @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
|
|
2781
|
+
* returns a boolean value based on the conditions specified in the code.
|
|
2782
|
+
*/
|
|
2783
|
+
_ensurePredicate(elementNodeOrPredicate) {
|
|
2784
|
+
if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
|
|
2785
|
+
if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
2786
|
+
return (node) => node.value === elementNodeOrPredicate;
|
|
2787
|
+
}
|
|
2687
2788
|
};
|
|
2688
2789
|
|
|
2689
2790
|
// src/data-structures/linked-list/skip-linked-list.ts
|