Skip to content

Commit

Permalink
fix: #114. feat: Enhanced SinglyLinkedList methods to support Node ty…
Browse files Browse the repository at this point in the history
…pe parameters for push, unshift, addAfter, addBefore, getNode, addAt, delete, indexOf; added a new get method.
  • Loading branch information
zrwusa committed Nov 20, 2024
1 parent 09a5d8a commit fde5af1
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 156 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)

## [v1.53.4](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming)
## [v1.53.5](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming)

### Changes

Expand Down
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@
"@typescript-eslint/eslint-plugin": "^8.12.1",
"@typescript-eslint/parser": "^8.12.1",
"auto-changelog": "^2.5.0",
"avl-tree-typed": "^1.53.3",
"avl-tree-typed": "^1.53.4",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.53.3",
"bst-typed": "^1.53.3",
"data-structure-typed": "^1.53.3",
"binary-tree-typed": "^1.53.4",
"bst-typed": "^1.53.4",
"data-structure-typed": "^1.53.4",
"dependency-cruiser": "^16.5.0",
"doctoc": "^2.2.1",
"eslint": "^9.13.0",
Expand All @@ -83,7 +83,7 @@
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-import": "^2.31.0",
"fast-glob": "^3.3.2",
"heap-typed": "^1.53.3",
"heap-typed": "^1.53.4",
"istanbul-badges-readme": "^1.9.0",
"jest": "^29.7.0",
"js-sdsl": "^4.4.2",
Expand Down
56 changes: 30 additions & 26 deletions src/data-structures/linked-list/doubly-linked-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,13 +819,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
existingElementOrNode: E | DoublyLinkedListNode<E>,
newElementOrNode: E | DoublyLinkedListNode<E>
): boolean {
let existingNode;

if (existingElementOrNode instanceof DoublyLinkedListNode) {
existingNode = existingElementOrNode;
} else {
existingNode = this.getNode(existingElementOrNode);
}
const existingNode: DoublyLinkedListNode<E> | undefined = this.getNode(existingElementOrNode);

if (existingNode) {
const newNode = this._ensureNode(newElementOrNode);
Expand Down Expand Up @@ -862,13 +856,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
* was not found in the linked list.
*/
addAfter(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
let existingNode;

if (existingElementOrNode instanceof DoublyLinkedListNode) {
existingNode = existingElementOrNode;
} else {
existingNode = this.getNode(existingElementOrNode);
}
const existingNode: DoublyLinkedListNode<E> | undefined = this.getNode(existingElementOrNode);

if (existingNode) {
const newNode = this._ensureNode(newElementOrNode);
Expand Down Expand Up @@ -978,17 +966,15 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* The indexOf function in TypeScript returns the index of a specified element or node in a Doubly
* Linked List.
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
* `indexOf` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
* element of type `E`.
* @returns The `indexOf` method is returning the index of the element or node in the doubly linked
* list. If the element or node is found in the list, the method returns the index of that element or
* node. If the element or node is not found in the list, the method returns -1.
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)}
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
* can be one of the following:
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
* matches the provided element, node, or predicate. If no match is found, it returns -1.
*/
indexOf(elementOrNode: E | DoublyLinkedListNode<E>): number {
const predicate = this._ensurePredicate(elementOrNode);
indexOf(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number {
const predicate = this._ensurePredicate(elementNodeOrPredicate);
let index = 0;
let current = this.head;
while (current) {
Expand All @@ -1005,8 +991,6 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
* Time Complexity: O(n)
* Space Complexity: O(1)
*
*/
/**
* This function retrieves an element from a doubly linked list based on a given element
* node or predicate.
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
Expand Down Expand Up @@ -1180,6 +1164,26 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
return mappedList;
}

/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
*/
countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number {
const predicate = this._ensurePredicate(elementOrNode);
let count = 0;
let current = this.head;

while (current) {
if (predicate(current)) {
count++;
}
current = current.next;
}

return count;
}

/**
* Time Complexity: O(n)
* Space Complexity: O(n)
Expand Down
Loading

0 comments on commit fde5af1

Please sign in to comment.