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