[linked-list] Standardize the method names of DoublyLinkedList and SinglyLinkedList.

This commit is contained in:
Revone 2023-10-29 16:43:19 +08:00
parent bd1720973a
commit c492af0461
12 changed files with 291 additions and 186 deletions

View file

@ -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.39.0](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.39.1](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
### Changes

50
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "data-structure-typed",
"version": "1.39.0",
"version": "1.39.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "data-structure-typed",
"version": "1.39.0",
"version": "1.39.1",
"license": "MIT",
"devDependencies": {
"@types/benchmark": "^2.1.3",
@ -15,17 +15,17 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"auto-changelog": "^2.4.0",
"avl-tree-typed": "^1.38.9",
"avl-tree-typed": "^1.39.0",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.38.9",
"bst-typed": "^1.38.9",
"binary-tree-typed": "^1.39.0",
"bst-typed": "^1.39.0",
"dependency-cruiser": "^14.1.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"heap-typed": "^1.38.9",
"heap-typed": "^1.39.0",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.7.0",
"prettier": "^3.0.3",
@ -2728,12 +2728,12 @@
}
},
"node_modules/avl-tree-typed": {
"version": "1.38.9",
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.9.tgz",
"integrity": "sha512-1Qy7d5G8JXa5Z2KKmlZI1WOmJkIyW6l2zRGHCxXItEfIg+BirZm6BK4IOb03OJjA2qg2N5AxgZDKuMDAzbRXNw==",
"version": "1.39.0",
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.39.0.tgz",
"integrity": "sha512-YpYfpO0slt8R5lcy4wrz7inKFAG1NKUZk2FnJ2FEYIdBJaBIUvW0cflx6Cuuy1iRiXbazpTqGQgKC0nERWUiow==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.9"
"data-structure-typed": "^1.39.0"
}
},
"node_modules/babel-jest": {
@ -2927,12 +2927,12 @@
}
},
"node_modules/binary-tree-typed": {
"version": "1.38.9",
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.9.tgz",
"integrity": "sha512-XGp+ADfVMhhpEqLV+Bi27FkJcT1P6ExPpGuhzhKzyfoWmsnjp94tuuvrUDCqPjYoioGwzbG7yUnC2Fbi4CbsAA==",
"version": "1.39.0",
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.39.0.tgz",
"integrity": "sha512-oJzCbrs+l4qacscxFvVB/ielr22KiO8z/bWSx/tJmafpinWmkAuQIBRGqo+frfFi8MKstNerHPEF+4yT8AakUA==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.9"
"data-structure-typed": "^1.39.0"
}
},
"node_modules/brace-expansion": {
@ -3011,12 +3011,12 @@
}
},
"node_modules/bst-typed": {
"version": "1.38.9",
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.9.tgz",
"integrity": "sha512-jnHwqyTjRCDitmzXydhcP1YmRsCfBYxwhDXD8lm1pQPWOXkcAIU7HqTg7WCPrReo1GaWfQJeBhAIsguluEQ6Lw==",
"version": "1.39.0",
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.39.0.tgz",
"integrity": "sha512-AvitIe0j8jPrXyZkQI5zhTYIc4eS5rxvW59/0YHymg54BeMs+zm1WzouPhsh7qp9D+IcTpQmqDY0kjc07Tic3w==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.9"
"data-structure-typed": "^1.39.0"
}
},
"node_modules/buffer-from": {
@ -3413,9 +3413,9 @@
}
},
"node_modules/data-structure-typed": {
"version": "1.38.9",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.38.9.tgz",
"integrity": "sha512-iHOVtsBY7ZVI28I0ikedDFmBeu3rGu1gSnmemg11PT/KIuflNe9zdwlNm+yZ4M56LbQp0aqY09YoNnNX43UvRw==",
"version": "1.39.0",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.39.0.tgz",
"integrity": "sha512-HESmI/a+uE3ELRDVKIBSmfgSMep4ETJco8jeZD2By5lNhMW9HDZE+SGNqlSeBJd3PdJyact9dWlXWwg7S3+ztQ==",
"dev": true
},
"node_modules/debug": {
@ -4771,12 +4771,12 @@
}
},
"node_modules/heap-typed": {
"version": "1.38.9",
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.9.tgz",
"integrity": "sha512-50RxpJOqMDMbpGa3NZny3YO8NArmwVeIipNDHkOvHXH+P93BvbaFz06rOyuJ1cUxDwTPsv34VfJgrz576/BZtg==",
"version": "1.39.0",
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.39.0.tgz",
"integrity": "sha512-n9Qa+Ph6nnqRN6lz52R62CHrpgWixU/39P22WT9Epf+4dj+pDYPKEGekCCidjjxQXgoR56d6XjnZWFgiQYYN4Q==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.9"
"data-structure-typed": "^1.39.0"
}
},
"node_modules/html-escaper": {

View file

@ -61,17 +61,17 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"auto-changelog": "^2.4.0",
"avl-tree-typed": "^1.38.9",
"avl-tree-typed": "^1.39.0",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.38.9",
"bst-typed": "^1.38.9",
"binary-tree-typed": "^1.39.0",
"bst-typed": "^1.39.0",
"dependency-cruiser": "^14.1.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"heap-typed": "^1.38.9",
"heap-typed": "^1.39.0",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.7.0",
"prettier": "^3.0.3",

View file

@ -147,11 +147,11 @@ export class DoublyLinkedList<E = any> {
}
/**
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
* list is empty, it returns null.
*/
pollLast(): E | undefined {
popLast(): E | undefined {
return this.pop();
}
@ -175,11 +175,11 @@ export class DoublyLinkedList<E = any> {
}
/**
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
* list.
*/
pollFirst(): E | undefined {
popFirst(): E | undefined {
return this.shift();
}
@ -211,18 +211,18 @@ export class DoublyLinkedList<E = any> {
}
/**
* The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty.
* @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
*/
peekFirst(): E | undefined {
getFirst(): E | undefined {
return this.head?.val;
}
/**
* The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty.
* @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
*/
peekLast(): E | undefined {
getLast(): E | undefined {
return this.tail?.val;
}
@ -266,7 +266,7 @@ export class DoublyLinkedList<E = any> {
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
* is found in the linked list. If no such node is found, it returns `null`.
*/
findNode(val: E | null): DoublyLinkedListNode<E> | null {
getNode(val: E | null): DoublyLinkedListNode<E> | null {
let current = this.head;
while (current) {
@ -310,6 +310,43 @@ export class DoublyLinkedList<E = any> {
return true;
}
/**
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
* itself.
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
* list.
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
* insertion fails.
*/
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
let existingNode;
if (existingValueOrNode instanceof DoublyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.getNode(existingValueOrNode);
}
if (existingNode) {
const newNode = new DoublyLinkedListNode(newValue);
newNode.prev = existingNode.prev;
if (existingNode.prev) {
existingNode.prev.next = newNode;
}
newNode.next = existingNode;
existingNode.prev = newNode;
if (existingNode === this.head) {
this.head = newNode;
}
this._length++;
return true;
}
return false;
}
/**
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
@ -331,9 +368,6 @@ export class DoublyLinkedList<E = any> {
return removedNode!.val;
}
delete(valOrNode: E): boolean;
delete(valOrNode: DoublyLinkedListNode<E>): boolean;
/**
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
* @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
@ -347,7 +381,7 @@ export class DoublyLinkedList<E = any> {
if (valOrNode instanceof DoublyLinkedListNode) {
node = valOrNode;
} else {
node = this.findNode(valOrNode);
node = this.getNode(valOrNode);
}
if (node) {
@ -437,14 +471,14 @@ export class DoublyLinkedList<E = any> {
}
/**
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
* value that satisfies the given callback function, or null if no value satisfies the callback.
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
* function is used to determine whether a given value satisfies a certain condition.
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
* the callback function. If no value satisfies the condition, it returns `null`.
*/
findLast(callback: (val: E) => boolean): E | null {
findBackward(callback: (val: E) => boolean): E | null {
let current = this.tail;
while (current) {
if (callback(current.val)) {
@ -456,10 +490,10 @@ export class DoublyLinkedList<E = any> {
}
/**
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
* @returns The `toArrayReverse()` function returns an array of type `E[]`.
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
*/
toArrayReverse(): E[] {
toArrayBackward(): E[] {
const array: E[] = [];
let current = this.tail;
while (current) {
@ -555,9 +589,6 @@ export class DoublyLinkedList<E = any> {
return accumulator;
}
insertAfter(existingValueOrNode: E, newValue: E): boolean;
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
/**
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
@ -573,7 +604,7 @@ export class DoublyLinkedList<E = any> {
if (existingValueOrNode instanceof DoublyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
existingNode = this.getNode(existingValueOrNode);
}
if (existingNode) {
@ -594,40 +625,12 @@ export class DoublyLinkedList<E = any> {
return false;
}
/**
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
* itself.
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
* list.
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
* insertion fails.
*/
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
let existingNode;
* [Symbol.iterator]() {
let current = this.head;
if (existingValueOrNode instanceof DoublyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
while (current) {
yield current.val;
current = current.next;
}
if (existingNode) {
const newNode = new DoublyLinkedListNode(newValue);
newNode.prev = existingNode.prev;
if (existingNode.prev) {
existingNode.prev.next = newNode;
}
newNode.next = existingNode;
existingNode.prev = newNode;
if (existingNode === this.head) {
this.head = newNode;
}
this._length++;
return true;
}
return false;
}
}

View file

@ -87,17 +87,13 @@ export class SinglyLinkedList<E = any> {
return singlyLinkedList;
}
getLength(): number {
return this._length;
}
/**
* The `push` function adds a new node with the given data to the end of a singly linked list.
* @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
* The `push` function adds a new node with the given val to the end of a singly linked list.
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
* any type (E) as specified in the generic type declaration of the class or function.
*/
push(data: E): void {
const newNode = new SinglyLinkedListNode(data);
push(val: E): void {
const newNode = new SinglyLinkedListNode(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
@ -108,6 +104,15 @@ export class SinglyLinkedList<E = any> {
this._length++;
}
/**
* The `push` function adds a new node with the given val to the end of a singly linked list.
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
* any type (E) as specified in the generic type declaration of the class or function.
*/
addLast(val: E): void {
this.push(val);
}
/**
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
* pointers accordingly.
@ -135,6 +140,16 @@ export class SinglyLinkedList<E = any> {
return val;
}
/**
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
* pointers accordingly.
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
* the linked list is empty, it returns `null`.
*/
popLast(): E | undefined {
return this.pop();
}
/**
* The `shift()` function removes and returns the value of the first node in a linked list.
* @returns The value of the node that is being removed from the beginning of the linked list.
@ -147,6 +162,14 @@ export class SinglyLinkedList<E = any> {
return removedNode.val;
}
/**
* The `popFirst()` function removes and returns the value of the first node in a linked list.
* @returns The value of the node that is being removed from the beginning of the linked list.
*/
popFirst(): E | undefined {
return this.shift();
}
/**
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
@ -164,6 +187,15 @@ export class SinglyLinkedList<E = any> {
this._length++;
}
/**
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
* linked list.
*/
addFirst(val: E): void {
this.unshift(val);
}
/**
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
* @param {number} index - The index parameter is a number that represents the position of the element we want to
@ -382,7 +414,7 @@ export class SinglyLinkedList<E = any> {
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
* the specified value is found, the function returns `null`.
*/
findNode(value: E): SinglyLinkedListNode<E> | null {
getNode(value: E): SinglyLinkedListNode<E> | null {
let current = this.head;
while (current) {
@ -432,9 +464,6 @@ export class SinglyLinkedList<E = any> {
return false;
}
insertAfter(existingValueOrNode: E, newValue: E): boolean;
insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
/**
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
@ -449,7 +478,7 @@ export class SinglyLinkedList<E = any> {
if (existingValueOrNode instanceof SinglyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
existingNode = this.getNode(existingValueOrNode);
}
if (existingNode) {
@ -485,7 +514,80 @@ export class SinglyLinkedList<E = any> {
return count;
}
*[Symbol.iterator]() {
/**
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
* represents the value of the current node in the linked list, and the index argument represents the index of the
* current node in the linked list.
*/
forEach(callback: (val: E, index: number) => void): void {
let current = this.head;
let index = 0;
while (current) {
callback(current.val, index);
current = current.next;
index++;
}
}
/**
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
* SinglyLinkedList with the transformed values.
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
* SinglyLinkedList).
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
*/
map<U>(callback: (val: E) => U): SinglyLinkedList<U> {
const mappedList = new SinglyLinkedList<U>();
let current = this.head;
while (current) {
mappedList.push(callback(current.val));
current = current.next;
}
return mappedList;
}
/**
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
* elements that satisfy the given callback function.
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
* It is used to determine whether a value should be included in the filtered list or not.
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
*/
filter(callback: (val: E) => boolean): SinglyLinkedList<E> {
const filteredList = new SinglyLinkedList<E>();
let current = this.head;
while (current) {
if (callback(current.val)) {
filteredList.push(current.val);
}
current = current.next;
}
return filteredList;
}
/**
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
* single value.
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
* used to perform a specific operation on each element of the linked list.
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
* point for the reduction operation.
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
* elements in the linked list.
*/
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U {
let accumulator = initialValue;
let current = this.head;
while (current) {
accumulator = callback(accumulator, current.val);
current = current.next;
}
return accumulator;
}
* [Symbol.iterator]() {
let current = this.head;
while (current) {

View file

@ -95,12 +95,12 @@ export class ObjectDeque<E = number> {
}
/**
* The function `pollFirst()` removes and returns the first element in a data structure.
* The function `popFirst()` removes and returns the first element in a data structure.
* @returns The value of the first element in the data structure.
*/
pollFirst() {
popFirst() {
if (!this._size) return;
const value = this.peekFirst();
const value = this.getFirst();
delete this._nodes[this._first];
this._first++;
this._size--;
@ -108,20 +108,20 @@ export class ObjectDeque<E = number> {
}
/**
* The `peekFirst` function returns the first element in an array-like data structure if it exists.
* The `getFirst` function returns the first element in an array-like data structure if it exists.
* @returns The element at the first position of the `_nodes` array.
*/
peekFirst() {
getFirst() {
if (this._size) return this._nodes[this._first];
}
/**
* The `pollLast()` function removes and returns the last element in a data structure.
* The `popLast()` function removes and returns the last element in a data structure.
* @returns The value that was removed from the data structure.
*/
pollLast() {
popLast() {
if (!this._size) return;
const value = this.peekLast();
const value = this.getLast();
delete this._nodes[this._last];
this._last--;
this._size--;
@ -130,10 +130,10 @@ export class ObjectDeque<E = number> {
}
/**
* The `peekLast()` function returns the last element in an array-like data structure.
* The `getLast()` function returns the last element in an array-like data structure.
* @returns The last element in the array "_nodes" is being returned.
*/
peekLast() {
getLast() {
if (this._size) return this._nodes[this._last];
}
@ -188,19 +188,19 @@ export class ArrayDeque<E> {
}
/**
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
*/
pollLast(): E | null {
popLast(): E | null {
return this._nodes.pop() ?? null;
}
/**
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
* empty.
*/
pollFirst(): E | null {
popFirst(): E | null {
return this._nodes.shift() ?? null;
}
@ -219,19 +219,19 @@ export class ArrayDeque<E> {
}
/**
* The `peekFirst` function returns the first element of an array or null if the array is empty.
* @returns The function `peekFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
* The `getFirst` function returns the first element of an array or null if the array is empty.
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
* empty, it will return `null`.
*/
peekFirst(): E | null {
getFirst(): E | null {
return this._nodes[0] ?? null;
}
/**
* The `peekLast` function returns the last element of an array or null if the array is empty.
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
* The `getLast` function returns the last element of an array or null if the array is empty.
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
*/
peekLast(): E | null {
getLast(): E | null {
return this._nodes[this._nodes.length - 1] ?? null;
}

View file

@ -123,11 +123,11 @@ export class Queue<E = any> {
}
/**
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
* array is empty, it returns `null`.
*/
peekLast(): E | undefined {
getLast(): E | undefined {
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
}

View file

@ -109,7 +109,7 @@ describe('DoublyLinkedList Operation Test', () => {
list.reverse();
expect(list.toArray()).toEqual([3, 2, 1]);
expect(list.toArrayReverse()).toEqual([1, 2, 3]);
expect(list.toArrayBackward()).toEqual([1, 2, 3]);
});
it('should map elements using a callback function', () => {
@ -189,7 +189,7 @@ describe('DoublyLinkedList Operation Test', () => {
list.push(3);
list.push(4);
const lastEven = list.findLast(val => val % 2 === 0);
const lastEven = list.findBackward(val => val % 2 === 0);
expect(lastEven).toBe(4);
});
@ -211,7 +211,7 @@ describe('DoublyLinkedList Operation Test', () => {
list.push(2);
list.push(3);
const reversedArray = list.toArrayReverse();
const reversedArray = list.toArrayBackward();
expect(reversedArray).toEqual([3, 2, 1]);
});
@ -327,8 +327,8 @@ describe('DoublyLinkedList Operation Test', () => {
const insertSuccess = objectList.insertBefore(obj2, newObj);
expect(insertSuccess).toBe(true);
const findNode = objectList.findNode(newObj); // Use newObj instead of obj2
expect(findNode?.val).toEqual(newObj);
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
expect(getNode?.val).toEqual(newObj);
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
expect(deleted).toBe(true);

View file

@ -11,7 +11,7 @@ describe('LinkedList Performance Test', () => {
for (let i = 0; i < magnitude.SQUARED; i++) {
doublyList.push(i);
if (i === midIndex) {
midNode = doublyList.findNode(i);
midNode = doublyList.getNode(i);
} else if (i > midIndex && midNode) {
doublyList.insertBefore(midNode, i);
}
@ -24,7 +24,7 @@ describe('LinkedList Performance Test', () => {
for (let i = 0; i < magnitude.SQUARED; i++) {
singlyList.push(i);
if (i === midIndex) {
midSinglyNode = singlyList.findNode(i);
midSinglyNode = singlyList.getNode(i);
} else if (i > midIndex && midSinglyNode) {
singlyList.insertBefore(midSinglyNode.val, i);
}

View file

@ -362,8 +362,8 @@ describe('SinglyLinkedList Operation Test', () => {
const insertSuccess = objectList.insertBefore(obj2, newObj);
expect(insertSuccess).toBe(true);
const findNode = objectList.findNode(newObj); // Use newObj instead of obj2
expect(findNode?.val).toEqual(newObj);
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
expect(getNode?.val).toEqual(newObj);
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
expect(deleted).toBe(true);

View file

@ -15,20 +15,20 @@ describe('Deque Tests', () => {
it('should add elements at the beginning and end', () => {
deque.addFirst(1);
deque.addLast(2);
expect(deque.peekFirst()).toBe(1);
expect(deque.peekLast()).toBe(2);
expect(deque.getFirst()).toBe(1);
expect(deque.getLast()).toBe(2);
});
it('should delete elements from the beginning and end', () => {
deque.addFirst(1);
deque.addLast(2);
deque.pollFirst();
deque.pollLast();
deque.popFirst();
deque.popLast();
expect(deque.isEmpty()).toBe(true);
});
it('should handle edge case when removing from an empty deque', () => {
const result = deque.pollFirst();
const result = deque.popFirst();
expect(result).toBeUndefined();
});
@ -40,18 +40,18 @@ describe('Deque Tests', () => {
it('should handle adding and removing elements alternately', () => {
deque.addFirst(1);
expect(deque.pollFirst()).toBe(1);
expect(deque.popFirst()).toBe(1);
deque.addLast(2);
expect(deque.pollLast()).toBe(2);
expect(deque.popLast()).toBe(2);
expect(deque.isEmpty()).toBe(true);
});
it('should handle adding and removing elements in a cyclic manner', () => {
deque.addFirst(1);
deque.addLast(2);
expect(deque.pollFirst()).toBe(1);
expect(deque.popFirst()).toBe(1);
deque.addFirst(3);
expect(deque.pollLast()).toBe(2);
expect(deque.popLast()).toBe(2);
expect(deque.size).toBe(1);
});
// Add more test cases as needed
@ -68,20 +68,20 @@ describe('Deque Tests', () => {
it('should add elements at the beginning and end', () => {
objectDeque.addFirst('one');
objectDeque.addLast('two');
expect(objectDeque.peekFirst()).toBe('one');
expect(objectDeque.peekLast()).toBe('two');
expect(objectDeque.getFirst()).toBe('one');
expect(objectDeque.getLast()).toBe('two');
});
it('should delete elements from the beginning and end', () => {
objectDeque.addFirst('one');
objectDeque.addLast('two');
objectDeque.pollFirst();
objectDeque.pollLast();
objectDeque.popFirst();
objectDeque.popLast();
expect(objectDeque.isEmpty()).toBe(true);
});
it('should handle edge case when removing from an empty deque', () => {
const result = objectDeque.pollFirst();
const result = objectDeque.popFirst();
expect(result).toBeUndefined();
});
@ -105,20 +105,20 @@ describe('Deque Tests', () => {
it('should add elements at the beginning and end', () => {
arrayDeque.addFirst(1);
arrayDeque.addLast(2);
expect(arrayDeque.peekFirst()).toBe(1);
expect(arrayDeque.peekLast()).toBe(2);
expect(arrayDeque.getFirst()).toBe(1);
expect(arrayDeque.getLast()).toBe(2);
});
it('should delete elements from the beginning and end', () => {
arrayDeque.addFirst(1);
arrayDeque.addLast(2);
arrayDeque.pollFirst();
arrayDeque.pollLast();
arrayDeque.popFirst();
arrayDeque.popLast();
expect(arrayDeque.isEmpty()).toBe(true);
});
it('should handle edge case when removing from an empty deque', () => {
const result = arrayDeque.pollFirst();
const result = arrayDeque.popFirst();
expect(result).toBeNull();
});
@ -165,16 +165,16 @@ describe('Deque', () => {
deque.addLast(2);
expect(deque.size).toBe(2);
expect(deque.peekFirst()).toBe(1);
expect(deque.peekLast()).toBe(2);
expect(deque.getFirst()).toBe(1);
expect(deque.getLast()).toBe(2);
});
test('should remove elements from the front and back', () => {
deque.addFirst(1);
deque.addLast(2);
const firstElement = deque.pollFirst();
const lastElement = deque.pollLast();
const firstElement = deque.popFirst();
const lastElement = deque.popLast();
expect(deque.size).toBe(0);
expect(firstElement).toBe(1);
@ -203,7 +203,7 @@ describe('Deque', () => {
deque.addLast(1);
expect(deque.isEmpty()).toBe(false);
deque.pollFirst();
deque.popFirst();
expect(deque.isEmpty()).toBe(true);
});
});
@ -225,16 +225,16 @@ describe('ArrayDeque', () => {
deque.addLast(2);
expect(deque.size).toBe(2);
expect(deque.peekFirst()).toBe(1);
expect(deque.peekLast()).toBe(2);
expect(deque.getFirst()).toBe(1);
expect(deque.getLast()).toBe(2);
});
test('should remove elements from the front and back', () => {
deque.addFirst(1);
deque.addLast(2);
const firstElement = deque.pollFirst();
const lastElement = deque.pollLast();
const firstElement = deque.popFirst();
const lastElement = deque.popLast();
expect(deque.size).toBe(0);
expect(firstElement).toBe(1);
@ -263,7 +263,7 @@ describe('ArrayDeque', () => {
deque.addLast(1);
expect(deque.isEmpty()).toBe(false);
deque.pollFirst();
deque.popFirst();
expect(deque.isEmpty()).toBe(true);
});
@ -319,8 +319,8 @@ describe('ObjectDeque', () => {
deque.addFirst(2);
expect(deque.size).toBe(2);
expect(deque.peekFirst()).toBe(2);
expect(deque.peekLast()).toBe(1);
expect(deque.getFirst()).toBe(2);
expect(deque.getLast()).toBe(1);
});
test('should add elements to the end of the deque', () => {
@ -328,37 +328,37 @@ describe('ObjectDeque', () => {
deque.addLast(2);
expect(deque.size).toBe(2);
expect(deque.peekFirst()).toBe(1);
expect(deque.peekLast()).toBe(2);
expect(deque.getFirst()).toBe(1);
expect(deque.getLast()).toBe(2);
});
test('should remove elements from the front of the deque', () => {
deque.addLast(1);
deque.addLast(2);
const removedElement = deque.pollFirst();
const removedElement = deque.popFirst();
expect(deque.size).toBe(1);
expect(removedElement).toBe(1);
expect(deque.peekFirst()).toBe(2);
expect(deque.getFirst()).toBe(2);
});
test('should remove elements from the end of the deque', () => {
deque.addLast(1);
deque.addLast(2);
const removedElement = deque.pollFirst();
const removedElement = deque.popFirst();
expect(deque.size).toBe(1);
expect(removedElement).toBe(1);
expect(deque.peekLast()).toBe(2);
expect(deque.getLast()).toBe(2);
});
test('should return the element at the front of the deque without removing it', () => {
deque.addFirst(1);
deque.addFirst(2);
expect(deque.peekFirst()).toBe(2);
expect(deque.getFirst()).toBe(2);
expect(deque.size).toBe(2);
});
@ -366,7 +366,7 @@ describe('ObjectDeque', () => {
deque.addLast(1);
deque.addLast(2);
expect(deque.peekLast()).toBe(2);
expect(deque.getLast()).toBe(2);
expect(deque.size).toBe(2);
});
@ -391,9 +391,9 @@ describe('ObjectDeque', () => {
deque.addLast(2);
deque.addLast(3);
expect(deque.peekFirst()).toBe(1);
expect(deque.getFirst()).toBe(1);
expect(deque.get(1)).toBe(2);
expect(deque.peekLast()).toBe(3);
expect(deque.getLast()).toBe(3);
});
test('should insert elements at a specific index', () => {
@ -402,9 +402,9 @@ describe('ObjectDeque', () => {
deque.addLast(3);
expect(deque.size).toBe(3);
expect(deque.peekFirst()).toBe(1);
expect(deque.getFirst()).toBe(1);
expect(deque.get(1)).toBe(2);
expect(deque.get(2)).toBe(3);
expect(deque.peekLast()).toBe(3);
expect(deque.getLast()).toBe(3);
});
});

View file

@ -86,7 +86,7 @@ describe('Queue', () => {
queue.push(2);
expect(queue.size).toBe(2);
expect(queue.peek()).toBe(1);
expect(queue.peekLast()).toBe(2);
expect(queue.getLast()).toBe(2);
});
it('should shift elements from the front of the queue', () => {
@ -96,7 +96,7 @@ describe('Queue', () => {
expect(shifted).toBe(1);
expect(queue.size).toBe(1);
expect(queue.peek()).toBe(2);
expect(queue.peekLast()).toBe(2);
expect(queue.getLast()).toBe(2);
});
it('should handle shifting when queue reaches half size', () => {
@ -117,7 +117,7 @@ describe('Queue', () => {
queue.push(1);
queue.push(2);
expect(queue.peek()).toBe(1);
expect(queue.peekLast()).toBe(2);
expect(queue.getLast()).toBe(2);
});
it('should handle shifting when the queue is empty', () => {
@ -129,7 +129,7 @@ describe('Queue', () => {
it('should handle peeking when the queue is empty', () => {
expect(queue.peek()).toBeUndefined();
expect(queue.peekLast()).toBeUndefined();
expect(queue.getLast()).toBeUndefined();
});
it('should handle clearing the queue', () => {
@ -139,7 +139,7 @@ describe('Queue', () => {
queue.clear();
expect(queue.size).toBe(0);
expect(queue.peek()).toBeUndefined();
expect(queue.peekLast()).toBeUndefined();
expect(queue.getLast()).toBeUndefined();
});
it('should clone the queue', () => {
@ -149,7 +149,7 @@ describe('Queue', () => {
const clonedQueue = queue.clone();
expect(clonedQueue.size).toBe(3);
expect(clonedQueue.peek()).toBe(1);
expect(clonedQueue.peekLast()).toBe(3);
expect(clonedQueue.getLast()).toBe(3);
});
it('should handle creating a queue from an array', () => {
@ -157,7 +157,7 @@ describe('Queue', () => {
const newQueue = Queue.fromArray(elements);
expect(newQueue.size).toBe(5);
expect(newQueue.peek()).toBe(1);
expect(newQueue.peekLast()).toBe(5);
expect(newQueue.getLast()).toBe(5);
});
it('should iterate through the queue', () => {