From fba34e6ea7d28eb59f1d0cc93634020d3d3e5db1 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 12:59:11 +0530 Subject: [PATCH 01/36] Create RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Recursive/RecursiveLinearSearch.js diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js new file mode 100644 index 0000000000..db67399026 --- /dev/null +++ b/Recursive/RecursiveLinearSearch.js @@ -0,0 +1,27 @@ +/** + * Recursive Linear Search + * + * This function searches for a key within an array using a recursive approach. + * + * @param {Array} arr - The array to search within. + * @param {*} key - The element to search for. + * @param {number} index - (Optional) The current index being checked in the array (default is 0). + * @returns {number} - The index of the element if found, or -1 if not found. + */ +function recursiveLinearSearch(arr, key, index = 0) { + // Base case: If we have searched the entire array and haven't found the key, return -1. + if (index === arr.length) { + return -1; + } + + // Base case: If the current element matches the key, return its index. + if (arr[index] === key) { + return index; + } + + // Recursive case: Continue searching in the rest of the array. + return recursiveLinearSearch(arr, key, index + 1); +} + +export { recursiveLinearSearch }; + From c865c35efd24b3332ddda02bdf1ed8dedc1f672f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:01:02 +0530 Subject: [PATCH 02/36] Create RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Recursive/test/RecursiveLinearSearch.test.js diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js new file mode 100644 index 0000000000..b30e2314ba --- /dev/null +++ b/Recursive/test/RecursiveLinearSearch.test.js @@ -0,0 +1,30 @@ +import { recursiveLinearSearch } from '../RecursiveLinearSearch'; + +describe('RecursiveLinearSearch', () => { + const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999]; + + it('should return index 3 for searchValue 10', () => { + const searchValue = 10; + expect(recursiveLinearSearch(arr, searchValue)).toBe(3); + }); + + it('should return index 0 for searchValue 2', () => { + const searchValue = 2; + expect(recursiveLinearSearch(arr, searchValue)).toBe(0); + }); + + it('should return index 13 for searchValue 999', () => { + const searchValue = 999; + expect(recursiveLinearSearch(arr, searchValue)).toBe(13); + }); + + it('should return -1 for searchValue 1', () => { + const searchValue = 1; + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); + }); + + it('should return -1 for searchValue 1000', () => { + const searchValue = 1000; + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); + }); +}); From 00c13afd6bfc81655f7d28ba38f98780f9420f1a Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:07:02 +0530 Subject: [PATCH 03/36] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index db67399026..556fdc2f8c 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -1,3 +1,5 @@ +// Ecplanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ + /** * Recursive Linear Search * From 3185e53d6b97edda59bb293950fefb8dbb9c2e0d Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:12:30 +0530 Subject: [PATCH 04/36] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 556fdc2f8c..473dd49e81 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -10,20 +10,19 @@ * @param {number} index - (Optional) The current index being checked in the array (default is 0). * @returns {number} - The index of the element if found, or -1 if not found. */ -function recursiveLinearSearch(arr, key, index = 0) { +function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1; + return -1;; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index; + return index;; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1); + return recursiveLinearSearch(arr, key, index + 1);; } -export { recursiveLinearSearch }; - +export { recursiveLinearSearch };; From 8e77a5e9c3f349b626a5499d7d36cc0060317286 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:15:07 +0530 Subject: [PATCH 05/36] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 473dd49e81..3eb20df2c5 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1;; + return -1 ;; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index;; + return index ;; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1);; + return recursiveLinearSearch(arr, key, index + 1) ;; } -export { recursiveLinearSearch };; +export { recursiveLinearSearch } ;; From a6dd54f09c212719f022a391e9abd2fe61ef8269 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:17:00 +0530 Subject: [PATCH 06/36] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 3eb20df2c5..e0045b11f9 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -1,4 +1,4 @@ -// Ecplanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ +// Explanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ /** * Recursive Linear Search @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1 ;; + return -1; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index ;; + return index; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1) ;; + return recursiveLinearSearch(arr, key, index + 1); } -export { recursiveLinearSearch } ;; +export { recursiveLinearSearch }; From c1961f1814bb6e33cccfc8a14914cdf65c58e59f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:19:44 +0530 Subject: [PATCH 07/36] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index e0045b11f9..9b02132527 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1; + return -1 } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index; + return index } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1); + return recursiveLinearSearch(arr, key, index + 1) } -export { recursiveLinearSearch }; +export { recursiveLinearSearch } From a8391840ca61c31ef7c7b38bbe1eb591b673ca24 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:21:32 +0530 Subject: [PATCH 08/36] Update RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 36 ++++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js index b30e2314ba..2a7daa25a3 100644 --- a/Recursive/test/RecursiveLinearSearch.test.js +++ b/Recursive/test/RecursiveLinearSearch.test.js @@ -1,30 +1,30 @@ -import { recursiveLinearSearch } from '../RecursiveLinearSearch'; +import { recursiveLinearSearch } from '../RecursiveLinearSearch' describe('RecursiveLinearSearch', () => { - const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999]; + const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999] it('should return index 3 for searchValue 10', () => { - const searchValue = 10; - expect(recursiveLinearSearch(arr, searchValue)).toBe(3); - }); + const searchValue = 10 + expect(recursiveLinearSearch(arr, searchValue)).toBe(3) + }) it('should return index 0 for searchValue 2', () => { - const searchValue = 2; - expect(recursiveLinearSearch(arr, searchValue)).toBe(0); - }); + const searchValue = 2 + expect(recursiveLinearSearch(arr, searchValue)).toBe(0) + }) it('should return index 13 for searchValue 999', () => { - const searchValue = 999; - expect(recursiveLinearSearch(arr, searchValue)).toBe(13); - }); + const searchValue = 999 + expect(recursiveLinearSearch(arr, searchValue)).toBe(13) + }) it('should return -1 for searchValue 1', () => { - const searchValue = 1; - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); - }); + const searchValue = 1 + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) + }) it('should return -1 for searchValue 1000', () => { - const searchValue = 1000; - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); - }); -}); + const searchValue = 1000 + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) + }) +}) From c1b4f0c019a70080792cc7c305951fcd870e15f2 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:11:15 +0530 Subject: [PATCH 09/36] Delete Recursive/RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 Recursive/RecursiveLinearSearch.js diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js deleted file mode 100644 index 9b02132527..0000000000 --- a/Recursive/RecursiveLinearSearch.js +++ /dev/null @@ -1,28 +0,0 @@ -// Explanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ - -/** - * Recursive Linear Search - * - * This function searches for a key within an array using a recursive approach. - * - * @param {Array} arr - The array to search within. - * @param {*} key - The element to search for. - * @param {number} index - (Optional) The current index being checked in the array (default is 0). - * @returns {number} - The index of the element if found, or -1 if not found. - */ -function recursiveLinearSearch (arr, key, index = 0) { - // Base case: If we have searched the entire array and haven't found the key, return -1. - if (index === arr.length) { - return -1 - } - - // Base case: If the current element matches the key, return its index. - if (arr[index] === key) { - return index - } - - // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1) -} - -export { recursiveLinearSearch } From dd8cb8fcef2734ea0e43119d201376d50fe3a94f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:11:29 +0530 Subject: [PATCH 10/36] Delete Recursive/test/RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 30 -------------------- 1 file changed, 30 deletions(-) delete mode 100644 Recursive/test/RecursiveLinearSearch.test.js diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js deleted file mode 100644 index 2a7daa25a3..0000000000 --- a/Recursive/test/RecursiveLinearSearch.test.js +++ /dev/null @@ -1,30 +0,0 @@ -import { recursiveLinearSearch } from '../RecursiveLinearSearch' - -describe('RecursiveLinearSearch', () => { - const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999] - - it('should return index 3 for searchValue 10', () => { - const searchValue = 10 - expect(recursiveLinearSearch(arr, searchValue)).toBe(3) - }) - - it('should return index 0 for searchValue 2', () => { - const searchValue = 2 - expect(recursiveLinearSearch(arr, searchValue)).toBe(0) - }) - - it('should return index 13 for searchValue 999', () => { - const searchValue = 999 - expect(recursiveLinearSearch(arr, searchValue)).toBe(13) - }) - - it('should return -1 for searchValue 1', () => { - const searchValue = 1 - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) - }) - - it('should return -1 for searchValue 1000', () => { - const searchValue = 1000 - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) - }) -}) From aaef3a8e29ce24b46930119d96cea948bbcc265e Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:25:50 +0530 Subject: [PATCH 11/36] Create Rectangle.js --- Geometry/Rectangle.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Geometry/Rectangle.js diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js new file mode 100644 index 0000000000..ebc1c001b3 --- /dev/null +++ b/Geometry/Rectangle.js @@ -0,0 +1,42 @@ +/** + * Represents a Rectangle with specified length and width. + */ +export default class Rectangle { + /** + * Create a new Rectangle instance. + * + * @param {number} length - The length of the rectangle. + * @param {number} width - The width of the rectangle. + * @throws {Error} If length or width is less than or equal to 0. + */ + constructor(length, width) { + this.length = length + this.width = width + } + + /** + * Calculate the perimeter of the rectangle. + * + * @returns {number} The perimeter of the rectangle. + * @throws {Error} If length or width is less than or equal to 0. + */ + calculatePerimeter() { + if (this.length <= 0 || this.width <= 0) { + throw new Error("Invalid input. Length and width must be greater than 0.") + } + return 2 * (this.length + this.width) + } + + /** + * Calculate the area of the rectangle. + * + * @returns {number} The area of the rectangle. + * @throws {Error} If length or width is less than or equal to 0. + */ + calculateArea() { + if (this.length <= 0 || this.width <= 0) { + throw new Error("Invalid input. Length and width must be greater than 0.") + } + return this.length * this.width + } +} From 82e00cbddee8eed653845ba21dc7a159180d6c31 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:27:04 +0530 Subject: [PATCH 12/36] Update Rectangle.js --- Geometry/Rectangle.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js index ebc1c001b3..cc51d9595c 100644 --- a/Geometry/Rectangle.js +++ b/Geometry/Rectangle.js @@ -1,5 +1,6 @@ /** * Represents a Rectangle with specified length and width. + * Explanation:- https://en.wikipedia.org/wiki/Rectangle */ export default class Rectangle { /** From 2109aa6fe2f670d25d40a0ec9621a481a185c413 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:27:55 +0530 Subject: [PATCH 13/36] Create Rectangle.test.js --- Geometry/Test/Rectangle.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Geometry/Test/Rectangle.test.js diff --git a/Geometry/Test/Rectangle.test.js b/Geometry/Test/Rectangle.test.js new file mode 100644 index 0000000000..4139e4d708 --- /dev/null +++ b/Geometry/Test/Rectangle.test.js @@ -0,0 +1,15 @@ +import Rectangle from '../Rectangle' + +const rectangle = new Rectangle(5, 4) + +test('Calculate the area of a rectangle', () => { + expect(parseFloat(rectangle.calculateArea().toFixed(2))).toEqual(20.00) +}) + +test('Calculate the perimeter of a rectangle', () => { + expect(parseFloat(rectangle.calculatePerimeter().toFixed(2))).toEqual(18.00) +}) + +test('Invalid input for rectangle', () => { + expect(() => new Rectangle(0, 4)).toThrowError('Invalid input. Length and width must be greater than 0.') +}) From 72765bd97f3e810144277a1a2ff24e2fa63ac615 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 16:33:11 +0530 Subject: [PATCH 14/36] Update Rectangle.js --- Geometry/Rectangle.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js index cc51d9595c..0461036816 100644 --- a/Geometry/Rectangle.js +++ b/Geometry/Rectangle.js @@ -10,7 +10,7 @@ export default class Rectangle { * @param {number} width - The width of the rectangle. * @throws {Error} If length or width is less than or equal to 0. */ - constructor(length, width) { + constructor (length, width) { this.length = length this.width = width } @@ -21,9 +21,9 @@ export default class Rectangle { * @returns {number} The perimeter of the rectangle. * @throws {Error} If length or width is less than or equal to 0. */ - calculatePerimeter() { + calculatePerimeter () { if (this.length <= 0 || this.width <= 0) { - throw new Error("Invalid input. Length and width must be greater than 0.") + throw new Error('Invalid input. Length and width must be greater than 0.') } return 2 * (this.length + this.width) } @@ -34,9 +34,9 @@ export default class Rectangle { * @returns {number} The area of the rectangle. * @throws {Error} If length or width is less than or equal to 0. */ - calculateArea() { + calculateArea () { if (this.length <= 0 || this.width <= 0) { - throw new Error("Invalid input. Length and width must be greater than 0.") + throw new Error('Invalid input. Length and width must be greater than 0.') } return this.length * this.width } From 197cf3c5a64adf2df320650539e192475822d1e6 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 18:22:37 +0530 Subject: [PATCH 15/36] Delete Geometry/Rectangle.js --- Geometry/Rectangle.js | 43 ------------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 Geometry/Rectangle.js diff --git a/Geometry/Rectangle.js b/Geometry/Rectangle.js deleted file mode 100644 index 0461036816..0000000000 --- a/Geometry/Rectangle.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Represents a Rectangle with specified length and width. - * Explanation:- https://en.wikipedia.org/wiki/Rectangle - */ -export default class Rectangle { - /** - * Create a new Rectangle instance. - * - * @param {number} length - The length of the rectangle. - * @param {number} width - The width of the rectangle. - * @throws {Error} If length or width is less than or equal to 0. - */ - constructor (length, width) { - this.length = length - this.width = width - } - - /** - * Calculate the perimeter of the rectangle. - * - * @returns {number} The perimeter of the rectangle. - * @throws {Error} If length or width is less than or equal to 0. - */ - calculatePerimeter () { - if (this.length <= 0 || this.width <= 0) { - throw new Error('Invalid input. Length and width must be greater than 0.') - } - return 2 * (this.length + this.width) - } - - /** - * Calculate the area of the rectangle. - * - * @returns {number} The area of the rectangle. - * @throws {Error} If length or width is less than or equal to 0. - */ - calculateArea () { - if (this.length <= 0 || this.width <= 0) { - throw new Error('Invalid input. Length and width must be greater than 0.') - } - return this.length * this.width - } -} From 3e36938a2aaad117a2a828c45747e8efbbf965de Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 18:23:02 +0530 Subject: [PATCH 16/36] Delete Geometry/Test/Rectangle.test.js --- Geometry/Test/Rectangle.test.js | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 Geometry/Test/Rectangle.test.js diff --git a/Geometry/Test/Rectangle.test.js b/Geometry/Test/Rectangle.test.js deleted file mode 100644 index 4139e4d708..0000000000 --- a/Geometry/Test/Rectangle.test.js +++ /dev/null @@ -1,15 +0,0 @@ -import Rectangle from '../Rectangle' - -const rectangle = new Rectangle(5, 4) - -test('Calculate the area of a rectangle', () => { - expect(parseFloat(rectangle.calculateArea().toFixed(2))).toEqual(20.00) -}) - -test('Calculate the perimeter of a rectangle', () => { - expect(parseFloat(rectangle.calculatePerimeter().toFixed(2))).toEqual(18.00) -}) - -test('Invalid input for rectangle', () => { - expect(() => new Rectangle(0, 4)).toThrowError('Invalid input. Length and width must be greater than 0.') -}) From 860dda7e0e22d2df752979b15f3c2144fb82ba86 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 18:28:46 +0530 Subject: [PATCH 17/36] Create CircularDoublyLinkedList.js --- .../Linked-List/CircularDoublyLinkedList.js | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 Data-Structures/Linked-List/CircularDoublyLinkedList.js diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js new file mode 100644 index 0000000000..cdbfa9a1f9 --- /dev/null +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -0,0 +1,119 @@ +class Node { + constructor (element) { + this.element = element + this.next = null + this.prev = null + } +} + +class CircularDoublyLinkedList { + constructor () { + this.length = 0 + this.head = null + this.tail = null + } + + // Add new element + append (element) { + const node = new Node (element) + + if (!this.head) { + this.head = node + this.tail = node + node.next = node // Circular reference + node.prev = node // Circular reference + } else { + node.prev = this.tail + node.next = this.head + this.tail.next = node + this.head.prev = node + this.tail = node + } + + this.length++ + } + + // Insert element at a specific position + insert (position, element) { + if (position >= 0 && position <= this.length) { + const node = new Node(element) + let current = this.head + let previous = null + let index = 0 + + if (position === 0) { + node.next = current + node.prev = this.tail + this.head = node + current.prev = node + this.tail.next = node + } else { + while (index++ < position) { + previous = current + current = current.next + } + node.next = current + node.prev = previous + previous.next = node + current.prev = node + + if (position === this.length) { + this.tail = node + } + } + + this.length++ + return true + } else { + return false + } + } + + // Remove element at a specific position + removeAt (position) { + if (position >= 0 && position < this.length) { + let current = this.head + let previous = null + let index = 0 + + if (position === 0) { + this.head = current.next + this.head.prev = this.tail + this.tail.next = this.head + if (this.length === 1) { + this.tail = null + } + } else { + while (index++ < position) { + previous = current + current = current.next + } + previous.next = current.next + current.next.prev = previous + + if (position === this.length - 1) { + this.tail = previous + } + } + + this.length-- + return current.element + } else { + return null + } + } + + // Print the list + print() { + const elements = [] + let currentNode = this.head + if (!currentNode) return + do { + elements.push(currentNode.element) + currentNode = currentNode.next + } while (currentNode !== this.head) + console.log(elements.join(' <-> ')) + } +} + +export { CircularDoublyLinkedList } From 9bdaf7d6d4aa8fae84f0bffd5d6cf533abe06cfe Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 18:38:15 +0530 Subject: [PATCH 18/36] Update CircularDoublyLinkedList.js --- .../Linked-List/CircularDoublyLinkedList.js | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index cdbfa9a1f9..ebb2b44feb 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -15,7 +15,7 @@ class CircularDoublyLinkedList { // Add new element append (element) { - const node = new Node (element) + const node = new Node(element) if (!this.head) { this.head = node @@ -104,8 +104,8 @@ class CircularDoublyLinkedList { } // Print the list - print() { - const elements = [] + print () { + const elements = []; let currentNode = this.head if (!currentNode) return do { @@ -114,6 +114,23 @@ class CircularDoublyLinkedList { } while (currentNode !== this.head) console.log(elements.join(' <-> ')) } + + // Convert the list to a string + toString () { + const elements = [] + let currentNode = this.head + if (!currentNode) return '' + do { + elements.push(currentNode.element) + currentNode = currentNode.next + } while (currentNode !== this.head) + return elements.join(' <-> ') + } + + // Check if the list is empty + isEmpty () { + return this.length === 0 + } } export { CircularDoublyLinkedList } From 834cd1b5f7f0a6b14604958d8f10efddc201b8b9 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 18:38:57 +0530 Subject: [PATCH 19/36] Update CircularDoublyLinkedList.js --- Data-Structures/Linked-List/CircularDoublyLinkedList.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index ebb2b44feb..81234e8432 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -1,3 +1,5 @@ +// Explanation:- https://www.javatpoint.com/circular-doubly-linked-list + class Node { constructor (element) { this.element = element From 12ec96edd500215fd2c3e9deeb9662a7ef8c4655 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 18:48:14 +0530 Subject: [PATCH 20/36] Create CircularDoublyLinkedList.test.js --- .../test/CircularDoublyLinkedList.test.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js new file mode 100644 index 0000000000..d3fc7492c6 --- /dev/null +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -0,0 +1,55 @@ +import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList'; // Adjust the import path as needed + +describe('CircularDoublyLinkedList', () => { + it('Check append', () => { + const list = new CircularDoublyLinkedList(); + + list.append(1); + expect(list.toString()).toEqual('1'); + + list.append(2); + expect(list.toString()).toEqual('1 <-> 2'); + }); + + it('Check insert', () => { + const list = new CircularDoublyLinkedList(); + + list.insert(0, 1); + expect(list.toString()).toEqual('1'); + + list.insert(1, 20); + expect(list.toString()).toEqual('1 <-> 20'); + }); + + it('Check removeAt', () => { + const list = new CircularDoublyLinkedList(); + + list.append(10); + list.append(40); + list.append(30); + + list.removeAt(0); + expect(list.toString()).toEqual('40 <-> 30'); + + list.removeAt(1); + expect(list.toString()).toEqual('40'); + }); + + it('Check print', () => { + const list = new CircularDoublyLinkedList(); + + list.append(20); + expect(() => { + list.print(); + }).not.toThrow(); + }); + + it('Check isEmpty', () => { + const list = new CircularDoublyLinkedList(); + + expect(list.isEmpty()).toEqual(true); + + list.append('Hello'); + expect(list.isEmpty()).toEqual(false); + }); +}); From b1f967a0fbfe925aa5d9d490ad171e19cf533937 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 18:51:37 +0530 Subject: [PATCH 21/36] Update CircularDoublyLinkedList.test.js --- .../test/CircularDoublyLinkedList.test.js | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index d3fc7492c6..089ce0ae0d 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -1,55 +1,55 @@ -import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList'; // Adjust the import path as needed +import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList' // Adjust the import path as needed -describe('CircularDoublyLinkedList', () => { - it('Check append', () => { - const list = new CircularDoublyLinkedList(); +describe ('CircularDoublyLinkedList', () => { + it ('Check append', () => { + const list = new CircularDoublyLinkedList() - list.append(1); - expect(list.toString()).toEqual('1'); + list.append(1) + expect(list.toString()).toEqual('1') - list.append(2); - expect(list.toString()).toEqual('1 <-> 2'); - }); + list.append(2) + expect(list.toString()).toEqual('1 <-> 2') + }) - it('Check insert', () => { - const list = new CircularDoublyLinkedList(); + it ('Check insert', () => { + const list = new CircularDoublyLinkedList() - list.insert(0, 1); - expect(list.toString()).toEqual('1'); + list.insert(0, 1) + expect(list.toString()).toEqual('1') - list.insert(1, 20); - expect(list.toString()).toEqual('1 <-> 20'); - }); + list.insert(1, 20) + expect(list.toString()).toEqual('1 <-> 20') + }) - it('Check removeAt', () => { - const list = new CircularDoublyLinkedList(); + it ('Check removeAt', () => { + const list = new CircularDoublyLinkedList() - list.append(10); - list.append(40); - list.append(30); + list.append(10) + list.append(40) + list.append(30) - list.removeAt(0); - expect(list.toString()).toEqual('40 <-> 30'); + list.removeAt(0) + expect(list.toString()).toEqual('40 <-> 30') - list.removeAt(1); - expect(list.toString()).toEqual('40'); - }); + list.removeAt(1) + expect(list.toString()).toEqual('40') + }) - it('Check print', () => { - const list = new CircularDoublyLinkedList(); + it ('Check print', () => { + const list = new CircularDoublyLinkedList() - list.append(20); + list.append(20) expect(() => { - list.print(); - }).not.toThrow(); - }); + list.print() + }).not.toThrow() + }) - it('Check isEmpty', () => { - const list = new CircularDoublyLinkedList(); + it ('Check isEmpty', () => { + const list = new CircularDoublyLinkedList() - expect(list.isEmpty()).toEqual(true); + expect(list.isEmpty()).toEqual(true) - list.append('Hello'); - expect(list.isEmpty()).toEqual(false); - }); -}); + list.append('Hello') + expect(list.isEmpty()).toEqual(false) + }) +}) From 808d740d72c2a130085eed1aca9c6e7ff24869b6 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 19:02:32 +0530 Subject: [PATCH 22/36] Update CircularDoublyLinkedList.js --- Data-Structures/Linked-List/CircularDoublyLinkedList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index 81234e8432..6c0fcdf7fd 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -107,7 +107,7 @@ class CircularDoublyLinkedList { // Print the list print () { - const elements = []; + const elements = [] let currentNode = this.head if (!currentNode) return do { From 393e591f9c33145819b50d1b3e7d4f90121f3aaf Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 19:03:56 +0530 Subject: [PATCH 23/36] Update CircularDoublyLinkedList.test.js --- .../test/CircularDoublyLinkedList.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index 089ce0ae0d..7e6e0e298b 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -1,7 +1,7 @@ import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList' // Adjust the import path as needed -describe ('CircularDoublyLinkedList', () => { - it ('Check append', () => { +describe('CircularDoublyLinkedList', () => { + it('Check append', () => { const list = new CircularDoublyLinkedList() list.append(1) @@ -11,7 +11,7 @@ describe ('CircularDoublyLinkedList', () => { expect(list.toString()).toEqual('1 <-> 2') }) - it ('Check insert', () => { + it('Check insert', () => { const list = new CircularDoublyLinkedList() list.insert(0, 1) @@ -21,7 +21,7 @@ describe ('CircularDoublyLinkedList', () => { expect(list.toString()).toEqual('1 <-> 20') }) - it ('Check removeAt', () => { + it('Check removeAt', () => { const list = new CircularDoublyLinkedList() list.append(10) @@ -35,7 +35,7 @@ describe ('CircularDoublyLinkedList', () => { expect(list.toString()).toEqual('40') }) - it ('Check print', () => { + it('Check print', () => { const list = new CircularDoublyLinkedList() list.append(20) @@ -44,7 +44,7 @@ describe ('CircularDoublyLinkedList', () => { }).not.toThrow() }) - it ('Check isEmpty', () => { + it('Check isEmpty', () => { const list = new CircularDoublyLinkedList() expect(list.isEmpty()).toEqual(true) From 46b572ea89bb2eaa05c765e82428c0bb59f18e4f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 19:34:53 +0530 Subject: [PATCH 24/36] Update CircularDoublyLinkedList.js --- .../Linked-List/CircularDoublyLinkedList.js | 65 +++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index 6c0fcdf7fd..3818dc8ab5 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -1,7 +1,11 @@ // Explanation:- https://www.javatpoint.com/circular-doubly-linked-list class Node { - constructor (element) { + /** + * Creates a new Node with the given element. + * @param {*} element - The element to be stored in the node. + */ + constructor(element) { this.element = element this.next = null this.prev = null @@ -9,14 +13,20 @@ class Node { } class CircularDoublyLinkedList { - constructor () { + /** + * Creates an empty Circular Doubly Linked List. + */ + constructor() { this.length = 0 this.head = null this.tail = null } - // Add new element - append (element) { + /** + * Appends an element to the end of the list. + * @param {*} element - The element to be appended. + */ + append(element) { const node = new Node(element) if (!this.head) { @@ -35,8 +45,13 @@ class CircularDoublyLinkedList { this.length++ } - // Insert element at a specific position - insert (position, element) { + /** + * Inserts an element at the specified position in the list. + * @param {number} position - The position at which to insert the element. + * @param {*} element - The element to be inserted. + * @returns {boolean} - True if the insertion was successful, false otherwise. + */ + insert(position, element) { if (position >= 0 && position <= this.length) { const node = new Node(element) let current = this.head @@ -71,8 +86,12 @@ class CircularDoublyLinkedList { } } - // Remove element at a specific position - removeAt (position) { + /** + * Removes and returns the element at the specified position. + * @param {number} position - The position of the element to be removed. + * @returns {*} - The removed element, or null if the position is invalid. + */ + removeAt(position) { if (position >= 0 && position < this.length) { let current = this.head let previous = null @@ -105,32 +124,26 @@ class CircularDoublyLinkedList { } } - // Print the list - print () { + /** + * Converts the list to an array. + * @returns {Array} - An array containing the elements of the list. + */ + toArray() { const elements = [] let currentNode = this.head - if (!currentNode) return + if (!currentNode) return elements do { elements.push(currentNode.element) currentNode = currentNode.next } while (currentNode !== this.head) - console.log(elements.join(' <-> ')) + return elements } - // Convert the list to a string - toString () { - const elements = [] - let currentNode = this.head - if (!currentNode) return '' - do { - elements.push(currentNode.element) - currentNode = currentNode.next - } while (currentNode !== this.head) - return elements.join(' <-> ') - } - - // Check if the list is empty - isEmpty () { + /** + * Checks if the list is empty. + * @returns {boolean} - True if the list is empty, false otherwise. + */ + isEmpty() { return this.length === 0 } } From 476aae151f49dd01bb14bc17d8463a36a59d20ee Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 19:42:55 +0530 Subject: [PATCH 25/36] Update CircularDoublyLinkedList.test.js --- .../test/CircularDoublyLinkedList.test.js | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index 7e6e0e298b..61b4421d7b 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -1,55 +1,69 @@ import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList' // Adjust the import path as needed describe('CircularDoublyLinkedList', () => { - it('Check append', () => { + /** + * Creates a new CircularDoublyLinkedList and appends elements to it. + * @param {Array} elements - The elements to append. + * @returns {CircularDoublyLinkedList} - The created list. + */ + function createAndAppend(elements) { const list = new CircularDoublyLinkedList() + elements.forEach((element) => list.append(element)) + return list + } + + it('Check append', () => { + const list = createAndAppend([1]) - list.append(1) - expect(list.toString()).toEqual('1') + expect(list.toArray()).toEqual([1]) list.append(2) - expect(list.toString()).toEqual('1 <-> 2') + expect(list.toArray()).toEqual([1, 2]) }) it('Check insert', () => { - const list = new CircularDoublyLinkedList() + const list = createAndAppend([1]) - list.insert(0, 1) - expect(list.toString()).toEqual('1') + list.insert(0, 20) + expect(list.toArray()).toEqual([20, 1]) - list.insert(1, 20) - expect(list.toString()).toEqual('1 <-> 20') + list.insert(1, 30) + expect(list.toArray()).toEqual([20, 30, 1]) }) it('Check removeAt', () => { - const list = new CircularDoublyLinkedList() - - list.append(10) - list.append(40) - list.append(30) + const list = createAndAppend([10, 40, 30]) list.removeAt(0) - expect(list.toString()).toEqual('40 <-> 30') + expect(list.toArray()).toEqual([40, 30]) list.removeAt(1) - expect(list.toString()).toEqual('40') + expect(list.toArray()).toEqual([40]) }) - it('Check print', () => { - const list = new CircularDoublyLinkedList() + it('Check toArray', () => { + const list = createAndAppend([20]) + + expect(list.toArray()).toEqual([20]) + }) - list.append(20) - expect(() => { - list.print() - }).not.toThrow() + it('Check createIterator', () => { + const list = createAndAppend([10, 20, 30]) + const iterator = list.createIterator() + + const elements = []; + for (let element of iterator) { + elements.push(element) + } + + expect(elements).toEqual([10, 20, 30]) }) it('Check isEmpty', () => { - const list = new CircularDoublyLinkedList() - - expect(list.isEmpty()).toEqual(true) + const emptyList = new CircularDoublyLinkedList() + expect(emptyList.isEmpty()).toEqual(true) - list.append('Hello') - expect(list.isEmpty()).toEqual(false) + const nonEmptyList = createAndAppend(['Hello']) + expect(nonEmptyList.isEmpty()).toEqual(false) }) }) From 4997d01dbe9cbaca3fff9b2db402908ff1662179 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 19:47:13 +0530 Subject: [PATCH 26/36] Update CircularDoublyLinkedList.test.js --- .../Linked-List/test/CircularDoublyLinkedList.test.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index 61b4421d7b..bb8cdf9951 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -6,7 +6,7 @@ describe('CircularDoublyLinkedList', () => { * @param {Array} elements - The elements to append. * @returns {CircularDoublyLinkedList} - The created list. */ - function createAndAppend(elements) { + function createAndAppend (elements) { const list = new CircularDoublyLinkedList() elements.forEach((element) => list.append(element)) return list @@ -43,19 +43,16 @@ describe('CircularDoublyLinkedList', () => { it('Check toArray', () => { const list = createAndAppend([20]) - expect(list.toArray()).toEqual([20]) }) it('Check createIterator', () => { const list = createAndAppend([10, 20, 30]) const iterator = list.createIterator() - - const elements = []; + const elements = [] for (let element of iterator) { elements.push(element) } - expect(elements).toEqual([10, 20, 30]) }) From 627c3ffea57d00f6cf7c7b8983dba2cce3993988 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 19:48:08 +0530 Subject: [PATCH 27/36] Update CircularDoublyLinkedList.js --- .../Linked-List/CircularDoublyLinkedList.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index 3818dc8ab5..57757087c4 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -5,7 +5,7 @@ class Node { * Creates a new Node with the given element. * @param {*} element - The element to be stored in the node. */ - constructor(element) { + constructor (element) { this.element = element this.next = null this.prev = null @@ -16,7 +16,7 @@ class CircularDoublyLinkedList { /** * Creates an empty Circular Doubly Linked List. */ - constructor() { + constructor () { this.length = 0 this.head = null this.tail = null @@ -26,7 +26,7 @@ class CircularDoublyLinkedList { * Appends an element to the end of the list. * @param {*} element - The element to be appended. */ - append(element) { + append (element) { const node = new Node(element) if (!this.head) { @@ -51,7 +51,7 @@ class CircularDoublyLinkedList { * @param {*} element - The element to be inserted. * @returns {boolean} - True if the insertion was successful, false otherwise. */ - insert(position, element) { + insert (position, element) { if (position >= 0 && position <= this.length) { const node = new Node(element) let current = this.head @@ -91,7 +91,7 @@ class CircularDoublyLinkedList { * @param {number} position - The position of the element to be removed. * @returns {*} - The removed element, or null if the position is invalid. */ - removeAt(position) { + removeAt (position) { if (position >= 0 && position < this.length) { let current = this.head let previous = null @@ -128,7 +128,7 @@ class CircularDoublyLinkedList { * Converts the list to an array. * @returns {Array} - An array containing the elements of the list. */ - toArray() { + toArray () { const elements = [] let currentNode = this.head if (!currentNode) return elements @@ -143,7 +143,7 @@ class CircularDoublyLinkedList { * Checks if the list is empty. * @returns {boolean} - True if the list is empty, false otherwise. */ - isEmpty() { + isEmpty () { return this.length === 0 } } From 6eb5856bc5d1687b4cb31f5993c51cd023bf19c5 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 19:52:58 +0530 Subject: [PATCH 28/36] Update CircularDoublyLinkedList.test.js --- .../Linked-List/test/CircularDoublyLinkedList.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index bb8cdf9951..6ae762749d 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -50,7 +50,7 @@ describe('CircularDoublyLinkedList', () => { const list = createAndAppend([10, 20, 30]) const iterator = list.createIterator() const elements = [] - for (let element of iterator) { + for (const element of iterator) { elements.push(element) } expect(elements).toEqual([10, 20, 30]) From 21c42c95bcf6f9598c48e9b1ed35636f6944d428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:03:29 +0200 Subject: [PATCH 29/36] Clean up tests a bit --- .../test/CircularDoublyLinkedList.test.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index 6ae762749d..6128d72291 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -1,4 +1,4 @@ -import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList' // Adjust the import path as needed +import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList' describe('CircularDoublyLinkedList', () => { /** @@ -12,7 +12,7 @@ describe('CircularDoublyLinkedList', () => { return list } - it('Check append', () => { + it('append', () => { const list = createAndAppend([1]) expect(list.toArray()).toEqual([1]) @@ -31,7 +31,7 @@ describe('CircularDoublyLinkedList', () => { expect(list.toArray()).toEqual([20, 30, 1]) }) - it('Check removeAt', () => { + it('removeAt', () => { const list = createAndAppend([10, 40, 30]) list.removeAt(0) @@ -41,22 +41,17 @@ describe('CircularDoublyLinkedList', () => { expect(list.toArray()).toEqual([40]) }) - it('Check toArray', () => { + it('toArray', () => { const list = createAndAppend([20]) expect(list.toArray()).toEqual([20]) }) - it('Check createIterator', () => { + it('createIterator', () => { const list = createAndAppend([10, 20, 30]) - const iterator = list.createIterator() - const elements = [] - for (const element of iterator) { - elements.push(element) - } - expect(elements).toEqual([10, 20, 30]) + expect([...list.createIterator()]).toEqual([10, 20, 30]) }) - it('Check isEmpty', () => { + it('isEmpty', () => { const emptyList = new CircularDoublyLinkedList() expect(emptyList.isEmpty()).toEqual(true) From eac5a0fbfdf129c0f9697faf9f3d34b093d0ba43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:04:55 +0200 Subject: [PATCH 30/36] missed one "Check" --- .../Linked-List/test/CircularDoublyLinkedList.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index 6128d72291..ee338c7835 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -6,7 +6,7 @@ describe('CircularDoublyLinkedList', () => { * @param {Array} elements - The elements to append. * @returns {CircularDoublyLinkedList} - The created list. */ - function createAndAppend (elements) { + function createAndAppend(elements) { const list = new CircularDoublyLinkedList() elements.forEach((element) => list.append(element)) return list @@ -21,7 +21,7 @@ describe('CircularDoublyLinkedList', () => { expect(list.toArray()).toEqual([1, 2]) }) - it('Check insert', () => { + it('insert', () => { const list = createAndAppend([1]) list.insert(0, 20) From 142473568097a6cdd261cc8d3c965d786f68d1c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:08:22 +0200 Subject: [PATCH 31/36] createIterator -> elements --- .../Linked-List/test/CircularDoublyLinkedList.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index ee338c7835..3c09182263 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -46,9 +46,9 @@ describe('CircularDoublyLinkedList', () => { expect(list.toArray()).toEqual([20]) }) - it('createIterator', () => { + it('elements', () => { const list = createAndAppend([10, 20, 30]) - expect([...list.createIterator()]).toEqual([10, 20, 30]) + expect([...list.elements()]).toEqual([10, 20, 30]) }) it('isEmpty', () => { From aba860024b532f0259e77f633f835d5d7c90d309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:12:16 +0200 Subject: [PATCH 32/36] Replace toArray with elements iterator --- .../Linked-List/CircularDoublyLinkedList.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index 57757087c4..23709df946 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -125,18 +125,15 @@ class CircularDoublyLinkedList { } /** - * Converts the list to an array. - * @returns {Array} - An array containing the elements of the list. + * Iterator over the elements in the list. */ - toArray () { - const elements = [] + *elements() { let currentNode = this.head - if (!currentNode) return elements + if (!currentNode) return do { - elements.push(currentNode.element) + yield currentNode.element currentNode = currentNode.next } while (currentNode !== this.head) - return elements } /** From ab99a65c7eba095cf09a3fcba48b46eec0ef035c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:12:42 +0200 Subject: [PATCH 33/36] Remove `toArray` test --- .../Linked-List/test/CircularDoublyLinkedList.test.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index 3c09182263..ad7baf997b 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -41,11 +41,6 @@ describe('CircularDoublyLinkedList', () => { expect(list.toArray()).toEqual([40]) }) - it('toArray', () => { - const list = createAndAppend([20]) - expect(list.toArray()).toEqual([20]) - }) - it('elements', () => { const list = createAndAppend([10, 20, 30]) expect([...list.elements()]).toEqual([10, 20, 30]) From 42b8013cf3250fc3e7235014adf2a9994f3bc2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:17:51 +0200 Subject: [PATCH 34/36] Remove usages of `toArray` in other tests --- .../test/CircularDoublyLinkedList.test.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index ad7baf997b..944b257347 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -12,33 +12,35 @@ describe('CircularDoublyLinkedList', () => { return list } + const expectList = (list, expected) => expect([...list.elements()]).toEqual(expected) + it('append', () => { const list = createAndAppend([1]) - expect(list.toArray()).toEqual([1]) + expectList(list, [1]) list.append(2) - expect(list.toArray()).toEqual([1, 2]) + expectList(list, [1, 2]) }) it('insert', () => { const list = createAndAppend([1]) list.insert(0, 20) - expect(list.toArray()).toEqual([20, 1]) + expectList(list, [20, 1]) list.insert(1, 30) - expect(list.toArray()).toEqual([20, 30, 1]) + expectList(list, [20, 30, 1]) }) it('removeAt', () => { const list = createAndAppend([10, 40, 30]) list.removeAt(0) - expect(list.toArray()).toEqual([40, 30]) + expectList(list, [40, 30]) list.removeAt(1) - expect(list.toArray()).toEqual([40]) + expectList(list, [40]) }) it('elements', () => { From 93550020b3d0aa19b7a863b54c7a1d3451727ad4 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Mon, 9 Oct 2023 23:50:40 +0530 Subject: [PATCH 35/36] Update CircularDoublyLinkedList.js --- .../Linked-List/CircularDoublyLinkedList.js | 142 +++++++++--------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index 23709df946..72e266decf 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -5,10 +5,10 @@ class Node { * Creates a new Node with the given element. * @param {*} element - The element to be stored in the node. */ - constructor (element) { - this.element = element - this.next = null - this.prev = null + constructor(element) { + this.element = element; + this.next = null; + this.prev = null; } } @@ -16,33 +16,10 @@ class CircularDoublyLinkedList { /** * Creates an empty Circular Doubly Linked List. */ - constructor () { - this.length = 0 - this.head = null - this.tail = null - } - - /** - * Appends an element to the end of the list. - * @param {*} element - The element to be appended. - */ - append (element) { - const node = new Node(element) - - if (!this.head) { - this.head = node - this.tail = node - node.next = node // Circular reference - node.prev = node // Circular reference - } else { - node.prev = this.tail - node.next = this.head - this.tail.next = node - this.head.prev = node - this.tail = node - } - - this.length++ + constructor() { + this.length = 0; + this.head = null; + this.tail = null; } /** @@ -51,76 +28,91 @@ class CircularDoublyLinkedList { * @param {*} element - The element to be inserted. * @returns {boolean} - True if the insertion was successful, false otherwise. */ - insert (position, element) { + insertAt(position, element) { if (position >= 0 && position <= this.length) { - const node = new Node(element) - let current = this.head - let previous = null - let index = 0 + const node = new Node(element); + let current = this.head; + let previous = null; + let index = 0; if (position === 0) { - node.next = current - node.prev = this.tail - this.head = node - current.prev = node - this.tail.next = node + if (!this.head) { + this.head = node; + this.tail = node; + node.next = node; // Circular reference + node.prev = node; // Circular reference + } else { + node.next = current; + node.prev = this.tail; + this.head = node; + current.prev = node; + this.tail.next = node; + } } else { while (index++ < position) { - previous = current - current = current.next + previous = current; + current = current.next; } - node.next = current - node.prev = previous - previous.next = node - current.prev = node + node.next = current; + node.prev = previous; + previous.next = node; + current.prev = node; if (position === this.length) { - this.tail = node + this.tail = node; } } - this.length++ - return true + this.length++; + return true; } else { - return false + return false; } } + /** + * Appends an element to the end of the list. + * @param {*} element - The element to be appended. + */ + append(element) { + return this.insertAt(this.length, element); + } + /** * Removes and returns the element at the specified position. * @param {number} position - The position of the element to be removed. * @returns {*} - The removed element, or null if the position is invalid. */ - removeAt (position) { + removeAt(position) { if (position >= 0 && position < this.length) { - let current = this.head - let previous = null - let index = 0 + let current = this.head; + let previous = null; + let index = 0; if (position === 0) { - this.head = current.next - this.head.prev = this.tail - this.tail.next = this.head + this.head = current.next; + this.head.prev = this.tail; + this.tail.next = this.head; if (this.length === 1) { - this.tail = null + this.tail = null; } } else { while (index++ < position) { - previous = current - current = current.next + previous = current; + current = current.next; } - previous.next = current.next - current.next.prev = previous + previous.next = current.next; + current.next.prev = previous; if (position === this.length - 1) { - this.tail = previous + this.tail = previous; } } - this.length-- - return current.element + this.length--; + return current.element; } else { - return null + return null; } } @@ -128,21 +120,21 @@ class CircularDoublyLinkedList { * Iterator over the elements in the list. */ *elements() { - let currentNode = this.head - if (!currentNode) return + let currentNode = this.head; + if (!currentNode) return; do { - yield currentNode.element - currentNode = currentNode.next - } while (currentNode !== this.head) + yield currentNode.element; + currentNode = currentNode.next; + } while (currentNode !== this.head); } /** * Checks if the list is empty. * @returns {boolean} - True if the list is empty, false otherwise. */ - isEmpty () { - return this.length === 0 + isEmpty() { + return this.length === 0; } } -export { CircularDoublyLinkedList } +export { CircularDoublyLinkedList }; From 3a2de532e2178427fbd2e53434ca957b21d6de5f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Mon, 9 Oct 2023 23:52:46 +0530 Subject: [PATCH 36/36] Update CircularDoublyLinkedList.test.js --- .../test/CircularDoublyLinkedList.test.js | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js index 944b257347..f2ade52984 100644 --- a/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js @@ -1,4 +1,4 @@ -import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList' +import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList'; describe('CircularDoublyLinkedList', () => { /** @@ -7,52 +7,52 @@ describe('CircularDoublyLinkedList', () => { * @returns {CircularDoublyLinkedList} - The created list. */ function createAndAppend(elements) { - const list = new CircularDoublyLinkedList() - elements.forEach((element) => list.append(element)) - return list + const list = new CircularDoublyLinkedList(); + elements.forEach((element) => list.append(element)); + return list; } - const expectList = (list, expected) => expect([...list.elements()]).toEqual(expected) + const expectList = (list, expected) => expect([...list.elements()]).toEqual(expected); it('append', () => { - const list = createAndAppend([1]) + const list = createAndAppend([1]); - expectList(list, [1]) + expectList(list, [1]); - list.append(2) - expectList(list, [1, 2]) - }) + list.append(2); + expectList(list, [1, 2]); + }); - it('insert', () => { - const list = createAndAppend([1]) + it('insertAt', () => { + const list = createAndAppend([1]); - list.insert(0, 20) - expectList(list, [20, 1]) + list.insertAt(0, 20); + expectList(list, [20, 1]); - list.insert(1, 30) - expectList(list, [20, 30, 1]) - }) + list.insertAt(1, 30); + expectList(list, [20, 30, 1]); + }); it('removeAt', () => { - const list = createAndAppend([10, 40, 30]) + const list = createAndAppend([10, 40, 30]); - list.removeAt(0) - expectList(list, [40, 30]) + list.removeAt(0); + expectList(list, [40, 30]); - list.removeAt(1) - expectList(list, [40]) - }) + list.removeAt(1); + expectList(list, [40]); + }); it('elements', () => { - const list = createAndAppend([10, 20, 30]) - expect([...list.elements()]).toEqual([10, 20, 30]) - }) + const list = createAndAppend([10, 20, 30]); + expect([...list.elements()]).toEqual([10, 20, 30]); + }); it('isEmpty', () => { - const emptyList = new CircularDoublyLinkedList() - expect(emptyList.isEmpty()).toEqual(true) + const emptyList = new CircularDoublyLinkedList(); + expect(emptyList.isEmpty()).toEqual(true); - const nonEmptyList = createAndAppend(['Hello']) - expect(nonEmptyList.isEmpty()).toEqual(false) - }) -}) + const nonEmptyList = createAndAppend(['Hello']); + expect(nonEmptyList.isEmpty()).toEqual(false); + }); +});