|
| 1 | +/** |
| 2 | + * @author Rashik Ansar |
| 3 | + * |
| 4 | + * Implementation of singly linked list |
| 5 | + * Singly linked list is a linear data strucutre |
| 6 | + */ |
| 7 | + |
| 8 | +class Node { |
| 9 | + constructor(data) { |
| 10 | + this.data = data; |
| 11 | + this.next = null; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class SinglyLinkedList { |
| 16 | + constructor() { |
| 17 | + this.head = null; |
| 18 | + this.tail = null; |
| 19 | + this.length = 0; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Adding new node as a tail of the linked list |
| 24 | + * @param {any} data This dataue is added at the end of list |
| 25 | + * @returns {SinglyLinkedList} LinkedList after adding new node as tail |
| 26 | + */ |
| 27 | + push(data) { |
| 28 | + let temp = new Node(data); |
| 29 | + if (!this.head) { |
| 30 | + this.head = temp; |
| 31 | + this.tail = this.head; // temp |
| 32 | + } else { |
| 33 | + this.tail.next = temp; |
| 34 | + this.tail = temp; |
| 35 | + } |
| 36 | + this.length++; |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Adding new node as a head of the linked list |
| 42 | + * @param {any} data This dataue is added at the beginning of the list |
| 43 | + * @returns {SinglyLinkedList} LinkedList after adding a new node as head |
| 44 | + */ |
| 45 | + unshift(data) { |
| 46 | + let temp = new Node(data); |
| 47 | + if (!this.head) { |
| 48 | + this.head = temp; |
| 49 | + this.tail = this.head; |
| 50 | + } else { |
| 51 | + temp.next = this.head; |
| 52 | + this.head = temp; |
| 53 | + } |
| 54 | + this.length++; |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Adding a node to the linkedList at specified position |
| 60 | + * @param {number} index Position at which new node to insert |
| 61 | + * @param {any} data dataue in the new node |
| 62 | + * @returns {SinglyLinkedList} LinkedList after inserting a new node |
| 63 | + */ |
| 64 | + insert(index, data) { |
| 65 | + if (index < 0 || index > this.length) { |
| 66 | + throw Error('Given index is out of range'); |
| 67 | + } |
| 68 | + if (index === this.length) { |
| 69 | + return this.push(data); |
| 70 | + } |
| 71 | + if (index === 0) { |
| 72 | + return this.unshift(data); |
| 73 | + } |
| 74 | + let insertNode = new Node(data); |
| 75 | + let previous = this.get(index - 1); |
| 76 | + let temp = previous.next; |
| 77 | + previous.next = insertNode; |
| 78 | + insertNode.next = temp; |
| 79 | + this.length++; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Removes the node at the end of linked list(tail of linked list) |
| 85 | + * @returns {Node} the node which is going to pop |
| 86 | + */ |
| 87 | + pop() { |
| 88 | + if (!this.head) { |
| 89 | + throw Error( |
| 90 | + 'UNDERFLOW :::: LinkedList is empty, there is nothing to remove' |
| 91 | + ); |
| 92 | + } |
| 93 | + let current = this.head; |
| 94 | + let temp = current; |
| 95 | + while (current.next) { |
| 96 | + temp = current; |
| 97 | + current = current.next; |
| 98 | + } |
| 99 | + this.tail = temp; |
| 100 | + this.tail.next = null; |
| 101 | + this.length--; |
| 102 | + this.emptyListCheck(); |
| 103 | + return current; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Removes the node from the beginnig of linked list(head of linked list) |
| 108 | + * @returns {Node} the node which is going to shift |
| 109 | + */ |
| 110 | + shift() { |
| 111 | + if (!this.head) { |
| 112 | + throw Error( |
| 113 | + 'UNDERFLOW :::: LinkedList is empty, there is nothing to remove' |
| 114 | + ); |
| 115 | + } |
| 116 | + let current = this.head; |
| 117 | + this.head = current.next; |
| 118 | + this.length--; |
| 119 | + this.emptyListCheck(); |
| 120 | + return current; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Removes a node from the linkedList at specified position |
| 125 | + * @param {number} index |
| 126 | + * @returns {Node} Node which is removed from LinkedList |
| 127 | + */ |
| 128 | + remove(index) { |
| 129 | + if (index < 0 || index > this.length) { |
| 130 | + throw Error('Given index is out of range'); |
| 131 | + } |
| 132 | + if (index === this.length - 1) { |
| 133 | + return this.pop(); |
| 134 | + } |
| 135 | + if (index === 0) { |
| 136 | + return this.shift(); |
| 137 | + } |
| 138 | + let previous = this.get(index - 1); |
| 139 | + let temp = previous.next; |
| 140 | + previous.next = temp.next; |
| 141 | + this.length--; |
| 142 | + return temp; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Retrieve the node at specified index |
| 147 | + * @param {number} index Index of the node |
| 148 | + * @returns {Node} LinkedList Node at specified index |
| 149 | + */ |
| 150 | + get(index) { |
| 151 | + if (index < 0 || index >= this.length) { |
| 152 | + throw Error('Given index is out of range'); |
| 153 | + } |
| 154 | + let counter = 0; |
| 155 | + let current = this.head; |
| 156 | + while (counter !== index) { |
| 157 | + current = current.next; |
| 158 | + counter++; |
| 159 | + } |
| 160 | + return current; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Change the data of node at specified index |
| 165 | + * @param {number} index Index of the node |
| 166 | + * @param {any} data data replaces the current data at given index |
| 167 | + * @returns {SinglyLinkedList} LinkedList |
| 168 | + */ |
| 169 | + set(index, data) { |
| 170 | + // Here error checking will be done by the get method itself |
| 171 | + // No need to specify explicitly |
| 172 | + let existedNode = this.get(index); |
| 173 | + if (existedNode) { |
| 174 | + existedNode.data = data; |
| 175 | + return this; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Reversing the Linked list |
| 181 | + * @returns {SinglyLinkedList} LinkedList |
| 182 | + */ |
| 183 | + reverse() { |
| 184 | + let temp = this.head; |
| 185 | + this.head = this.tail; |
| 186 | + this.tail = temp; |
| 187 | + |
| 188 | + let previous = null; |
| 189 | + let after = null; |
| 190 | + while (temp) { |
| 191 | + after = temp.next; |
| 192 | + temp.next = previous; |
| 193 | + previous = temp; |
| 194 | + temp = after; |
| 195 | + } |
| 196 | + return this; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Traversing or Printing the Linked list |
| 201 | + */ |
| 202 | + traverse() { |
| 203 | + let current = this.head; |
| 204 | + while (current) { |
| 205 | + console.log(current.data); |
| 206 | + current = current.next; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * @returns {[]} Linkedlist data as elements in Array |
| 212 | + */ |
| 213 | + listAsArray() { |
| 214 | + let arr = []; |
| 215 | + let current = this.head; |
| 216 | + while (current) { |
| 217 | + arr.push(current.data); |
| 218 | + current = current.next; |
| 219 | + } |
| 220 | + return arr; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Utility Function (PRIVATE FUNCTION) |
| 225 | + * if the length is zero then assign null to both head and tail |
| 226 | + */ |
| 227 | + emptyListCheck() { |
| 228 | + if (this.length === 0) { |
| 229 | + this.head = null; |
| 230 | + this.tail = null; |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments