From dcdd0d667163018a893493accd1e9e8d3f6c53de Mon Sep 17 00:00:00 2001 From: parhan Date: Sun, 4 Oct 2020 20:34:15 +0700 Subject: [PATCH 1/4] add new data structures (Single Circular Linked List) --- .../Linked-List/SingleCircularLinkedList.js | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Data-Structures/Linked-List/SingleCircularLinkedList.js diff --git a/Data-Structures/Linked-List/SingleCircularLinkedList.js b/Data-Structures/Linked-List/SingleCircularLinkedList.js new file mode 100644 index 0000000000..6cfea9af52 --- /dev/null +++ b/Data-Structures/Linked-List/SingleCircularLinkedList.js @@ -0,0 +1,99 @@ +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() { + let count = 0 + let current = this.head + + while (current !== null && count !== this.size) { + console.log(current.data + '\n') + current = current.next + count++ + } + } +} + +const ll = new SinglyCircularLinkedList() + +ll.insert(10) +ll.insert(20) +ll.insert(30) +ll.insert(40) +ll.insert(50) +ll.insertAt(5, 60) + +ll.remove(5) + +ll.printData() \ No newline at end of file From bf9641956464ce82badad473f0ade537a9e08ecd Mon Sep 17 00:00:00 2001 From: parhan Date: Sun, 4 Oct 2020 20:57:42 +0700 Subject: [PATCH 2/4] fix standard --- Data-Structures/Linked-List/SingleCircularLinkedList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Linked-List/SingleCircularLinkedList.js b/Data-Structures/Linked-List/SingleCircularLinkedList.js index 6cfea9af52..59db7dc0f6 100644 --- a/Data-Structures/Linked-List/SingleCircularLinkedList.js +++ b/Data-Structures/Linked-List/SingleCircularLinkedList.js @@ -96,4 +96,4 @@ ll.insertAt(5, 60) ll.remove(5) -ll.printData() \ No newline at end of file +ll.printData() From b7d138bb2cfc2fa8d6e201e063b0812f616363a8 Mon Sep 17 00:00:00 2001 From: parhan Date: Sun, 4 Oct 2020 21:14:43 +0700 Subject: [PATCH 3/4] fix standard js --- .../Linked-List/SingleCircularLinkedList.js | 99 ------------------- 1 file changed, 99 deletions(-) delete mode 100644 Data-Structures/Linked-List/SingleCircularLinkedList.js diff --git a/Data-Structures/Linked-List/SingleCircularLinkedList.js b/Data-Structures/Linked-List/SingleCircularLinkedList.js deleted file mode 100644 index 59db7dc0f6..0000000000 --- a/Data-Structures/Linked-List/SingleCircularLinkedList.js +++ /dev/null @@ -1,99 +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() { - let count = 0 - let current = this.head - - while (current !== null && count !== this.size) { - console.log(current.data + '\n') - current = current.next - count++ - } - } -} - -const ll = new SinglyCircularLinkedList() - -ll.insert(10) -ll.insert(20) -ll.insert(30) -ll.insert(40) -ll.insert(50) -ll.insertAt(5, 60) - -ll.remove(5) - -ll.printData() From 29b3a053b708c989244f8046762ffb44ee3f7648 Mon Sep 17 00:00:00 2001 From: parhan Date: Sun, 4 Oct 2020 21:17:18 +0700 Subject: [PATCH 4/4] fix code standard --- .../SingleCircularLinkedList.js.js | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Data-Structures/Linked-List/SingleCircularLinkedList.js.js diff --git a/Data-Structures/Linked-List/SingleCircularLinkedList.js.js b/Data-Structures/Linked-List/SingleCircularLinkedList.js.js new file mode 100644 index 0000000000..fd50253ad9 --- /dev/null +++ b/Data-Structures/Linked-List/SingleCircularLinkedList.js.js @@ -0,0 +1,97 @@ +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 () { + let count = 0 + let current = this.head + + while (current !== null && count !== this.size) { + console.log(current.data + '\n') + current = current.next + count++ + } + } +} + +const ll = new SinglyCircularLinkedList() + +ll.insert(10) +ll.insert(20) +ll.insert(30) +ll.insert(40) +ll.insert(50) +ll.insertAt(5, 60) +ll.remove(5) +ll.printData()