From 7a01994338199467594e018fcd833548b80f69c2 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 16 Feb 2022 09:21:07 +0000 Subject: [PATCH 1/4] Auto-update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 46aa5907c6..927b3b1bde 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -255,6 +255,7 @@ * [UnionFind](https://github.com/TheAlgorithms/Javascript/blob/master/Search/UnionFind.js) ## Sorts + * [AlphaNumericalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/AlphaNumericalSort.js) * [BeadSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BeadSort.js) * [BogoSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BogoSort.js) * [BubbleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BubbleSort.js) From e362278bfb25cf280403a2376b912e37d0881658 Mon Sep 17 00:00:00 2001 From: Rahul Raj Date: Thu, 7 Apr 2022 23:01:35 +0530 Subject: [PATCH 2/4] Changes on SinglyLinkedList --- .../Linked-List/SinglyLinkedList.js | 173 +++++++++--------- .../Linked-List/test/SinglyLinkedList.test.js | 54 +++--- 2 files changed, 119 insertions(+), 108 deletions(-) diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index 9a003f09a0..2be8141ab2 100644 --- a/Data-Structures/Linked-List/SinglyLinkedList.js +++ b/Data-Structures/Linked-List/SinglyLinkedList.js @@ -6,7 +6,7 @@ * a singly linked list. */ -// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, get +// Methods - size, head, addLast, addFirst, addAt, removeFirst, removeLast, remove, removeAt, indexOf, isEmpty, elementAt, get, clean class Node { constructor (data) { @@ -21,6 +21,10 @@ class LinkedList { this.length = 0 } + initiateNodeandIndex () { + return { currentNode: this.headNode, currentIndex: 0 } + } + // Returns length size () { return this.length @@ -28,29 +32,32 @@ class LinkedList { // Returns the head head () { - return this.headNode ? this.headNode.data : null + return this.headNode?.data || null + } + + // Return if the list is empty + isEmpty () { + return this.length === 0 } // add a node at last it to linklist addLast (element) { - const node = new Node(element) - // Check if its the first element if (this.headNode === null) { - this.headNode = node - } else { - let currentNode = this.headNode - - // Loop till there is a node present in the list - while (currentNode.next) { - currentNode = currentNode.next - } + return this.addFirst(element) + } + let { currentNode } = this.initiateNodeandIndex() - // Adding node at the end of the list - currentNode.next = node + // Loop till there is a node present in the list + while (currentNode.next) { + currentNode = currentNode.next } - this.length++ // Increment the length + const node = new Node(element) + // Adding node at the end of the list and increase the legnth + currentNode.next = node + this.length++ + return this.size() } // add a node at first it to linklist @@ -58,75 +65,72 @@ class LinkedList { const node = new Node(element) node.next = this.headNode this.headNode = node - this.length++ // Increment the length + this.length++ + return this.size() } // remove the first from the linklist removeFirst () { + const removedNode = this.headNode if (this.length > 0) { this.headNode = this.headNode.next this.length-- } + console.log(removedNode.data) + return removedNode?.data } // remove the last from the linklist removeLast () { + if (this.isEmpty()) return null if (this.length === 1) { - this.headNode = null - this.length-- - } else if (this.length > 1) { - let index = 0 - let currentNode = this.headNode - while (index !== this.length - 2) { - index++ - currentNode = currentNode.next - } - - currentNode.next = null - this.length-- + return this.removeFirst() + } + let { currentIndex, currentNode } = this.initiateNodeandIndex() + while (currentIndex !== this.length - 2) { + currentIndex++ + currentNode = currentNode.next } + const removedNode = currentNode.next + currentNode.next = null + this.length-- + return removedNode.data } // Removes the node with the value as param remove (element) { - let currentNode = this.headNode - + if (this.isEmpty()) return null + let { currentNode } = this.initiateNodeandIndex() + let removedNode = null // Check if the head node is the element to remove if (currentNode.data === element) { - this.headNode = currentNode.next - } else { - // Check which node is the node to remove - while (currentNode && currentNode.next) { - if (currentNode.next.data === element) { - currentNode.next = currentNode.next.next - break - } - currentNode = currentNode.next + return this.removeFirst() + } + // Check which node is the node to remove + while (currentNode?.next) { + if (currentNode.next.data === element) { + removedNode = currentNode.next + currentNode.next = currentNode.next.next + this.length-- + break } + currentNode = currentNode.next } - - this.length-- // Decrementing the length - } - - // Return if the list is empty - isEmpty () { - return this.length === 0 + return removedNode?.data || null } // Returns the index of the element passed as param otherwise -1 indexOf (element) { - let currentNode = this.headNode - let index = 0 + let { currentIndex, currentNode } = this.initiateNodeandIndex() while (currentNode) { // Checking if the node is the element we are searching for if (currentNode.data === element) { - return index + return currentIndex } currentNode = currentNode.next - index++ + currentIndex++ } - return -1 } @@ -135,10 +139,9 @@ class LinkedList { if (index >= this.length || index < 0) { throw new RangeError('Out of Range index') } - let currentNode = this.headNode - let count = 0 - while (count < index) { - count++ + let { currentIndex, currentNode } = this.initiateNodeandIndex() + while (currentIndex < index) { + currentIndex++ currentNode = currentNode.next } return currentNode.data @@ -146,64 +149,60 @@ class LinkedList { // Adds the element at specified index addAt (index, element) { - const node = new Node(element) - // Check if index is out of bounds of list if (index > this.length || index < 0) { throw new RangeError('Out of Range index') } + if (index === 0) return this.addFirst(element) + if (index === this.length) return this.addLast(element) + let { currentIndex, currentNode } = this.initiateNodeandIndex() + const node = new Node(element) - let currentNode = this.headNode - let currentIndex = 0 - // Check if index is the start of list - if (index === 0) { - node.next = currentNode - this.headNode = node - } else { - while (currentIndex !== index - 1) { - currentIndex++ - currentNode = currentNode.next - } - - // Adding the node at specified index - const temp = currentNode.next - currentNode.next = node - node.next = temp + while (currentIndex !== index - 1) { + currentIndex++ + currentNode = currentNode.next } + // Adding the node at specified index + const tempNode = currentNode.next + currentNode.next = node + node.next = tempNode // Incrementing the length this.length++ + return this.size() } // Removes the node at specified index removeAt (index) { - let currentNode = this.headNode - let currentIndex = 0 - // Check if index is present in list if (index < 0 || index >= this.length) { throw new RangeError('Out of Range index') } + if (index === 0) return this.removeFirst() + if (index === this.length) return this.removeLast() - // Check if element is the first element - if (index === 0) { - this.headNode = currentNode.next - } else { - while (currentIndex !== index - 1) { - currentIndex++ - currentNode = currentNode.next - } - currentNode.next = currentNode.next.next + let { currentIndex, currentNode } = this.initiateNodeandIndex() + while (currentIndex !== index - 1) { + currentIndex++ + currentNode = currentNode.next } - + const removedNode = currentNode.next + currentNode.next = currentNode.next.next // Decrementing the length this.length-- + return removedNode.data + } + + // make the linkedList Empty + clean () { + this.headNode = null + this.length = 0 } // Method to get the LinkedList get () { const list = [] - let currentNode = this.headNode + let { currentNode } = this.initiateNodeandIndex() while (currentNode) { list.push(currentNode.data) currentNode = currentNode.next diff --git a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js index 33f8fd2895..1b85031242 100644 --- a/Data-Structures/Linked-List/test/SinglyLinkedList.test.js +++ b/Data-Structures/Linked-List/test/SinglyLinkedList.test.js @@ -5,38 +5,38 @@ describe('SinglyLinkedList', () => { const list = new LinkedList() expect(list.get()).toEqual([]) - list.addLast(1) + expect(list.addLast(1)).toEqual(1) expect(list.get()).toEqual([1]) - list.addLast(2) - expect(list.get()).toEqual([1, 2]) + expect(list.addLast(5)).toEqual(2) + expect(list.get()).toEqual([1, 5]) }) it('Check addFirst', () => { const list = new LinkedList() expect(list.get()).toEqual([]) - list.addFirst(1) + expect(list.addFirst(1)).toEqual(1) expect(list.get()).toEqual([1]) - list.addFirst(2) - expect(list.get()).toEqual([2, 1]) + expect(list.addFirst(5)).toEqual(2) + expect(list.get()).toEqual([5, 1]) }) it('Check addAt', () => { const list = new LinkedList() expect(list.get()).toEqual([]) - list.addAt(0, 10) + expect(list.addAt(0, 10)).toEqual(1) expect(list.get()).toEqual([10]) - list.addAt(1, 20) + expect(list.addAt(1, 20)).toEqual(2) expect(list.get()).toEqual([10, 20]) - list.addAt(1, 30) + expect(list.addAt(1, 30)).toEqual(3) expect(list.get()).toEqual([10, 30, 20]) - list.addAt(3, 40) + expect(list.addAt(3, 40)).toEqual(4) expect(list.get()).toEqual([10, 30, 20, 40]) }) @@ -46,10 +46,10 @@ describe('SinglyLinkedList', () => { list.addLast(2) expect(list.get()).toEqual([1, 2]) - list.removeLast() + expect(list.removeLast()).toEqual(2) expect(list.get()).toEqual([1]) - list.removeLast() + expect(list.removeLast()).toEqual(1) expect(list.get()).toEqual([]) }) @@ -59,10 +59,10 @@ describe('SinglyLinkedList', () => { list.addLast(2) expect(list.get()).toEqual([1, 2]) - list.removeFirst() + expect(list.removeFirst()).toEqual(1) expect(list.get()).toEqual([2]) - list.removeFirst() + expect(list.removeFirst()).toEqual(2) expect(list.get()).toEqual([]) }) @@ -75,13 +75,13 @@ describe('SinglyLinkedList', () => { list.addLast(50) expect(list.get()).toEqual([10, 20, 30, 40, 50]) - list.removeAt(0) + expect(list.removeAt(0)).toEqual(10) expect(list.get()).toEqual([20, 30, 40, 50]) - list.removeAt(3) + expect(list.removeAt(3)).toEqual(50) expect(list.get()).toEqual([20, 30, 40]) - list.removeAt(1) + expect(list.removeAt(1)).toEqual(30) expect(list.get()).toEqual([20, 40]) }) @@ -92,11 +92,11 @@ describe('SinglyLinkedList', () => { list.addLast(30) expect(list.get()).toEqual([10, 20, 30]) - list.remove(20) - expect(list.get()).toEqual([10, 30]) + expect(list.remove(10)).toEqual(10) + expect(list.get()).toEqual([20, 30]) - list.remove(30) - expect(list.get()).toEqual([10]) + expect(list.remove(100)).toEqual(null) + expect(list.get()).toEqual([20, 30]) }) it('Check indexOf', () => { @@ -163,4 +163,16 @@ describe('SinglyLinkedList', () => { list.removeFirst() expect(list.size()).toBe(1) }) + + it('Cleans the linkedList', () => { + const list = new LinkedList() + list.addLast(10) + list.addLast(20) + list.addLast(30) + list.addLast(40) + list.addLast(50) + expect(list.size()).toEqual(5) + list.clean() + expect(list.isEmpty()).toBe(true) + }) }) From 0a1efad67c826c7172ba9fe03d88eaccc860f122 Mon Sep 17 00:00:00 2001 From: Rahul Raj Date: Thu, 7 Apr 2022 23:13:37 +0530 Subject: [PATCH 3/4] Spelling change --- Data-Structures/Linked-List/SinglyLinkedList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index 8f1c966039..16a41d8ce7 100644 --- a/Data-Structures/Linked-List/SinglyLinkedList.js +++ b/Data-Structures/Linked-List/SinglyLinkedList.js @@ -54,7 +54,7 @@ class LinkedList { } const node = new Node(element) - // Adding node at the end of the list and increase the legnth + // Adding node at the end of the list and increase the length currentNode.next = node this.length++ return this.size() From e214d33c4737017c42e65c08fe38180d572171ee Mon Sep 17 00:00:00 2001 From: Rahul Raj Date: Fri, 8 Apr 2022 20:24:02 +0530 Subject: [PATCH 4/4] Added comment for initiateNodeAndIndex() --- .../Linked-List/SinglyLinkedList.js | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Data-Structures/Linked-List/SinglyLinkedList.js b/Data-Structures/Linked-List/SinglyLinkedList.js index 16a41d8ce7..378540c2c8 100644 --- a/Data-Structures/Linked-List/SinglyLinkedList.js +++ b/Data-Structures/Linked-List/SinglyLinkedList.js @@ -21,7 +21,8 @@ class LinkedList { this.length = 0 } - initiateNodeandIndex () { + // initiates the currentNode and currentIndex and return as an object + initiateNodeAndIndex () { return { currentNode: this.headNode, currentIndex: 0 } } @@ -46,7 +47,7 @@ class LinkedList { if (this.headNode === null) { return this.addFirst(element) } - let { currentNode } = this.initiateNodeandIndex() + let { currentNode } = this.initiateNodeAndIndex() // Loop till there is a node present in the list while (currentNode.next) { @@ -86,7 +87,7 @@ class LinkedList { if (this.length === 1) { return this.removeFirst() } - let { currentIndex, currentNode } = this.initiateNodeandIndex() + let { currentIndex, currentNode } = this.initiateNodeAndIndex() while (currentIndex !== this.length - 2) { currentIndex++ currentNode = currentNode.next @@ -100,7 +101,7 @@ class LinkedList { // Removes the node with the value as param remove (element) { if (this.isEmpty()) return null - let { currentNode } = this.initiateNodeandIndex() + let { currentNode } = this.initiateNodeAndIndex() let removedNode = null // Check if the head node is the element to remove if (currentNode.data === element) { @@ -121,7 +122,7 @@ class LinkedList { // Returns the index of the element passed as param otherwise -1 indexOf (element) { - let { currentIndex, currentNode } = this.initiateNodeandIndex() + let { currentIndex, currentNode } = this.initiateNodeAndIndex() while (currentNode) { // Checking if the node is the element we are searching for @@ -139,7 +140,7 @@ class LinkedList { if (index >= this.length || index < 0) { throw new RangeError('Out of Range index') } - let { currentIndex, currentNode } = this.initiateNodeandIndex() + let { currentIndex, currentNode } = this.initiateNodeAndIndex() while (currentIndex < index) { currentIndex++ currentNode = currentNode.next @@ -155,7 +156,7 @@ class LinkedList { } if (index === 0) return this.addFirst(element) if (index === this.length) return this.addLast(element) - let { currentIndex, currentNode } = this.initiateNodeandIndex() + let { currentIndex, currentNode } = this.initiateNodeAndIndex() const node = new Node(element) while (currentIndex !== index - 1) { @@ -181,7 +182,7 @@ class LinkedList { if (index === 0) return this.removeFirst() if (index === this.length) return this.removeLast() - let { currentIndex, currentNode } = this.initiateNodeandIndex() + let { currentIndex, currentNode } = this.initiateNodeAndIndex() while (currentIndex !== index - 1) { currentIndex++ currentNode = currentNode.next @@ -202,7 +203,7 @@ class LinkedList { // Method to get the LinkedList get () { const list = [] - let { currentNode } = this.initiateNodeandIndex() + let { currentNode } = this.initiateNodeAndIndex() while (currentNode) { list.push(currentNode.data) currentNode = currentNode.next @@ -213,7 +214,7 @@ class LinkedList { // Method to iterate over the LinkedList iterator () { - let currentNode = this.headNode + let { currentNode } = this.initiateNodeAndIndex() if (currentNode === null) return -1 const iterate = function * () { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy