|
| 1 | +// Methods - size, head, isEmpty, getElementAt, addAtFirst, add, clean, insertAt, remove, removeData, printData, get, clear |
| 2 | +import { Node } from './SinglyLinkedList.js' |
| 3 | + |
| 4 | +class SinglyCircularLinkedList { |
| 5 | + constructor () { |
| 6 | + this.headNode = null |
| 7 | + this.length = 0 |
| 8 | + } |
| 9 | + |
| 10 | + // Get size of the linkedList |
| 11 | + size = () => this.length |
| 12 | + // Get the headNode data |
| 13 | + head = () => this.headNode?.data || null |
| 14 | + // Check if the linkedList is empty |
| 15 | + isEmpty = () => this.length === 0 |
| 16 | + |
| 17 | + // initiate the node and index |
| 18 | + initiateNodeAndIndex () { |
| 19 | + return { currentNode: this.headNode, currentIndex: 0 } |
| 20 | + } |
| 21 | + |
| 22 | + // get the data specific to an index |
| 23 | + getElementAt (index) { |
| 24 | + if (this.length !== 0 && index >= 0 && index <= this.length) { |
| 25 | + let { currentNode } = this.initiateNodeAndIndex() |
| 26 | + for (let i = 0; i < index && currentNode !== null; i++) { |
| 27 | + currentNode = currentNode.next |
| 28 | + } |
| 29 | + return currentNode |
| 30 | + } |
| 31 | + return undefined |
| 32 | + } |
| 33 | + |
| 34 | + // Add the element in the first position |
| 35 | + addAtFirst (data) { |
| 36 | + const node = new Node(data) |
| 37 | + node.next = this.headNode |
| 38 | + this.headNode = node |
| 39 | + this.length++ |
| 40 | + return this.length |
| 41 | + } |
| 42 | + |
| 43 | + // Add any data to the end of the linkedList |
| 44 | + add (data) { |
| 45 | + if (!this.headNode) { return this.addAtFirst(data) } |
| 46 | + const node = new Node(data) |
| 47 | + // Getting the last node |
| 48 | + const currentNode = this.getElementAt(this.length - 1) |
| 49 | + currentNode.next = node |
| 50 | + node.next = this.headNode |
| 51 | + this.length++ |
| 52 | + return this.length |
| 53 | + } |
| 54 | + |
| 55 | + // insert data at a specific position |
| 56 | + insertAt (index, data) { |
| 57 | + if (index === 0) return this.addAtFirst(data) |
| 58 | + if (index === this.length) return this.add(data) |
| 59 | + if (index < 0 || index > this.length) throw new RangeError(`Index is out of range max ${this.length}`) |
| 60 | + const node = new Node(data) |
| 61 | + const previousNode = this.getElementAt(index - 1) |
| 62 | + node.next = previousNode.next |
| 63 | + previousNode.next = node |
| 64 | + this.length++ |
| 65 | + return this.length |
| 66 | + } |
| 67 | + |
| 68 | + // find the first index of the data |
| 69 | + indexOf (data) { |
| 70 | + let { currentNode } = this.initiateNodeAndIndex() |
| 71 | + // initializing currentIndex as -1 |
| 72 | + let currentIndex = -1 |
| 73 | + while (currentNode) { |
| 74 | + if (currentNode.data === data) { |
| 75 | + return currentIndex + 1 |
| 76 | + } |
| 77 | + currentIndex++ |
| 78 | + currentNode = currentNode.next |
| 79 | + } |
| 80 | + return -1 |
| 81 | + } |
| 82 | + |
| 83 | + // remove the data from the end of the list |
| 84 | + remove () { |
| 85 | + if (this.isEmpty()) return null |
| 86 | + const secondLastNode = this.getElementAt(this.length - 2) |
| 87 | + const removedNode = secondLastNode.next |
| 88 | + secondLastNode.next = this.headNode |
| 89 | + this.length-- |
| 90 | + return removedNode.data || null |
| 91 | + } |
| 92 | + |
| 93 | + // remove the data from the first of the list |
| 94 | + removeFirst () { |
| 95 | + if (this.isEmpty()) return null |
| 96 | + const removedNode = this.headNode |
| 97 | + if (this.length === 1) { |
| 98 | + this.clear() |
| 99 | + return removedNode.data |
| 100 | + } |
| 101 | + const lastNode = this.getElementAt(this.length - 1) |
| 102 | + this.headNode = this.headNode.next |
| 103 | + lastNode.next = this.headNode |
| 104 | + this.length-- |
| 105 | + return removedNode.data || null |
| 106 | + } |
| 107 | + |
| 108 | + // remove the data from the index |
| 109 | + removeAt (index) { |
| 110 | + if (this.isEmpty()) return null |
| 111 | + if (index === 0) return this.removeFirst() |
| 112 | + if (index === this.length) return this.remove() |
| 113 | + if (index < 0 && index > this.length) return null |
| 114 | + const previousNode = this.getElementAt(index - 1) |
| 115 | + const currentNode = previousNode.next |
| 116 | + previousNode.next = currentNode.next |
| 117 | + this.length-- |
| 118 | + return currentNode.data || null |
| 119 | + } |
| 120 | + |
| 121 | + // remove if the data is present |
| 122 | + removeData (data) { |
| 123 | + if (this.isEmpty()) return null |
| 124 | + const index = this.indexOf(data) |
| 125 | + return this.removeAt(index) |
| 126 | + } |
| 127 | + |
| 128 | + // logs the data |
| 129 | + printData (output = value => console.log(value)) { |
| 130 | + let { currentIndex, currentNode } = this.initiateNodeAndIndex() |
| 131 | + |
| 132 | + while (currentNode !== null && currentIndex < this.length) { |
| 133 | + output(currentNode.data) |
| 134 | + currentNode = currentNode.next |
| 135 | + currentIndex++ |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // get the data from the linkedList |
| 140 | + get () { |
| 141 | + let { currentIndex, currentNode } = this.initiateNodeAndIndex() |
| 142 | + const list = [] |
| 143 | + while (currentNode !== null && currentIndex < this.length) { |
| 144 | + list.push(currentNode.data) |
| 145 | + currentNode = currentNode.next |
| 146 | + currentIndex++ |
| 147 | + } |
| 148 | + return list |
| 149 | + } |
| 150 | + |
| 151 | + clear () { |
| 152 | + this.headNode = null |
| 153 | + this.length = 0 |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +export { SinglyCircularLinkedList } |
0 commit comments