|
| 1 | +class Node { |
| 2 | + // constructor |
| 3 | + constructor(element) { |
| 4 | + this.element = element; |
| 5 | + this.next = null |
| 6 | + } |
| 7 | +} |
| 8 | +// linkedlist class |
| 9 | +class LinkedList { |
| 10 | + constructor() { |
| 11 | + this.head = null; |
| 12 | + this.size = 0; |
| 13 | + } |
| 14 | + |
| 15 | + // adds an element at the end |
| 16 | + // of list |
| 17 | + add(element) { |
| 18 | + // creates a new node |
| 19 | + var node = new Node(element); |
| 20 | + |
| 21 | + // to store current node |
| 22 | + var current; |
| 23 | + |
| 24 | + // if list is Empty add the |
| 25 | + // element and make it head |
| 26 | + if (this.head == null) |
| 27 | + this.head = node; |
| 28 | + else { |
| 29 | + current = this.head; |
| 30 | + |
| 31 | + // iterate to the end of the |
| 32 | + // list |
| 33 | + while (current.next) { |
| 34 | + current = current.next; |
| 35 | + } |
| 36 | + |
| 37 | + // add node |
| 38 | + current.next = node; |
| 39 | + } |
| 40 | + this.size++; |
| 41 | + } |
| 42 | + |
| 43 | + // insert element at the position index |
| 44 | + // of the list |
| 45 | + insertAt(element, index) { |
| 46 | + if (index < 0 || index > this.size) |
| 47 | + return console.log("Please enter a valid index."); |
| 48 | + else { |
| 49 | + // creates a new node |
| 50 | + var node = new Node(element); |
| 51 | + var curr, prev; |
| 52 | + |
| 53 | + curr = this.head; |
| 54 | + |
| 55 | + // add the element to the |
| 56 | + // first index |
| 57 | + if (index == 0) { |
| 58 | + node.next = this.head; |
| 59 | + this.head = node; |
| 60 | + } else { |
| 61 | + curr = this.head; |
| 62 | + var it = 0; |
| 63 | + |
| 64 | + // iterate over the list to find |
| 65 | + // the position to insert |
| 66 | + while (it < index) { |
| 67 | + it++; |
| 68 | + prev = curr; |
| 69 | + curr = curr.next; |
| 70 | + } |
| 71 | + |
| 72 | + // adding an element |
| 73 | + node.next = curr; |
| 74 | + prev.next = node; |
| 75 | + } |
| 76 | + this.size++; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // removes an element from the |
| 81 | + // specified location |
| 82 | + removeFrom(index) { |
| 83 | + if (index < 0 || index >= this.size) |
| 84 | + return console.log("Please Enter a valid index"); |
| 85 | + else { |
| 86 | + var curr, prev, it = 0; |
| 87 | + curr = this.head; |
| 88 | + prev = curr; |
| 89 | + |
| 90 | + // deleting first element |
| 91 | + if (index === 0) { |
| 92 | + this.head = curr.next; |
| 93 | + } else { |
| 94 | + // iterate over the list to the |
| 95 | + // position to removce an element |
| 96 | + while (it < index) { |
| 97 | + it++; |
| 98 | + prev = curr; |
| 99 | + curr = curr.next; |
| 100 | + } |
| 101 | + |
| 102 | + // remove the element |
| 103 | + prev.next = curr.next; |
| 104 | + } |
| 105 | + this.size--; |
| 106 | + |
| 107 | + // return the remove element |
| 108 | + return curr.element; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // removes a given element from the |
| 113 | + // list |
| 114 | + removeElement(element) { |
| 115 | + var current = this.head; |
| 116 | + var prev = null; |
| 117 | + |
| 118 | + // iterate over the list |
| 119 | + while (current != null) { |
| 120 | + // comparing element with current |
| 121 | + // element if found then remove the |
| 122 | + // and return true |
| 123 | + if (current.element === element) { |
| 124 | + if (prev == null) { |
| 125 | + this.head = current.next; |
| 126 | + } else { |
| 127 | + prev.next = current.next; |
| 128 | + } |
| 129 | + this.size--; |
| 130 | + return current.element; |
| 131 | + } |
| 132 | + prev = current; |
| 133 | + current = current.next; |
| 134 | + } |
| 135 | + return -1; |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + // finds the index of element |
| 140 | + indexOf(element) { |
| 141 | + var count = 0; |
| 142 | + var current = this.head; |
| 143 | + |
| 144 | + // iterate over the list |
| 145 | + while (current != null) { |
| 146 | + // compare each element of the list |
| 147 | + // with given element |
| 148 | + if (current.element === element) |
| 149 | + return count; |
| 150 | + count++; |
| 151 | + current = current.next; |
| 152 | + } |
| 153 | + |
| 154 | + // not found |
| 155 | + return -1; |
| 156 | + } |
| 157 | + |
| 158 | + // checks the list for empty |
| 159 | + isEmpty() { |
| 160 | + return this.size == 0; |
| 161 | + } |
| 162 | + |
| 163 | + // gives the size of the list |
| 164 | + size_of_list() { |
| 165 | + console.log(this.size); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + // prints the list items |
| 170 | + printList() { |
| 171 | + var curr = this.head; |
| 172 | + var str = ""; |
| 173 | + while (curr) { |
| 174 | + str += curr.element + " "; |
| 175 | + curr = curr.next; |
| 176 | + } |
| 177 | + console.log(str); |
| 178 | + } |
| 179 | + |
| 180 | +} |
| 181 | + |
| 182 | +// creating an object for the |
| 183 | +// Linkedlist class |
| 184 | +var ll = new LinkedList(); |
| 185 | + |
| 186 | +// testing isEmpty on an empty list |
| 187 | +// returns true |
| 188 | +console.log(ll.isEmpty()); |
| 189 | + |
| 190 | +// adding element to the list |
| 191 | +ll.add(10); |
| 192 | + |
| 193 | +// prints 10 |
| 194 | +ll.printList(); |
| 195 | + |
| 196 | +// returns 1 |
| 197 | +console.log(ll.size_of_list()); |
| 198 | + |
| 199 | +// adding more elements to the list |
| 200 | +ll.add(20); |
| 201 | +ll.add(30); |
| 202 | +ll.add(40); |
| 203 | +ll.add(50); |
| 204 | + |
| 205 | +// returns 10 20 30 40 50 |
| 206 | +ll.printList(); |
| 207 | + |
| 208 | +// prints 50 from the list |
| 209 | +console.log("is element removed ?" + ll.removeElement(50)); |
| 210 | + |
| 211 | +// prints 10 20 30 40 |
| 212 | +ll.printList(); |
| 213 | + |
| 214 | +// returns 3 |
| 215 | +console.log("Index of 40 " + ll.indexOf(40)); |
| 216 | + |
| 217 | +// insert 60 at second position |
| 218 | +// ll contains 10 20 60 30 40 |
| 219 | +ll.insertAt(60, 2); |
| 220 | + |
| 221 | +ll.printList(); |
| 222 | + |
| 223 | +// returns false |
| 224 | +console.log("is List Empty ? " + ll.isEmpty()); |
| 225 | + |
| 226 | +// remove 3rd element from the list |
| 227 | +console.log(ll.removeFrom(3)); |
| 228 | + |
| 229 | +// prints 10 20 60 40 |
| 230 | +ll.printList(); |
0 commit comments