data-structure-typed 1.53.4 → 1.53.6

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.
@@ -1562,33 +1562,17 @@ var dataStructureTyped = (() => {
1562
1562
  get size() {
1563
1563
  return this._size;
1564
1564
  }
1565
- /**
1566
- * Time Complexity: O(n)
1567
- * Space Complexity: O(n)
1568
- *
1569
- * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
1570
- * array.
1571
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
1572
- * @returns The `fromArray` function returns a `SinglyLinkedList` object.
1573
- */
1574
- static fromArray(data) {
1575
- const singlyLinkedList = new _SinglyLinkedList();
1576
- for (const item of data) {
1577
- singlyLinkedList.push(item);
1578
- }
1579
- return singlyLinkedList;
1580
- }
1581
1565
  /**
1582
1566
  * Time Complexity: O(1)
1583
1567
  * Space Complexity: O(1)
1584
1568
  *
1585
- * The push function adds a new element to the end of a singly linked list.
1586
- * @param {E} element - The "element" parameter represents the value of the element that you want to
1587
- * add to the linked list.
1588
- * @returns The `push` method is returning a boolean value, `true`.
1569
+ * The `push` function adds a new element or node to the end of a singly linked list.
1570
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
1571
+ * method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
1572
+ * @returns The `push` method is returning a boolean value, specifically `true`.
1589
1573
  */
1590
- push(element) {
1591
- const newNode = new SinglyLinkedListNode(element);
1574
+ push(elementOrNode) {
1575
+ const newNode = this._ensureNode(elementOrNode);
1592
1576
  if (!this.head) {
1593
1577
  this._head = newNode;
1594
1578
  this._tail = newNode;
@@ -1644,13 +1628,15 @@ var dataStructureTyped = (() => {
1644
1628
  * Time Complexity: O(1)
1645
1629
  * Space Complexity: O(1)
1646
1630
  *
1647
- * The unshift function adds a new element to the beginning of a singly linked list.
1648
- * @param {E} element - The "element" parameter represents the value of the element that you want to
1649
- * add to the beginning of the singly linked list.
1650
- * @returns The `unshift` method is returning a boolean value, `true`.
1631
+ * The unshift function adds a new element or node to the beginning of a singly linked list in
1632
+ * TypeScript.
1633
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
1634
+ * `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
1635
+ * element of type `E`.
1636
+ * @returns The `unshift` method is returning a boolean value, specifically `true`.
1651
1637
  */
1652
- unshift(element) {
1653
- const newNode = new SinglyLinkedListNode(element);
1638
+ unshift(elementOrNode) {
1639
+ const newNode = this._ensureNode(elementOrNode);
1654
1640
  if (!this.head) {
1655
1641
  this._head = newNode;
1656
1642
  this._tail = newNode;
@@ -1661,6 +1647,27 @@ var dataStructureTyped = (() => {
1661
1647
  this._size++;
1662
1648
  return true;
1663
1649
  }
1650
+ /**
1651
+ * Time Complexity: O(n)
1652
+ * Space Complexity: O(1)
1653
+ *
1654
+ * This function searches for a specific element in a singly linked list based on a given node or
1655
+ * predicate.
1656
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1657
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
1658
+ * the following types:
1659
+ * @returns The `get` method returns the value of the first node in the singly linked list that
1660
+ * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
1661
+ */
1662
+ get(elementNodeOrPredicate) {
1663
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1664
+ let current = this.head;
1665
+ while (current) {
1666
+ if (predicate(current)) return current.value;
1667
+ current = current.next;
1668
+ }
1669
+ return void 0;
1670
+ }
1664
1671
  /**
1665
1672
  * Time Complexity: O(n)
1666
1673
  * Space Complexity: O(1)
@@ -1679,6 +1686,22 @@ var dataStructureTyped = (() => {
1679
1686
  }
1680
1687
  return current.value;
1681
1688
  }
1689
+ /**
1690
+ * Time Complexity: O(1)
1691
+ * Space Complexity: O(1)
1692
+ *
1693
+ * The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
1694
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1695
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
1696
+ * one of the following types:
1697
+ * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
1698
+ * instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
1699
+ * parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
1700
+ * the function returns `false`.
1701
+ */
1702
+ isNode(elementNodeOrPredicate) {
1703
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
1704
+ }
1682
1705
  /**
1683
1706
  * Time Complexity: O(n)
1684
1707
  * Space Complexity: O(1)
@@ -1727,18 +1750,18 @@ var dataStructureTyped = (() => {
1727
1750
  * Space Complexity: O(1)
1728
1751
  *
1729
1752
  * The delete function removes a node with a specific value from a singly linked list.
1730
- * @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
1753
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
1731
1754
  * or a `SinglyLinkedListNode<E>` object.
1732
1755
  * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
1733
1756
  * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
1734
1757
  */
1735
- delete(valueOrNode) {
1736
- if (valueOrNode === void 0) return false;
1758
+ delete(elementOrNode) {
1759
+ if (elementOrNode === void 0) return false;
1737
1760
  let value;
1738
- if (valueOrNode instanceof SinglyLinkedListNode) {
1739
- value = valueOrNode.value;
1761
+ if (elementOrNode instanceof SinglyLinkedListNode) {
1762
+ value = elementOrNode.value;
1740
1763
  } else {
1741
- value = valueOrNode;
1764
+ value = elementOrNode;
1742
1765
  }
1743
1766
  let current = this.head, prev = void 0;
1744
1767
  while (current) {
@@ -1766,25 +1789,28 @@ var dataStructureTyped = (() => {
1766
1789
  * Time Complexity: O(n)
1767
1790
  * Space Complexity: O(1)
1768
1791
  *
1769
- * The `addAt` function inserts a value at a specified index in a singly linked list.
1770
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
1771
- * linked list. It is of type number.
1772
- * @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
1773
- * specified index.
1774
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
1775
- * if the index is out of bounds.
1792
+ * The `addAt` function inserts a new element or node at a specified index in a singly linked list.
1793
+ * @param {number} index - The `index` parameter represents the position at which you want to add a
1794
+ * new element or node in the linked list. It is a number that indicates the index where the new
1795
+ * element or node should be inserted.
1796
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1797
+ * `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
1798
+ * parameter represents the element or node that you want to add to the linked list at the specified
1799
+ * index.
1800
+ * @returns The `addAt` method returns a boolean value - `true` if the element or node was
1801
+ * successfully added at the specified index, and `false` if the index is out of bounds.
1776
1802
  */
1777
- addAt(index, value) {
1803
+ addAt(index, newElementOrNode) {
1778
1804
  if (index < 0 || index > this._size) return false;
1779
1805
  if (index === 0) {
1780
- this.unshift(value);
1806
+ this.unshift(newElementOrNode);
1781
1807
  return true;
1782
1808
  }
1783
1809
  if (index === this._size) {
1784
- this.push(value);
1810
+ this.push(newElementOrNode);
1785
1811
  return true;
1786
1812
  }
1787
- const newNode = new SinglyLinkedListNode(value);
1813
+ const newNode = this._ensureNode(newElementOrNode);
1788
1814
  const prevNode = this.getNodeAt(index - 1);
1789
1815
  newNode.next = prevNode.next;
1790
1816
  prevNode.next = newNode;
@@ -1848,16 +1874,21 @@ var dataStructureTyped = (() => {
1848
1874
  * Time Complexity: O(n)
1849
1875
  * Space Complexity: O(1)
1850
1876
  *
1851
- * The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
1852
- * @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
1853
- * @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
1854
- * value is not found, it returns -1.
1877
+ * The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
1878
+ * list and returns its index if found.
1879
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1880
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
1881
+ * of the following types:
1882
+ * @returns The `indexOf` method returns the index of the first occurrence of the element that
1883
+ * matches the provided predicate in the singly linked list. If no matching element is found, it
1884
+ * returns -1.
1855
1885
  */
1856
- indexOf(value) {
1886
+ indexOf(elementNodeOrPredicate) {
1887
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1857
1888
  let index = 0;
1858
1889
  let current = this.head;
1859
1890
  while (current) {
1860
- if (current.value === value) {
1891
+ if (predicate(current)) {
1861
1892
  return index;
1862
1893
  }
1863
1894
  index++;
@@ -1869,16 +1900,21 @@ var dataStructureTyped = (() => {
1869
1900
  * Time Complexity: O(n)
1870
1901
  * Space Complexity: O(1)
1871
1902
  *
1872
- * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
1873
- * undefined.
1874
- * @param {E} value - The value parameter is the value that we want to search for in the linked list.
1875
- * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
1876
- * the specified value is found, the function returns `undefined`.
1903
+ * The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
1904
+ * element, node, or predicate.
1905
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
1906
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
1907
+ * of the following types:
1908
+ * @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
1909
+ * found based on the provided predicate, or it returns `undefined` if no matching node is found or
1910
+ * if the input parameter is `undefined`.
1877
1911
  */
1878
- getNode(value) {
1912
+ getNode(elementNodeOrPredicate) {
1913
+ if (elementNodeOrPredicate === void 0) return;
1914
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1879
1915
  let current = this.head;
1880
1916
  while (current) {
1881
- if (current.value === value) {
1917
+ if (predicate(current)) {
1882
1918
  return current;
1883
1919
  }
1884
1920
  current = current.next;
@@ -1889,29 +1925,34 @@ var dataStructureTyped = (() => {
1889
1925
  * Time Complexity: O(n)
1890
1926
  * Space Complexity: O(1)
1891
1927
  *
1892
- * The `addBefore` function inserts a new value before an existing value in a singly linked list.
1893
- * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
1894
- * new value before. It can be either the value itself or a node containing the value in the linked list.
1895
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
1896
- * @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
1897
- * inserted before the existing value, and `false` otherwise.
1928
+ * The function `addBefore` in TypeScript adds a new element or node before an existing element or
1929
+ * node in a singly linked list.
1930
+ * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
1931
+ * element or node in the linked list before which you want to add a new element or node.
1932
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1933
+ * `addBefore` method represents the element or node that you want to insert before the existing
1934
+ * element or node in the linked list. This new element can be of type `E` or a
1935
+ * `SinglyLinkedListNode<E>`.
1936
+ * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
1937
+ * successfully added before the existing element or node, and `false` if the operation was
1938
+ * unsuccessful.
1898
1939
  */
1899
- addBefore(existingValueOrNode, newValue) {
1940
+ addBefore(existingElementOrNode, newElementOrNode) {
1900
1941
  if (!this.head) return false;
1901
1942
  let existingValue;
1902
- if (existingValueOrNode instanceof SinglyLinkedListNode) {
1903
- existingValue = existingValueOrNode.value;
1943
+ if (this.isNode(existingElementOrNode)) {
1944
+ existingValue = existingElementOrNode.value;
1904
1945
  } else {
1905
- existingValue = existingValueOrNode;
1946
+ existingValue = existingElementOrNode;
1906
1947
  }
1907
1948
  if (this.head.value === existingValue) {
1908
- this.unshift(newValue);
1949
+ this.unshift(newElementOrNode);
1909
1950
  return true;
1910
1951
  }
1911
1952
  let current = this.head;
1912
1953
  while (current.next) {
1913
1954
  if (current.next.value === existingValue) {
1914
- const newNode = new SinglyLinkedListNode(newValue);
1955
+ const newNode = this._ensureNode(newElementOrNode);
1915
1956
  newNode.next = current.next;
1916
1957
  current.next = newNode;
1917
1958
  this._size++;
@@ -1925,22 +1966,22 @@ var dataStructureTyped = (() => {
1925
1966
  * Time Complexity: O(n)
1926
1967
  * Space Complexity: O(1)
1927
1968
  *
1928
- * The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
1929
- * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
1930
- * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
1931
- * @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
1932
- * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
1933
- * existing value or node, and false if the existing value or node was not found in the linked list.
1969
+ * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
1970
+ * in a singly linked list.
1971
+ * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
1972
+ * an element of type E or a SinglyLinkedListNode of type E.
1973
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1974
+ * `addAfter` method represents the element or node that you want to add after the existing element
1975
+ * or node in a singly linked list. This parameter can be either the value of the new element or a
1976
+ * reference to a `SinglyLinkedListNode` containing
1977
+ * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
1978
+ * successfully added after the existing element or node, and `false` if the existing element or node
1979
+ * was not found.
1934
1980
  */
1935
- addAfter(existingValueOrNode, newValue) {
1936
- let existingNode;
1937
- if (existingValueOrNode instanceof SinglyLinkedListNode) {
1938
- existingNode = existingValueOrNode;
1939
- } else {
1940
- existingNode = this.getNode(existingValueOrNode);
1941
- }
1981
+ addAfter(existingElementOrNode, newElementOrNode) {
1982
+ const existingNode = this.getNode(existingElementOrNode);
1942
1983
  if (existingNode) {
1943
- const newNode = new SinglyLinkedListNode(newValue);
1984
+ const newNode = this._ensureNode(newElementOrNode);
1944
1985
  newNode.next = existingNode.next;
1945
1986
  existingNode.next = newNode;
1946
1987
  if (existingNode === this.tail) {
@@ -1955,15 +1996,19 @@ var dataStructureTyped = (() => {
1955
1996
  * Time Complexity: O(n)
1956
1997
  * Space Complexity: O(1)
1957
1998
  *
1958
- * The function counts the number of occurrences of a given value in a linked list.
1959
- * @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
1960
- * @returns The count of occurrences of the given value in the linked list.
1999
+ * The function `countOccurrences` iterates through a singly linked list and counts the occurrences
2000
+ * of a specified element or nodes that satisfy a given predicate.
2001
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
2002
+ * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
2003
+ * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
2004
+ * node, or predicate function in the singly linked list.
1961
2005
  */
1962
- countOccurrences(value) {
2006
+ countOccurrences(elementOrNode) {
2007
+ const predicate = this._ensurePredicate(elementOrNode);
1963
2008
  let count = 0;
1964
2009
  let current = this.head;
1965
2010
  while (current) {
1966
- if (current.value === value) {
2011
+ if (predicate(current)) {
1967
2012
  count++;
1968
2013
  }
1969
2014
  current = current.next;
@@ -2049,6 +2094,61 @@ var dataStructureTyped = (() => {
2049
2094
  current = current.next;
2050
2095
  }
2051
2096
  }
2097
+ /**
2098
+ * Time Complexity: O(n)
2099
+ * Space Complexity: O(n)
2100
+ *
2101
+ * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
2102
+ * array.
2103
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
2104
+ * @returns The `fromArray` function returns a `SinglyLinkedList` object.
2105
+ */
2106
+ static fromArray(data) {
2107
+ const singlyLinkedList = new _SinglyLinkedList();
2108
+ for (const item of data) {
2109
+ singlyLinkedList.push(item);
2110
+ }
2111
+ return singlyLinkedList;
2112
+ }
2113
+ /**
2114
+ * The _isPredicate function in TypeScript checks if the input is a function that takes a
2115
+ * SinglyLinkedListNode as an argument and returns a boolean.
2116
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2117
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
2118
+ * @returns The _isPredicate method is returning a boolean value based on whether the
2119
+ * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
2120
+ * function, the method will return true, indicating that it is a predicate function. If it is not a
2121
+ * function, the method will return false.
2122
+ */
2123
+ _isPredicate(elementNodeOrPredicate) {
2124
+ return typeof elementNodeOrPredicate === "function";
2125
+ }
2126
+ /**
2127
+ * The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
2128
+ * node if necessary.
2129
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
2130
+ * an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
2131
+ * @returns A SinglyLinkedListNode<E> object is being returned.
2132
+ */
2133
+ _ensureNode(elementOrNode) {
2134
+ if (this.isNode(elementOrNode)) return elementOrNode;
2135
+ return new SinglyLinkedListNode(elementOrNode);
2136
+ }
2137
+ /**
2138
+ * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
2139
+ * function, or a value to compare with the node's value.
2140
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2141
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
2142
+ * @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
2143
+ * function is returned that checks if a given node is equal to the input node. If the input is a
2144
+ * predicate function, it is returned as is. If the input is neither a node nor a predicate function,
2145
+ * a function is returned that checks if a given node's value is equal to the input
2146
+ */
2147
+ _ensurePredicate(elementNodeOrPredicate) {
2148
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
2149
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
2150
+ return (node) => node.value === elementNodeOrPredicate;
2151
+ }
2052
2152
  };
2053
2153
 
2054
2154
  // src/data-structures/linked-list/doubly-linked-list.ts
@@ -2177,28 +2277,33 @@ var dataStructureTyped = (() => {
2177
2277
  return (_a = this.tail) == null ? void 0 : _a.value;
2178
2278
  }
2179
2279
  /**
2180
- * Time Complexity: O(n)
2181
- * Space Complexity: O(n)
2280
+ * Time Complexity: O(1)
2281
+ * Space Complexity: O(1)
2182
2282
  *
2183
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
2184
- * given array.
2185
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
2186
- * @returns The `fromArray` function returns a DoublyLinkedList object.
2283
+ * The function `isNode` in TypeScript checks if a given input is an instance of
2284
+ * `DoublyLinkedListNode`.
2285
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2286
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
2287
+ * be one of the following types:
2288
+ * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
2289
+ * instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
2290
+ * parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
2291
+ * the function returns `false`.
2187
2292
  */
2188
- static fromArray(data) {
2189
- return new _DoublyLinkedList(data);
2293
+ isNode(elementNodeOrPredicate) {
2294
+ return elementNodeOrPredicate instanceof DoublyLinkedListNode;
2190
2295
  }
2191
2296
  /**
2192
2297
  * Time Complexity: O(1)
2193
2298
  * Space Complexity: O(1)
2194
2299
  *
2195
- * The push function adds a new element to the end of a doubly linked list.
2196
- * @param {E} element - The "element" parameter represents the value that you want to add to the
2197
- * doubly linked list.
2198
- * @returns The `push` method is returning a boolean value, `true`.
2300
+ * The `push` function adds a new element or node to the end of a doubly linked list.
2301
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
2302
+ * method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
2303
+ * @returns The `push` method is returning a boolean value, specifically `true`.
2199
2304
  */
2200
- push(element) {
2201
- const newNode = new DoublyLinkedListNode(element);
2305
+ push(elementOrNode) {
2306
+ const newNode = this._ensureNode(elementOrNode);
2202
2307
  if (!this.head) {
2203
2308
  this._head = newNode;
2204
2309
  this._tail = newNode;
@@ -2254,13 +2359,14 @@ var dataStructureTyped = (() => {
2254
2359
  * Time Complexity: O(1)
2255
2360
  * Space Complexity: O(1)
2256
2361
  *
2257
- * The unshift function adds a new element to the beginning of a doubly linked list.
2258
- * @param {E} element - The "element" parameter represents the value of the element that you want to
2259
- * add to the beginning of the doubly linked list.
2260
- * @returns The `unshift` method is returning a boolean value, `true`.
2362
+ * The unshift function adds a new element or node to the beginning of a doubly linked list.
2363
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
2364
+ * `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
2365
+ * element of type `E`.
2366
+ * @returns The `unshift` method is returning a boolean value, specifically `true`.
2261
2367
  */
2262
- unshift(element) {
2263
- const newNode = new DoublyLinkedListNode(element);
2368
+ unshift(elementOrNode) {
2369
+ const newNode = this._ensureNode(elementOrNode);
2264
2370
  if (!this.head) {
2265
2371
  this._head = newNode;
2266
2372
  this._tail = newNode;
@@ -2313,16 +2419,26 @@ var dataStructureTyped = (() => {
2313
2419
  * Time Complexity: O(n)
2314
2420
  * Space Complexity: O(1)
2315
2421
  *
2316
- * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
2317
- * node if found, otherwise it returns undefined.
2318
- * @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
2319
- * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
2320
- * is found in the linked list. If no such node is found, it returns `undefined`.
2422
+ * This TypeScript function searches for a node in a doubly linked list based on a given element node
2423
+ * or predicate.
2424
+ * @param {| E
2425
+ * | DoublyLinkedListNode<E>
2426
+ * | ((node: DoublyLinkedListNode<E>) => boolean)
2427
+ * | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
2428
+ * node in a doubly linked list based on a given element, node, or predicate function. The
2429
+ * `elementNodeOrPredicate` parameter can be one of the following:
2430
+ * @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
2431
+ * input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
2432
+ * Otherwise, it iterates through the linked list starting from the head node and applies the
2433
+ * provided predicate function to each node. If a node satisfies the predicate, that node is
2434
+ * returned. If
2321
2435
  */
2322
- getNode(value) {
2436
+ getNode(elementNodeOrPredicate) {
2437
+ if (elementNodeOrPredicate === void 0) return;
2438
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2323
2439
  let current = this.head;
2324
2440
  while (current) {
2325
- if (current.value === value) {
2441
+ if (predicate(current)) {
2326
2442
  return current;
2327
2443
  }
2328
2444
  current = current.next;
@@ -2333,25 +2449,27 @@ var dataStructureTyped = (() => {
2333
2449
  * Time Complexity: O(n)
2334
2450
  * Space Complexity: O(1)
2335
2451
  *
2336
- * The `insert` function inserts a value at a specified index in a doubly linked list.
2337
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
2338
- * DoublyLinkedList. It is of type number.
2339
- * @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
2340
- * specified index.
2341
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
2342
- * if the index is out of bounds.
2452
+ * The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
2453
+ * @param {number} index - The `index` parameter in the `addAt` method represents the position at
2454
+ * which you want to add a new element or node in the doubly linked list. It indicates the location
2455
+ * where the new element or node should be inserted.
2456
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
2457
+ * `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
2458
+ * @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
2459
+ * successfully added at the specified index, and `false` if the index is out of bounds (less than 0
2460
+ * or greater than the size of the list).
2343
2461
  */
2344
- addAt(index, value) {
2462
+ addAt(index, newElementOrNode) {
2345
2463
  if (index < 0 || index > this._size) return false;
2346
2464
  if (index === 0) {
2347
- this.unshift(value);
2465
+ this.unshift(newElementOrNode);
2348
2466
  return true;
2349
2467
  }
2350
2468
  if (index === this._size) {
2351
- this.push(value);
2469
+ this.push(newElementOrNode);
2352
2470
  return true;
2353
2471
  }
2354
- const newNode = new DoublyLinkedListNode(value);
2472
+ const newNode = this._ensureNode(newElementOrNode);
2355
2473
  const prevNode = this.getNodeAt(index - 1);
2356
2474
  const nextNode = prevNode.next;
2357
2475
  newNode.prev = prevNode;
@@ -2365,24 +2483,21 @@ var dataStructureTyped = (() => {
2365
2483
  * Time Complexity: O(1) or O(n)
2366
2484
  * Space Complexity: O(1)
2367
2485
  *
2368
- * The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
2369
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
2370
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
2371
- * itself.
2372
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
2373
- * list.
2374
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
2375
- * insertion fails.
2486
+ * The `addBefore` function in TypeScript adds a new element or node before an existing element or
2487
+ * node in a doubly linked list.
2488
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
2489
+ * in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
2490
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
2491
+ * represents the element or node that you want to add before the `existingElementOrNode` in a doubly
2492
+ * linked list.
2493
+ * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
2494
+ * successfully added before the existing element or node, and `false` if the existing element or
2495
+ * node was not found.
2376
2496
  */
2377
- addBefore(existingValueOrNode, newValue) {
2378
- let existingNode;
2379
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
2380
- existingNode = existingValueOrNode;
2381
- } else {
2382
- existingNode = this.getNode(existingValueOrNode);
2383
- }
2497
+ addBefore(existingElementOrNode, newElementOrNode) {
2498
+ const existingNode = this.getNode(existingElementOrNode);
2384
2499
  if (existingNode) {
2385
- const newNode = new DoublyLinkedListNode(newValue);
2500
+ const newNode = this._ensureNode(newElementOrNode);
2386
2501
  newNode.prev = existingNode.prev;
2387
2502
  if (existingNode.prev) {
2388
2503
  existingNode.prev.next = newNode;
@@ -2401,23 +2516,22 @@ var dataStructureTyped = (() => {
2401
2516
  * Time Complexity: O(1) or O(n)
2402
2517
  * Space Complexity: O(1)
2403
2518
  *
2404
- * The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
2405
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
2406
- * after which the new value will be inserted. It can be either the value of the existing node or the existing node
2407
- * itself.
2408
- * @param {E} newValue - The value that you want to insert into the doubly linked list.
2409
- * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
2410
- * existing value or node is not found in the doubly linked list.
2411
- */
2412
- addAfter(existingValueOrNode, newValue) {
2413
- let existingNode;
2414
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
2415
- existingNode = existingValueOrNode;
2416
- } else {
2417
- existingNode = this.getNode(existingValueOrNode);
2418
- }
2519
+ * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
2520
+ * in a doubly linked list.
2521
+ * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
2522
+ * element or node in the doubly linked list after which you want to add a new element or node.
2523
+ * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
2524
+ * `addAfter` method represents the element or node that you want to add after the existing element
2525
+ * or node in a doubly linked list. This parameter can be either an element value or a
2526
+ * `DoublyLinkedListNode` object that you want to insert
2527
+ * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
2528
+ * successfully added after the existing element or node, and `false` if the existing element or node
2529
+ * was not found in the linked list.
2530
+ */
2531
+ addAfter(existingElementOrNode, newElementOrNode) {
2532
+ const existingNode = this.getNode(existingElementOrNode);
2419
2533
  if (existingNode) {
2420
- const newNode = new DoublyLinkedListNode(newValue);
2534
+ const newNode = this._ensureNode(newElementOrNode);
2421
2535
  newNode.next = existingNode.next;
2422
2536
  if (existingNode.next) {
2423
2537
  existingNode.next.prev = newNode;
@@ -2464,19 +2578,17 @@ var dataStructureTyped = (() => {
2464
2578
  * Time Complexity: O(1) or O(n)
2465
2579
  * Space Complexity: O(1)
2466
2580
  *
2467
- * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
2468
- * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
2469
- * a `DoublyLinkedListNode<E>` object.
2470
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
2471
- * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
2581
+ * The `delete` function removes a specified element or node from a doubly linked list if it exists.
2582
+ * @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
2583
+ * the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
2584
+ * can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
2585
+ * doubly linked list
2586
+ * @returns The `delete` method returns a boolean value - `true` if the element or node was
2587
+ * successfully deleted from the doubly linked list, and `false` if the element or node was not found
2588
+ * in the list.
2472
2589
  */
2473
- delete(valOrNode) {
2474
- let node;
2475
- if (valOrNode instanceof DoublyLinkedListNode) {
2476
- node = valOrNode;
2477
- } else {
2478
- node = this.getNode(valOrNode);
2479
- }
2590
+ delete(elementOrNode) {
2591
+ const node = this.getNode(elementOrNode);
2480
2592
  if (node) {
2481
2593
  if (node === this.head) {
2482
2594
  this.shift();
@@ -2518,17 +2630,19 @@ var dataStructureTyped = (() => {
2518
2630
  * Time Complexity: O(n)
2519
2631
  * Space Complexity: O(1)
2520
2632
  *
2521
- * The function returns the index of the first occurrence of a given value in a linked list.
2522
- * @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
2523
- * that we are searching for in the linked list.
2524
- * @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
2525
- * list. If the value is not found, it returns -1.
2633
+ * This function finds the index of a specified element, node, or predicate in a doubly linked list.
2634
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2635
+ * elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
2636
+ * can be one of the following:
2637
+ * @returns The `indexOf` method returns the index of the element in the doubly linked list that
2638
+ * matches the provided element, node, or predicate. If no match is found, it returns -1.
2526
2639
  */
2527
- indexOf(value) {
2640
+ indexOf(elementNodeOrPredicate) {
2641
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2528
2642
  let index = 0;
2529
2643
  let current = this.head;
2530
2644
  while (current) {
2531
- if (current.value === value) {
2645
+ if (predicate(current)) {
2532
2646
  return index;
2533
2647
  }
2534
2648
  index++;
@@ -2540,19 +2654,41 @@ var dataStructureTyped = (() => {
2540
2654
  * Time Complexity: O(n)
2541
2655
  * Space Complexity: O(1)
2542
2656
  *
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`.
2657
+ * This function retrieves an element from a doubly linked list based on a given element
2658
+ * node or predicate.
2659
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2660
+ * elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
2661
+ * which can be one of the following types:
2662
+ * @returns The `get` method returns the value of the first node in the doubly linked list that
2663
+ * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
2549
2664
  */
2550
- findBackward(callback) {
2665
+ get(elementNodeOrPredicate) {
2666
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2667
+ let current = this.head;
2668
+ while (current) {
2669
+ if (predicate(current)) return current.value;
2670
+ current = current.next;
2671
+ }
2672
+ return void 0;
2673
+ }
2674
+ /**
2675
+ * Time Complexity: O(n)
2676
+ * Space Complexity: O(1)
2677
+ *
2678
+ * The `getBackward` function searches for a specific element in a doubly linked list starting from
2679
+ * the tail and moving backwards.
2680
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2681
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
2682
+ * function can be one of the following types:
2683
+ * @returns The `getBackward` method returns the value of the element node that matches the provided
2684
+ * predicate when traversing the doubly linked list backwards. If no matching element is found, it
2685
+ * returns `undefined`.
2686
+ */
2687
+ getBackward(elementNodeOrPredicate) {
2688
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2551
2689
  let current = this.tail;
2552
2690
  while (current) {
2553
- if (callback(current.value)) {
2554
- return current.value;
2555
- }
2691
+ if (predicate(current)) return current.value;
2556
2692
  current = current.prev;
2557
2693
  }
2558
2694
  return void 0;
@@ -2674,6 +2810,35 @@ var dataStructureTyped = (() => {
2674
2810
  }
2675
2811
  return mappedList;
2676
2812
  }
2813
+ /**
2814
+ * Time Complexity: O(n)
2815
+ * Space Complexity: O(1)
2816
+ *
2817
+ */
2818
+ countOccurrences(elementOrNode) {
2819
+ const predicate = this._ensurePredicate(elementOrNode);
2820
+ let count = 0;
2821
+ let current = this.head;
2822
+ while (current) {
2823
+ if (predicate(current)) {
2824
+ count++;
2825
+ }
2826
+ current = current.next;
2827
+ }
2828
+ return count;
2829
+ }
2830
+ /**
2831
+ * Time Complexity: O(n)
2832
+ * Space Complexity: O(n)
2833
+ *
2834
+ * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
2835
+ * given array.
2836
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
2837
+ * @returns The `fromArray` function returns a DoublyLinkedList object.
2838
+ */
2839
+ static fromArray(data) {
2840
+ return new _DoublyLinkedList(data);
2841
+ }
2677
2842
  /**
2678
2843
  * The function returns an iterator that iterates over the values of a linked list.
2679
2844
  */
@@ -2684,6 +2849,45 @@ var dataStructureTyped = (() => {
2684
2849
  current = current.next;
2685
2850
  }
2686
2851
  }
2852
+ /**
2853
+ * The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
2854
+ * as an argument and returns a boolean.
2855
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2856
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
2857
+ * types:
2858
+ * @returns The _isPredicate method is returning a boolean value indicating whether the
2859
+ * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
2860
+ * function, the method will return true, indicating that it is a predicate function.
2861
+ */
2862
+ _isPredicate(elementNodeOrPredicate) {
2863
+ return typeof elementNodeOrPredicate === "function";
2864
+ }
2865
+ /**
2866
+ * The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
2867
+ * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
2868
+ * an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
2869
+ * @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
2870
+ * as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
2871
+ * value and returned.
2872
+ */
2873
+ _ensureNode(elementOrNode) {
2874
+ if (this.isNode(elementOrNode)) return elementOrNode;
2875
+ return new DoublyLinkedListNode(elementOrNode);
2876
+ }
2877
+ /**
2878
+ * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
2879
+ * function, or a value to compare with the node's value.
2880
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2881
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
2882
+ * types:
2883
+ * @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
2884
+ * returns a boolean value based on the conditions specified in the code.
2885
+ */
2886
+ _ensurePredicate(elementNodeOrPredicate) {
2887
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
2888
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
2889
+ return (node) => node.value === elementNodeOrPredicate;
2890
+ }
2687
2891
  };
2688
2892
 
2689
2893
  // src/data-structures/linked-list/skip-linked-list.ts