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/6] 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 52c07137370ab275adea3c855bb8471b8e572aa1 Mon Sep 17 00:00:00 2001 From: Rahul Raj <43320722+rraj798@users.noreply.github.com> Date: Sat, 9 Apr 2022 17:26:00 +0530 Subject: [PATCH 2/6] Update and rename SingleCircularLinkedList.js.js to SinglyCircularLinkedList.js --- .../SingleCircularLinkedList.js.js | 88 ---------- .../Linked-List/SinglyCircularLinkedList.js | 159 ++++++++++++++++++ 2 files changed, 159 insertions(+), 88 deletions(-) delete mode 100644 Data-Structures/Linked-List/SingleCircularLinkedList.js.js create mode 100644 Data-Structures/Linked-List/SinglyCircularLinkedList.js diff --git a/Data-Structures/Linked-List/SingleCircularLinkedList.js.js b/Data-Structures/Linked-List/SingleCircularLinkedList.js.js deleted file mode 100644 index 2ba3541916..0000000000 --- a/Data-Structures/Linked-List/SingleCircularLinkedList.js.js +++ /dev/null @@ -1,88 +0,0 @@ -class Node { - constructor (data, next = null) { - this.data = data - this.next = next - } -} - -class SinglyCircularLinkedList { - constructor () { - this.head = null - this.size = 0 - } - - insert (data) { - const node = new Node(data) - - if (!this.head) { - node.next = node - this.head = node - this.size++ - } else { - node.next = this.head - - let current = this.head - - while (current.next.data !== this.head.data) { - current = current.next - } - - current.next = node - this.size++ - } - } - - insertAt (index, data) { - const node = new Node(data) - - if (index < 0 || index > this.size) return - - if (index === 0) { - this.head = node - this.size = 1 - return - } - - let previous - let count = 0 - let current = this.head - - while (count < index) { - previous = current - current = current.next - count++ - } - - node.next = current - previous.next = node - this.size++ - } - - remove () { - if (!this.head) return - - let prev - let current = this.head - - while (current.next !== this.head) { - prev = current - current = current.next - } - - prev.next = this.head - this.size-- - } - - printData (output = value => console.log(value)) { - let count = 0 - let current = this.head - - while (current !== null && count < this.size) { - output(current.data) - current = current.next - count++ - } - } -} - -export { SinglyCircularLinkedList } diff --git a/Data-Structures/Linked-List/SinglyCircularLinkedList.js b/Data-Structures/Linked-List/SinglyCircularLinkedList.js new file mode 100644 index 0000000000..619222529a --- /dev/null +++ b/Data-Structures/Linked-List/SinglyCircularLinkedList.js @@ -0,0 +1,159 @@ +// Methods - size, head, isEmpty, isAvailable, getElementAt, addAtFirst, add, clean, insertAt, remove, removeData, printData, get +import { Node } from './SinglyLinkedList.js' + +class SinglyCircularLinkedList { + constructor () { + this.headNode = null + this.length = 0 + } + + // Get size of the linkedList + size = () => this.length + // Get the headNode data + head = () => this.headNode?.data || null + // Check if the linkedList is empty + isEmpty = () => this.length === 0 + // Check if the data is available or not + isAvailable = (data) => this.indexOf(data) !== -1 + + // initiate the node and index + initiateNodeAndIndex () { + return { currentNode: this.headNode, currentIndex: 0 } + } + + // get the data specific to an index + getElementAt (index) { + if (this.length !== 0 && index >= 0 && index <= this.length) { + let { currentNode } = this.initiateNodeAndIndex() + for (let i = 0; i < index && currentNode !== null; i++) { + currentNode = currentNode.next + } + return currentNode + } + return undefined + } + + // Add the element in the first position + addAtFirst (data) { + const node = new Node(data) + node.next = this.headNode + this.headNode = node + this.length++ + return this.length + } + + // Add any data to the end of the linkedList + add (data) { + if (!this.headNode) { return this.addAtFirst(data) } + const node = new Node(data) + // Getting the last node + const currentNode = this.getElementAt(this.length - 1) + currentNode.next = node + node.next = this.headNode + this.length++ + return this.length + } + + // insert data at a specific position + insertAt (index, data) { + if (index === 0) return this.addAtFirst(data) + if (index === this.length) return this.add(data) + if (index < 0 || index > this.length) throw new RangeError(`Index is out of range max ${this.length}`) + const node = new Node(data) + const previousNode = this.getElementAt(index - 1) + node.next = previousNode.next + previousNode.next = node + this.length++ + return this.length + } + + // find the first index of the data + indexOf (data) { + let { currentNode } = this.initiateNodeAndIndex() + // initializing currentIndex as -1 + let currentIndex = -1 + while (currentNode) { + if (currentNode.data === data) { + return currentIndex + 1 + } + currentIndex++ + currentNode = currentNode.next + } + return -1 + } + + // remove the data from the end of the list + remove () { + if (this.isEmpty()) return null + const secondLastNode = this.getElementAt(this.length - 2) + const removedNode = secondLastNode.next + secondLastNode.next = this.headNode + this.length-- + return removedNode.data || null + } + + // remove the data from the first of the list + removeFirst () { + if (this.isEmpty()) return null + const removedNode = this.headNode + if (this.length === 1) { + this.clear() + return removedNode.data + } + const lastNode = this.getElementAt(this.length - 1) + this.headNode = this.headNode.next + lastNode.next = this.headNode + this.length-- + return removedNode.data || null + } + + // remove the data from the index + removeAt (index) { + if (this.isEmpty()) return null + if (index === 0) return this.removeFirst() + if (index === this.length) return this.remove() + if (index < 0 && index > this.length) return null + const previousNode = this.getElementAt(index - 1) + const currentNode = previousNode.next + previousNode.next = currentNode.next + this.length-- + return currentNode.data || null + } + + // remove if the data is present + removeData (data) { + if (this.isEmpty()) return null + const index = this.indexOf(data) + return this.removeAt(index) + } + + // logs the data + printData (output = value => console.log(value)) { + let { currentIndex, currentNode } = this.initiateNodeAndIndex() + + while (currentNode !== null && currentIndex < this.length) { + output(currentNode.data) + currentNode = currentNode.next + currentIndex++ + } + } + + // get the data from the linkedList + get () { + let { currentIndex, currentNode } = this.initiateNodeAndIndex() + const list = [] + while (currentNode !== null && currentIndex < this.length) { + list.push(currentNode.data) + currentNode = currentNode.next + currentIndex++ + } + return list + } + + clear () { + this.headNode = null + this.length = 0 + } +} + +export { SinglyCircularLinkedList } From 2b79cdc8efd177e099c52abae9e86286c0c02cd6 Mon Sep 17 00:00:00 2001 From: Rahul Raj <43320722+rraj798@users.noreply.github.com> Date: Sat, 9 Apr 2022 17:27:58 +0530 Subject: [PATCH 3/6] Test Case creation for SinglyCircularLinkedList --- .../test/SinglyCircularLinkedList.test.js | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js diff --git a/Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js b/Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js new file mode 100644 index 0000000000..2a32846011 --- /dev/null +++ b/Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js @@ -0,0 +1,158 @@ +import { SinglyCircularLinkedList } from '../SinglyCircularLinkedList' + +describe('SinglyCircularLinkedList', () => { + let list + beforeEach(() => { + list = new SinglyCircularLinkedList() + }) + it('Check get', () => { + expect(list.get()).toEqual([]) + expect(list.add(1)).toEqual(1) + expect(list.get()).toEqual([1]) + expect(list.add(5)).toEqual(2) + expect(list.get()).toEqual([1, 5]) + }) + + it('Check size', () => { + expect(list.size()).toEqual(0) + expect(list.add(1)).toEqual(1) + expect(list.add(1)).toEqual(2) + expect(list.size()).toEqual(2) + }) + + it('Check head', () => { + expect(list.head()).toEqual(null) + expect(list.add(1)).toEqual(1) + expect(list.head()).toEqual(1) + expect(list.add(1)).toEqual(2) + expect(list.head()).toEqual(1) + expect(list.addAtFirst(100)).toEqual(3) + expect(list.head()).toEqual(100) + expect(list.insertAt(0, 500)).toEqual(4) + expect(list.head()).toEqual(500) + list.clear() + expect(list.head()).toEqual(null) + }) + + it('Check isEmpty', () => { + expect(list.isEmpty()).toEqual(true) + expect(list.add(1)).toEqual(1) + expect(list.add(1)).toEqual(2) + expect(list.isEmpty()).toEqual(false) + }) + + it('Check isAvailable', () => { + expect(list.isAvailable(200)).toBeFalsy() + list.add(1) + list.add(5) + list.add(7) + list.add(9) + list.add(0) + expect(list.isAvailable(5)).toBeTruthy() + expect(list.isAvailable(90)).toBeFalsy() + }) + + it('Check getElementAt', () => { + list.add(100) + list.add(200) + list.add(300) + list.add(500) + list.add(900) + + expect(list.getElementAt(1).data).toEqual(200) + expect(list.getElementAt(3).data).toEqual(500) + }) + + it('Check addAtFirst', () => { + list.add(1) + list.add(5) + list.add(7) + list.add(9) + list.add(0) + expect(list.get()).toEqual([1, 5, 7, 9, 0]) + list.addAtFirst(100) + expect(list.get()).toEqual([100, 1, 5, 7, 9, 0]) + }) + + it('Check add', () => { + list.add(1) + list.add(5) + list.add(7) + list.add(9) + list.add(0) + expect(list.get()).toEqual([1, 5, 7, 9, 0]) + list.add(100) + expect(list.get()).toEqual([1, 5, 7, 9, 0, 100]) + }) + + it('Check insertAt', () => { + expect(list.insertAt(0, 100)).toEqual(1) + expect(list.get()).toEqual([100]) + expect(list.insertAt(0, 200)).toEqual(2) + expect(list.get()).toEqual([200, 100]) + expect(list.insertAt(2, 300)).toEqual(3) + expect(list.get()).toEqual([200, 100, 300]) + }) + + it('Checks indexOf', () => { + expect(list.indexOf(200)).toEqual(-1) + list.add(100) + list.add(200) + list.add(300) + list.add(500) + list.add(900) + expect(list.indexOf(200)).toEqual(1) + }) + + it('Check remove', () => { + expect(list.remove()).toEqual(null) + list.add(100) + list.add(200) + list.add(300) + list.add(500) + list.add(900) + expect(list.get()).toEqual([100, 200, 300, 500, 900]) + const removedData = list.remove() + expect(removedData).toEqual(900) + expect(list.get()).toEqual([100, 200, 300, 500]) + }) + + it('Check removeFirst', () => { + expect(list.removeFirst()).toEqual(null) + list.add(100) + list.add(200) + list.add(300) + list.add(500) + list.add(900) + expect(list.get()).toEqual([100, 200, 300, 500, 900]) + const removedData = list.removeFirst() + expect(removedData).toEqual(100) + expect(list.get()).toEqual([200, 300, 500, 900]) + }) + + it('Check removeAt', () => { + expect(list.removeAt(1)).toEqual(null) + list.add(100) + list.add(200) + list.add(300) + list.add(500) + list.add(900) + expect(list.get()).toEqual([100, 200, 300, 500, 900]) + const removedData = list.removeAt(2) + expect(removedData).toEqual(300) + expect(list.get()).toEqual([100, 200, 500, 900]) + }) + + it('Check removeData', () => { + expect(list.removeData(100)).toEqual(null) + list.add(100) + list.add(200) + list.add(300) + list.add(500) + list.add(900) + expect(list.get()).toEqual([100, 200, 300, 500, 900]) + const removedData = list.removeData(200) + expect(removedData).toEqual(200) + expect(list.get()).toEqual([100, 300, 500, 900]) + }) +}) From 698976d768f9685f3379f772f491a76c1814f1b3 Mon Sep 17 00:00:00 2001 From: Rahul Raj <43320722+rraj798@users.noreply.github.com> Date: Sat, 9 Apr 2022 18:00:09 +0530 Subject: [PATCH 4/6] Update SinglyCircularLinkedList.js --- Data-Structures/Linked-List/SinglyCircularLinkedList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Linked-List/SinglyCircularLinkedList.js b/Data-Structures/Linked-List/SinglyCircularLinkedList.js index 619222529a..4ba982866c 100644 --- a/Data-Structures/Linked-List/SinglyCircularLinkedList.js +++ b/Data-Structures/Linked-List/SinglyCircularLinkedList.js @@ -1,4 +1,4 @@ -// Methods - size, head, isEmpty, isAvailable, getElementAt, addAtFirst, add, clean, insertAt, remove, removeData, printData, get +// Methods - size, head, isEmpty, isAvailable, getElementAt, addAtFirst, add, clean, insertAt, remove, removeData, printData, get, clear import { Node } from './SinglyLinkedList.js' class SinglyCircularLinkedList { From 7aca8c37c3f8972fc1146441818ee44d1e15011d Mon Sep 17 00:00:00 2001 From: Rahul Raj <43320722+rraj798@users.noreply.github.com> Date: Sun, 10 Apr 2022 14:18:00 +0530 Subject: [PATCH 5/6] Update SinglyCircularLinkedList.test.js --- .../Linked-List/test/SinglyCircularLinkedList.test.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js b/Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js index 2a32846011..8f5087c537 100644 --- a/Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js +++ b/Data-Structures/Linked-List/test/SinglyCircularLinkedList.test.js @@ -41,17 +41,6 @@ describe('SinglyCircularLinkedList', () => { expect(list.isEmpty()).toEqual(false) }) - it('Check isAvailable', () => { - expect(list.isAvailable(200)).toBeFalsy() - list.add(1) - list.add(5) - list.add(7) - list.add(9) - list.add(0) - expect(list.isAvailable(5)).toBeTruthy() - expect(list.isAvailable(90)).toBeFalsy() - }) - it('Check getElementAt', () => { list.add(100) list.add(200) From a3182d116e9391ae61024bb2acf3121819db3dc0 Mon Sep 17 00:00:00 2001 From: Rahul Raj <43320722+rraj798@users.noreply.github.com> Date: Sun, 10 Apr 2022 14:18:25 +0530 Subject: [PATCH 6/6] Update SinglyCircularLinkedList.js --- Data-Structures/Linked-List/SinglyCircularLinkedList.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Data-Structures/Linked-List/SinglyCircularLinkedList.js b/Data-Structures/Linked-List/SinglyCircularLinkedList.js index 4ba982866c..e1a8085f82 100644 --- a/Data-Structures/Linked-List/SinglyCircularLinkedList.js +++ b/Data-Structures/Linked-List/SinglyCircularLinkedList.js @@ -1,4 +1,4 @@ -// Methods - size, head, isEmpty, isAvailable, getElementAt, addAtFirst, add, clean, insertAt, remove, removeData, printData, get, clear +// Methods - size, head, isEmpty, getElementAt, addAtFirst, add, clean, insertAt, remove, removeData, printData, get, clear import { Node } from './SinglyLinkedList.js' class SinglyCircularLinkedList { @@ -13,8 +13,6 @@ class SinglyCircularLinkedList { head = () => this.headNode?.data || null // Check if the linkedList is empty isEmpty = () => this.length === 0 - // Check if the data is available or not - isAvailable = (data) => this.indexOf(data) !== -1 // initiate the node and index initiateNodeAndIndex () {