data-structure-typed 1.53.5 → 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.
- package/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +13 -11
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +27 -26
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +144 -62
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +201 -97
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +13 -11
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +27 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +144 -62
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +201 -97
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/umd/data-structure-typed.js +216 -113
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/linked-list/doubly-linked-list.ts +30 -26
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
- package/testToExample.ts +0 -215
|
@@ -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}
|
|
1587
|
-
*
|
|
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(
|
|
1591
|
-
const newNode =
|
|
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
|
-
*
|
|
1649
|
-
*
|
|
1650
|
-
*
|
|
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(
|
|
1653
|
-
const newNode =
|
|
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>}
|
|
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(
|
|
1736
|
-
if (
|
|
1758
|
+
delete(elementOrNode) {
|
|
1759
|
+
if (elementOrNode === void 0) return false;
|
|
1737
1760
|
let value;
|
|
1738
|
-
if (
|
|
1739
|
-
value =
|
|
1761
|
+
if (elementOrNode instanceof SinglyLinkedListNode) {
|
|
1762
|
+
value = elementOrNode.value;
|
|
1740
1763
|
} else {
|
|
1741
|
-
value =
|
|
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
|
|
1770
|
-
* @param {number} index - The index parameter represents the position at which
|
|
1771
|
-
* linked list. It is
|
|
1772
|
-
*
|
|
1773
|
-
*
|
|
1774
|
-
*
|
|
1775
|
-
*
|
|
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,
|
|
1803
|
+
addAt(index, newElementOrNode) {
|
|
1778
1804
|
if (index < 0 || index > this._size) return false;
|
|
1779
1805
|
if (index === 0) {
|
|
1780
|
-
this.unshift(
|
|
1806
|
+
this.unshift(newElementOrNode);
|
|
1781
1807
|
return true;
|
|
1782
1808
|
}
|
|
1783
1809
|
if (index === this._size) {
|
|
1784
|
-
this.push(
|
|
1810
|
+
this.push(newElementOrNode);
|
|
1785
1811
|
return true;
|
|
1786
1812
|
}
|
|
1787
|
-
const newNode =
|
|
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
|
|
1852
|
-
*
|
|
1853
|
-
* @
|
|
1854
|
-
*
|
|
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(
|
|
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
|
|
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
|
|
1873
|
-
*
|
|
1874
|
-
* @param {E
|
|
1875
|
-
*
|
|
1876
|
-
*
|
|
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(
|
|
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
|
|
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`
|
|
1893
|
-
*
|
|
1894
|
-
*
|
|
1895
|
-
*
|
|
1896
|
-
* @
|
|
1897
|
-
*
|
|
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(
|
|
1940
|
+
addBefore(existingElementOrNode, newElementOrNode) {
|
|
1900
1941
|
if (!this.head) return false;
|
|
1901
1942
|
let existingValue;
|
|
1902
|
-
if (
|
|
1903
|
-
existingValue =
|
|
1943
|
+
if (this.isNode(existingElementOrNode)) {
|
|
1944
|
+
existingValue = existingElementOrNode.value;
|
|
1904
1945
|
} else {
|
|
1905
|
-
existingValue =
|
|
1946
|
+
existingValue = existingElementOrNode;
|
|
1906
1947
|
}
|
|
1907
1948
|
if (this.head.value === existingValue) {
|
|
1908
|
-
this.unshift(
|
|
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 =
|
|
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
|
|
1929
|
-
*
|
|
1930
|
-
*
|
|
1931
|
-
*
|
|
1932
|
-
* @
|
|
1933
|
-
*
|
|
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(
|
|
1936
|
-
|
|
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 =
|
|
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
|
|
1959
|
-
*
|
|
1960
|
-
* @
|
|
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(
|
|
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
|
|
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
|
|
@@ -2395,12 +2495,7 @@ var dataStructureTyped = (() => {
|
|
|
2395
2495
|
* node was not found.
|
|
2396
2496
|
*/
|
|
2397
2497
|
addBefore(existingElementOrNode, newElementOrNode) {
|
|
2398
|
-
|
|
2399
|
-
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
2400
|
-
existingNode = existingElementOrNode;
|
|
2401
|
-
} else {
|
|
2402
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
2403
|
-
}
|
|
2498
|
+
const existingNode = this.getNode(existingElementOrNode);
|
|
2404
2499
|
if (existingNode) {
|
|
2405
2500
|
const newNode = this._ensureNode(newElementOrNode);
|
|
2406
2501
|
newNode.prev = existingNode.prev;
|
|
@@ -2434,12 +2529,7 @@ var dataStructureTyped = (() => {
|
|
|
2434
2529
|
* was not found in the linked list.
|
|
2435
2530
|
*/
|
|
2436
2531
|
addAfter(existingElementOrNode, newElementOrNode) {
|
|
2437
|
-
|
|
2438
|
-
if (existingElementOrNode instanceof DoublyLinkedListNode) {
|
|
2439
|
-
existingNode = existingElementOrNode;
|
|
2440
|
-
} else {
|
|
2441
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
2442
|
-
}
|
|
2532
|
+
const existingNode = this.getNode(existingElementOrNode);
|
|
2443
2533
|
if (existingNode) {
|
|
2444
2534
|
const newNode = this._ensureNode(newElementOrNode);
|
|
2445
2535
|
newNode.next = existingNode.next;
|
|
@@ -2540,17 +2630,15 @@ var dataStructureTyped = (() => {
|
|
|
2540
2630
|
* Time Complexity: O(n)
|
|
2541
2631
|
* Space Complexity: O(1)
|
|
2542
2632
|
*
|
|
2543
|
-
*
|
|
2544
|
-
*
|
|
2545
|
-
*
|
|
2546
|
-
*
|
|
2547
|
-
*
|
|
2548
|
-
*
|
|
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.
|
|
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.
|
|
2551
2639
|
*/
|
|
2552
|
-
indexOf(
|
|
2553
|
-
const predicate = this._ensurePredicate(
|
|
2640
|
+
indexOf(elementNodeOrPredicate) {
|
|
2641
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2554
2642
|
let index = 0;
|
|
2555
2643
|
let current = this.head;
|
|
2556
2644
|
while (current) {
|
|
@@ -2566,8 +2654,6 @@ var dataStructureTyped = (() => {
|
|
|
2566
2654
|
* Time Complexity: O(n)
|
|
2567
2655
|
* Space Complexity: O(1)
|
|
2568
2656
|
*
|
|
2569
|
-
*/
|
|
2570
|
-
/**
|
|
2571
2657
|
* This function retrieves an element from a doubly linked list based on a given element
|
|
2572
2658
|
* node or predicate.
|
|
2573
2659
|
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
@@ -2724,6 +2810,23 @@ var dataStructureTyped = (() => {
|
|
|
2724
2810
|
}
|
|
2725
2811
|
return mappedList;
|
|
2726
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
|
+
}
|
|
2727
2830
|
/**
|
|
2728
2831
|
* Time Complexity: O(n)
|
|
2729
2832
|
* Space Complexity: O(n)
|