mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
Integration test for SinglyLinkedList and DoublyLinkedList completed
This commit is contained in:
parent
b9dba24bf3
commit
dcec645f05
|
@ -5,8 +5,13 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
export class DoublyLinkedListNode<T> {
|
||||
export class DoublyLinkedListNode<T = number> {
|
||||
|
||||
/**
|
||||
* The constructor function initializes the value, next, and previous properties of an object.
|
||||
* @param {T} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
|
||||
* is defined as a generic type "T".
|
||||
*/
|
||||
constructor(val: T) {
|
||||
this._val = val;
|
||||
this._next = null;
|
||||
|
@ -46,6 +51,9 @@ export class DoublyLinkedListNode<T> {
|
|||
|
||||
export class DoublyLinkedList<T> {
|
||||
|
||||
/**
|
||||
* The constructor initializes the linked list with an empty head, tail, and length.
|
||||
*/
|
||||
constructor() {
|
||||
this._head = null;
|
||||
this._tail = null;
|
||||
|
@ -82,10 +90,28 @@ export class DoublyLinkedList<T> {
|
|||
this._length = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
||||
* given array.
|
||||
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
||||
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
||||
*/
|
||||
static fromArray<T>(data: T[]) {
|
||||
const doublyLinkedList = new DoublyLinkedList<T>();
|
||||
for (const item of data) {
|
||||
doublyLinkedList.push(item);
|
||||
}
|
||||
return doublyLinkedList;
|
||||
}
|
||||
|
||||
getLength(): number {
|
||||
return this._length;
|
||||
}
|
||||
|
||||
/**
|
||||
* The push function adds a new node with the given value to the end of the doubly linked list.
|
||||
* @param {T} val - The value to be added to the linked list.
|
||||
*/
|
||||
push(val: T): void {
|
||||
const newNode = new DoublyLinkedListNode(val);
|
||||
if (!this.head) {
|
||||
|
@ -99,6 +125,11 @@ export class DoublyLinkedList<T> {
|
|||
this.length++;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `pop()` 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.
|
||||
*/
|
||||
pop(): T | null {
|
||||
if (!this.tail) return null;
|
||||
const removedNode = this.tail;
|
||||
|
@ -113,6 +144,11 @@ export class DoublyLinkedList<T> {
|
|||
return removedNode.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `shift()` 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.
|
||||
*/
|
||||
shift(): T | null {
|
||||
if (!this.head) return null;
|
||||
const removedNode = this.head;
|
||||
|
@ -127,6 +163,11 @@ export class DoublyLinkedList<T> {
|
|||
return removedNode.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
||||
* @param {T} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
||||
* doubly linked list.
|
||||
*/
|
||||
unshift(val: T): void {
|
||||
const newNode = new DoublyLinkedListNode(val);
|
||||
if (!this.head) {
|
||||
|
@ -140,7 +181,14 @@ export class DoublyLinkedList<T> {
|
|||
this.length++;
|
||||
}
|
||||
|
||||
get(index: number): T | null {
|
||||
/**
|
||||
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
||||
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
||||
* retrieve from the list.
|
||||
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
||||
* or the linked list is empty, it will return null.
|
||||
*/
|
||||
getAt(index: number): T | null {
|
||||
if (index < 0 || index >= this.length) return null;
|
||||
let current = this.head;
|
||||
for (let i = 0; i < index; i++) {
|
||||
|
@ -149,6 +197,14 @@ export class DoublyLinkedList<T> {
|
|||
return current!.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function `getNodeAt` returns the node at a given index in a doubly 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 node we want to
|
||||
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
||||
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<T>` object if the index is within the
|
||||
* valid range of the linked list, otherwise it returns `null`.
|
||||
*/
|
||||
getNodeAt(index: number): DoublyLinkedListNode<T> | null {
|
||||
if (index < 0 || index >= this.length) return null;
|
||||
let current = this.head;
|
||||
|
@ -158,7 +214,14 @@ export class DoublyLinkedList<T> {
|
|||
return current;
|
||||
}
|
||||
|
||||
findNodeByValue(val: T): DoublyLinkedListNode<T> | null {
|
||||
/**
|
||||
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
||||
* node if found, otherwise it returns null.
|
||||
* @param {T} val - The `val` parameter is the value that we want to search for in the doubly linked list.
|
||||
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<T>` 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: T): DoublyLinkedListNode<T> | null {
|
||||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
|
@ -171,6 +234,15 @@ export class DoublyLinkedList<T> {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
||||
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
||||
* DoublyLinkedList. It is of type number.
|
||||
* @param {T} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
|
||||
* specified index.
|
||||
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
||||
* if the index is out of bounds.
|
||||
*/
|
||||
insert(index: number, val: T): boolean {
|
||||
if (index < 0 || index > this.length) return false;
|
||||
if (index === 0) {
|
||||
|
@ -193,6 +265,13 @@ export class DoublyLinkedList<T> {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* data structure. It is of type number.
|
||||
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
||||
* bounds.
|
||||
*/
|
||||
deleteAt(index: number): T | null {
|
||||
if (index < 0 || index >= this.length) return null;
|
||||
if (index === 0) return this.shift();
|
||||
|
@ -207,6 +286,12 @@ export class DoublyLinkedList<T> {
|
|||
return removedNode!.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `delete` function removes a node with a specific value from a doubly linked list.
|
||||
* @param {T} val - The `val` parameter represents the value that you want to delete from the linked list.
|
||||
* @returns The `delete` method returns a boolean value. It returns `true` if the value `val` is found and deleted from
|
||||
* the linked list, and `false` if the value is not found in the linked list.
|
||||
*/
|
||||
delete(val: T): boolean {
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
|
@ -229,6 +314,10 @@ export class DoublyLinkedList<T> {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `toArray` function converts a linked list into an array.
|
||||
* @returns The `toArray()` method is returning an array of type `T[]`.
|
||||
*/
|
||||
toArray(): T[] {
|
||||
const array: T[] = [];
|
||||
let current = this.head;
|
||||
|
@ -239,12 +328,22 @@ export class DoublyLinkedList<T> {
|
|||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
||||
*/
|
||||
clear(): void {
|
||||
this._head = null;
|
||||
this._tail = null;
|
||||
this._length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
||||
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
||||
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
||||
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
||||
* the callback function. If no element satisfies the condition, it returns `null`.
|
||||
*/
|
||||
find(callback: (val: T) => boolean): T | null {
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
|
@ -256,6 +355,13 @@ export class DoublyLinkedList<T> {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function returns the index of the first occurrence of a given value in a linked list.
|
||||
* @param {T} val - The parameter `val` is of type `T`, which means it can be any data type. It represents the value
|
||||
* that we are searching for in the linked list.
|
||||
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
|
||||
* list. If the value is not found, it returns -1.
|
||||
*/
|
||||
indexOf(val: T): number {
|
||||
let index = 0;
|
||||
let current = this.head;
|
||||
|
@ -269,6 +375,14 @@ export class DoublyLinkedList<T> {
|
|||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `findLast` 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 T 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
|
||||
* the callback function. If no value satisfies the condition, it returns `null`.
|
||||
*/
|
||||
findLast(callback: (val: T) => boolean): T | null {
|
||||
let current = this.tail;
|
||||
while (current) {
|
||||
|
@ -280,6 +394,10 @@ export class DoublyLinkedList<T> {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
|
||||
* @returns The `toArrayReverse()` function returns an array of type `T[]`.
|
||||
*/
|
||||
toArrayReverse(): T[] {
|
||||
const array: T[] = [];
|
||||
let current = this.tail;
|
||||
|
@ -290,6 +408,9 @@ export class DoublyLinkedList<T> {
|
|||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
||||
*/
|
||||
reverse(): void {
|
||||
let current = this.head;
|
||||
[this.head, this.tail] = [this.tail, this.head];
|
||||
|
@ -300,6 +421,12 @@ export class DoublyLinkedList<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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: T, index: number) => void): void {
|
||||
let current = this.head;
|
||||
let index = 0;
|
||||
|
@ -310,6 +437,14 @@ export class DoublyLinkedList<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
||||
* DoublyLinkedList with the transformed values.
|
||||
* @param callback - The callback parameter is a function that takes a value of type T (the type of values stored in
|
||||
* the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
||||
* DoublyLinkedList).
|
||||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
||||
*/
|
||||
map<U>(callback: (val: T) => U): DoublyLinkedList<U> {
|
||||
const mappedList = new DoublyLinkedList<U>();
|
||||
let current = this.head;
|
||||
|
@ -320,6 +455,13 @@ export class DoublyLinkedList<T> {
|
|||
return mappedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
||||
* elements that satisfy the given callback function.
|
||||
* @param callback - The `callback` parameter is a function that takes a value of type `T` 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 DoublyLinkedList class.
|
||||
*/
|
||||
filter(callback: (val: T) => boolean): DoublyLinkedList<T> {
|
||||
const filteredList = new DoublyLinkedList<T>();
|
||||
let current = this.head;
|
||||
|
@ -332,6 +474,16 @@ export class DoublyLinkedList<T> {
|
|||
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: T) => U, initialValue: U): U {
|
||||
let accumulator = initialValue;
|
||||
let current = this.head;
|
||||
|
@ -342,8 +494,16 @@ export class DoublyLinkedList<T> {
|
|||
return accumulator;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function inserts a new value after an existing value in a doubly linked list.
|
||||
* @param {T} existingValue - The existing value is the value of the node after which we want to insert the new value.
|
||||
* @param {T} newValue - The `newValue` parameter represents the value of the new node that you want to insert after
|
||||
* the existing node.
|
||||
* @returns The method is returning a boolean value. It returns true if the insertion is successful and false if the
|
||||
* existing value is not found in the linked list.
|
||||
*/
|
||||
insertAfter(existingValue: T, newValue: T): boolean {
|
||||
const existingNode = this.findNodeByValue(existingValue);
|
||||
const existingNode = this.findNode(existingValue);
|
||||
|
||||
if (existingNode) {
|
||||
const newNode = new DoublyLinkedListNode(newValue);
|
||||
|
@ -363,8 +523,16 @@ export class DoublyLinkedList<T> {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `insertBefore` function inserts a new value before an existing value in a doubly linked list.
|
||||
* @param {T} existingValue - The existing value is the value of the node that you want to insert the new value before.
|
||||
* @param {T} newValue - The `newValue` parameter represents the value of the new node that you want to insert before
|
||||
* the existing node.
|
||||
* @returns The method is returning a boolean value. It returns true if the insertion is successful and false if the
|
||||
* existing value is not found in the linked list.
|
||||
*/
|
||||
insertBefore(existingValue: T, newValue: T): boolean {
|
||||
const existingNode = this.findNodeByValue(existingValue);
|
||||
const existingNode = this.findNode(existingValue);
|
||||
|
||||
if (existingNode) {
|
||||
const newNode = new DoublyLinkedListNode(newValue);
|
||||
|
|
|
@ -5,8 +5,13 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
export class SinglyLinkedListNode<T> {
|
||||
export class SinglyLinkedListNode<T = number> {
|
||||
|
||||
/**
|
||||
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
||||
* @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
|
||||
* will be stored in the node of a linked list.
|
||||
*/
|
||||
constructor(val: T) {
|
||||
this._val = val;
|
||||
this._next = null;
|
||||
|
@ -35,6 +40,9 @@ export class SinglyLinkedListNode<T> {
|
|||
|
||||
export class SinglyLinkedList<T> {
|
||||
|
||||
/**
|
||||
* The constructor initializes the linked list with an empty head, tail, and length.
|
||||
*/
|
||||
constructor() {
|
||||
this._head = null;
|
||||
this._tail = null;
|
||||
|
@ -71,10 +79,29 @@ export class SinglyLinkedList<T> {
|
|||
this._length = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
||||
* array.
|
||||
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
||||
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
||||
*/
|
||||
static fromArray<T>(data: T[]) {
|
||||
const singlyLinkedList = new SinglyLinkedList<T>();
|
||||
for (const item of data) {
|
||||
singlyLinkedList.push(item);
|
||||
}
|
||||
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 {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
|
||||
* any type (T) as specified in the generic type declaration of the class or function.
|
||||
*/
|
||||
push(data: T): void {
|
||||
const newNode = new SinglyLinkedListNode(data);
|
||||
if (!this.head) {
|
||||
|
@ -87,6 +114,12 @@ export class SinglyLinkedList<T> {
|
|||
this.length++;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `pop()` 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`.
|
||||
*/
|
||||
pop(): T | null {
|
||||
if (!this.head) return null;
|
||||
if (this.head === this.tail) {
|
||||
|
@ -108,6 +141,10 @@ export class SinglyLinkedList<T> {
|
|||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
shift(): T | null {
|
||||
if (!this.head) return null;
|
||||
const removedNode = this.head;
|
||||
|
@ -116,6 +153,11 @@ export class SinglyLinkedList<T> {
|
|||
return removedNode.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
||||
* @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
||||
* linked list.
|
||||
*/
|
||||
unshift(val: T): void {
|
||||
const newNode = new SinglyLinkedListNode(val);
|
||||
if (!this.head) {
|
||||
|
@ -128,7 +170,14 @@ export class SinglyLinkedList<T> {
|
|||
this.length++;
|
||||
}
|
||||
|
||||
get(index: number): T | null {
|
||||
/**
|
||||
* 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
|
||||
* retrieve from the list.
|
||||
* @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
|
||||
* `null` if the index is out of bounds.
|
||||
*/
|
||||
getAt(index: number): T | null {
|
||||
if (index < 0 || index >= this.length) return null;
|
||||
let current = this.head;
|
||||
for (let i = 0; i < index; i++) {
|
||||
|
@ -137,18 +186,48 @@ export class SinglyLinkedList<T> {
|
|||
return current!.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
||||
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
||||
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
||||
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
|
||||
* specified index exists, or `null` if the index is out of bounds.
|
||||
*/
|
||||
getNodeAt(index: number): SinglyLinkedListNode<T> | null {
|
||||
let current = this.head;
|
||||
for (let i = 0; i < index; i++) {
|
||||
current = current!.next;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* data structure. It is of type number.
|
||||
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
||||
* bounds.
|
||||
*/
|
||||
deleteAt(index: number): T | null {
|
||||
if (index < 0 || index >= this.length) return null;
|
||||
if (index === 0) return this.shift();
|
||||
if (index === this.length - 1) return this.pop();
|
||||
|
||||
let prevNode = this.getNodeAt(index - 1);
|
||||
const prevNode = this.getNodeAt(index - 1);
|
||||
const removedNode = prevNode!.next;
|
||||
prevNode!.next = removedNode!.next;
|
||||
this.length--;
|
||||
return removedNode!.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `delete` function removes a specified value from a linked list and returns true if the value was found and
|
||||
* removed, otherwise it returns false.
|
||||
* @param {T} value - The value parameter represents the value of the node that needs to be deleted from the linked
|
||||
* list.
|
||||
* @returns The `delete` method returns a boolean value. It returns `true` if the value was successfully deleted from
|
||||
* the linked list, and `false` if the value was not found in the linked list.
|
||||
*/
|
||||
delete(value: T): boolean {
|
||||
let current = this.head;
|
||||
let prev = null;
|
||||
|
@ -176,14 +255,15 @@ export class SinglyLinkedList<T> {
|
|||
return false;
|
||||
}
|
||||
|
||||
getNodeAt(index: number): SinglyLinkedListNode<T> | null {
|
||||
let current = this.head;
|
||||
for (let i = 0; i < index; i++) {
|
||||
current = current!.next;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `insert` function inserts a value at a specified index in a singly linked list.
|
||||
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
||||
* linked list. It is of type number.
|
||||
* @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
|
||||
* specified index.
|
||||
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
||||
* if the index is out of bounds.
|
||||
*/
|
||||
insert(index: number, val: T): boolean {
|
||||
if (index < 0 || index > this.length) return false;
|
||||
if (index === 0) {
|
||||
|
@ -203,16 +283,28 @@ export class SinglyLinkedList<T> {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
||||
* whether it is empty or not.
|
||||
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
||||
*/
|
||||
isEmpty(): boolean {
|
||||
return this.length === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
||||
*/
|
||||
clear(): void {
|
||||
this._head = null;
|
||||
this._tail = null;
|
||||
this._length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `toArray` function converts a linked list into an array.
|
||||
* @returns The `toArray()` method is returning an array of type `T[]`.
|
||||
*/
|
||||
toArray(): T[] {
|
||||
const array: T[] = [];
|
||||
let current = this.head;
|
||||
|
@ -223,6 +315,10 @@ export class SinglyLinkedList<T> {
|
|||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
||||
* @returns The reverse() method does not return anything. It has a return type of void.
|
||||
*/
|
||||
reverse(): void {
|
||||
if (!this.head || this.head === this.tail) return;
|
||||
|
||||
|
@ -240,6 +336,13 @@ export class SinglyLinkedList<T> {
|
|||
[this.head, this.tail] = [this.tail!, this.head!];
|
||||
}
|
||||
|
||||
/**
|
||||
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
||||
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
||||
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
||||
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
||||
* the callback function. If no element satisfies the condition, it returns `null`.
|
||||
*/
|
||||
find(callback: (val: T) => boolean): T | null {
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
|
@ -251,7 +354,12 @@ export class SinglyLinkedList<T> {
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
||||
* @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
|
||||
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
||||
* value is not found, it returns -1.
|
||||
*/
|
||||
indexOf(value: T): number {
|
||||
let index = 0;
|
||||
let current = this.head;
|
||||
|
@ -267,7 +375,14 @@ export class SinglyLinkedList<T> {
|
|||
return -1;
|
||||
}
|
||||
|
||||
findNodeByValue(value: T): SinglyLinkedListNode<T> | null {
|
||||
/**
|
||||
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
||||
* null.
|
||||
* @param {T} value - The value parameter is the value that we want to search for in the linked list.
|
||||
* @returns a `SinglyLinkedListNode<T>` 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: T): SinglyLinkedListNode<T> | null {
|
||||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
|
@ -280,6 +395,15 @@ export class SinglyLinkedList<T> {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
||||
* @param {T} existingValue - The existing value is the value that already exists in the linked list and before which
|
||||
* we want to insert a new value.
|
||||
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
||||
* @returns The `insertBefore` function returns a boolean value. It returns `true` if the `newValue` is successfully
|
||||
* inserted before the first occurrence of `existingValue` in the linked list. It returns `false` if the
|
||||
* `existingValue` is not found in the linked list.
|
||||
*/
|
||||
insertBefore(existingValue: T, newValue: T): boolean {
|
||||
if (!this.head) {
|
||||
return false;
|
||||
|
@ -305,8 +429,16 @@ export class SinglyLinkedList<T> {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function inserts a new value after an existing value in a singly linked list.
|
||||
* @param {T} existingValue - The existing value is the value of the node after which we want to insert the new value.
|
||||
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list
|
||||
* after the node with the `existingValue`.
|
||||
* @returns The method is returning a boolean value. It returns true if the insertion is successful and false if the
|
||||
* existing value is not found in the linked list.
|
||||
*/
|
||||
insertAfter(existingValue: T, newValue: T): boolean {
|
||||
const existingNode = this.findNodeByValue(existingValue);
|
||||
const existingNode = this.findNode(existingValue);
|
||||
|
||||
if (existingNode) {
|
||||
const newNode = new SinglyLinkedListNode(newValue);
|
||||
|
@ -322,6 +454,11 @@ export class SinglyLinkedList<T> {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function counts the number of occurrences of a given value in a linked list.
|
||||
* @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
||||
* @returns The count of occurrences of the given value in the linked list.
|
||||
*/
|
||||
countOccurrences(value: T): number {
|
||||
let count = 0;
|
||||
let current = this.head;
|
||||
|
|
|
@ -41,19 +41,19 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
// Inserting at the beginning
|
||||
list.insert(0, 0);
|
||||
expect(list.length).toBe(4);
|
||||
expect(list.get(0)).toBe(0);
|
||||
expect(list.get(1)).toBe(1);
|
||||
expect(list.getAt(0)).toBe(0);
|
||||
expect(list.getAt(1)).toBe(1);
|
||||
|
||||
// Inserting in the middle
|
||||
list.insert(2, 1.5);
|
||||
expect(list.length).toBe(5);
|
||||
expect(list.get(2)).toBe(1.5);
|
||||
expect(list.get(3)).toBe(2);
|
||||
expect(list.getAt(2)).toBe(1.5);
|
||||
expect(list.getAt(3)).toBe(2);
|
||||
|
||||
// Inserting at the end
|
||||
list.insert(5, 4);
|
||||
expect(list.length).toBe(6);
|
||||
expect(list.get(5)).toBe(4);
|
||||
expect(list.getAt(5)).toBe(4);
|
||||
expect(list.tail!.val).toBe(4);
|
||||
});
|
||||
|
||||
|
@ -326,7 +326,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
||||
expect(insertSuccess).toBe(true);
|
||||
|
||||
const findNode = objectList.findNodeByValue(newObj); // Use newObj instead of obj2
|
||||
const findNode = objectList.findNode(newObj); // Use newObj instead of obj2
|
||||
expect(findNode?.val).toEqual(newObj);
|
||||
|
||||
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
||||
|
|
|
@ -59,13 +59,13 @@ describe('SinglyLinkedList Operation Test', () => {
|
|||
list.push(1);
|
||||
list.push(2);
|
||||
list.push(3);
|
||||
const element = list.get(1);
|
||||
const element = list.getAt(1);
|
||||
expect(element).toBe(2);
|
||||
});
|
||||
|
||||
it('should return undefined for an out-of-bounds index', () => {
|
||||
list.push(1);
|
||||
const element = list.get(1);
|
||||
const element = list.getAt(1);
|
||||
expect(element).toBeNull();
|
||||
});
|
||||
});
|
||||
|
@ -363,7 +363,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|||
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
||||
expect(insertSuccess).toBe(true);
|
||||
|
||||
const findNode = objectList.findNodeByValue(newObj); // Use newObj instead of obj2
|
||||
const findNode = objectList.findNode(newObj); // Use newObj instead of obj2
|
||||
expect(findNode?.val).toEqual(newObj);
|
||||
|
||||
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
||||
|
|
Loading…
Reference in a new issue