|
| 1 | +/* SinglyLinkedList!! |
| 2 | +* A linked list is implar to an array, it hold values. |
| 3 | +* However, links in a linked list do not have indexes. With |
| 4 | +* a linked list you do not need to predetermine it's size as |
| 5 | +* it grows and shrinks as it is edited. This is an example of |
| 6 | +* a singly linked list. |
| 7 | +*/ |
| 8 | + |
| 9 | +//Functions - add, remove, indexOf, elementAt, addAt, removeAt, view |
| 10 | + |
| 11 | +//Creates a LinkedList |
| 12 | +function LinkedList(){ |
| 13 | + //Length of linklist and head is null at start |
| 14 | + var length = 0; |
| 15 | + var head = null; |
| 16 | + |
| 17 | + //Creating Node with element's value |
| 18 | + var Node = function(element){ |
| 19 | + this.element = element; |
| 20 | + this.next = null; |
| 21 | + }; |
| 22 | + |
| 23 | + //Returns length |
| 24 | + this.size = function(){ |
| 25 | + return length; |
| 26 | + }; |
| 27 | + |
| 28 | + //Returns the head |
| 29 | + this.head = function(){ |
| 30 | + return head; |
| 31 | + }; |
| 32 | + |
| 33 | + //Creates a node and adds it to linklist |
| 34 | + this.add = function(element){ |
| 35 | + var node = new Node(element); |
| 36 | + //Check if its the first element |
| 37 | + if(head === null){ |
| 38 | + head = node; |
| 39 | + } |
| 40 | + else { |
| 41 | + var currentNode = head; |
| 42 | + |
| 43 | + //Loop till there is node present in the list |
| 44 | + while(currentNode.next){ |
| 45 | + currentNode = currentNode.next; |
| 46 | + } |
| 47 | + |
| 48 | + //Adding node to the end of the list |
| 49 | + currentNode.next = node; |
| 50 | + } |
| 51 | + //Increment the length |
| 52 | + length++; |
| 53 | + }; |
| 54 | + |
| 55 | + //Removes the node with the value as param |
| 56 | + this.remove = function(element){ |
| 57 | + var currentNode = head; |
| 58 | + var previousNode; |
| 59 | + |
| 60 | + //Check if the head node is the element to remove |
| 61 | + if(currentNode.element === element){ |
| 62 | + head = currentNode.next; |
| 63 | + } |
| 64 | + else { |
| 65 | + |
| 66 | + //Check which node is the node to remove |
| 67 | + while(currentNode.element !== element) { |
| 68 | + previousNode = currentNode; |
| 69 | + currentNode = currentNode.next; |
| 70 | + } |
| 71 | + |
| 72 | + //Removing the currentNode |
| 73 | + previousNode.next = currentNode.next; |
| 74 | + } |
| 75 | + |
| 76 | + //Decrementing the length |
| 77 | + length--; |
| 78 | + }; |
| 79 | + |
| 80 | + //Return if the list is empty |
| 81 | + this.isEmpty = function(){ |
| 82 | + return length === 0; |
| 83 | + }; |
| 84 | + |
| 85 | + //Returns the index of the element passed as param otherwise -1 |
| 86 | + this.indexOf = function(element) { |
| 87 | + var currentNode = head; |
| 88 | + var index = -1; |
| 89 | + |
| 90 | + while(currentNode){ |
| 91 | + index++; |
| 92 | + |
| 93 | + //Checking if the node is the element we are searching for |
| 94 | + if(currentNode.element === element){ |
| 95 | + return index+1; |
| 96 | + } |
| 97 | + currentNode = currentNode.next; |
| 98 | + } |
| 99 | + |
| 100 | + return -1; |
| 101 | + }; |
| 102 | + |
| 103 | + //Returns the element at an index |
| 104 | + this.elementAt = function(index){ |
| 105 | + var currentNode = head; |
| 106 | + var count = 0; |
| 107 | + while(count < index){ |
| 108 | + count++; |
| 109 | + currentNode = currentNode.next; |
| 110 | + } |
| 111 | + return currentNode.element; |
| 112 | + }; |
| 113 | + |
| 114 | + //Adds the element at specified index |
| 115 | + this.addAt = function(index, element){ |
| 116 | + index--; |
| 117 | + var node = new Node(element); |
| 118 | + |
| 119 | + var currentNode = head; |
| 120 | + var previousNode; |
| 121 | + var currentIndex = 0; |
| 122 | + |
| 123 | + //Check if index is out of bounds of list |
| 124 | + if(index > length){ |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + //Check if index is the start of list |
| 129 | + if(index === 0){ |
| 130 | + node.next = currentNode; |
| 131 | + head = node; |
| 132 | + } |
| 133 | + else { |
| 134 | + while (currentIndex < index) { |
| 135 | + currentIndex++; |
| 136 | + previousNode = currentNode; |
| 137 | + currentNode = currentNode.next; |
| 138 | + } |
| 139 | + |
| 140 | + //Adding the node at specified index |
| 141 | + node.next = currentNode; |
| 142 | + previousNode.next = node; |
| 143 | + } |
| 144 | + |
| 145 | + //Incrementing the length |
| 146 | + length++; |
| 147 | + }; |
| 148 | + |
| 149 | + //Removes the node at specified index |
| 150 | + this.removeAt = function(index) { |
| 151 | + index--; |
| 152 | + var currentNode = head; |
| 153 | + var previousNode; |
| 154 | + var currentIndex = 0; |
| 155 | + |
| 156 | + //Check if index is present in list |
| 157 | + if(index < 0 || index >= length){ |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + //Check if element is the first element |
| 162 | + if (index === 0) { |
| 163 | + head = currentNode.next; |
| 164 | + } |
| 165 | + else { |
| 166 | + while (currentIndex < index) { |
| 167 | + currentIndex++; |
| 168 | + previousNode = currentNode; |
| 169 | + currentNode = currentNode.next; |
| 170 | + } |
| 171 | + previousNode.next = currentNode.next; |
| 172 | + } |
| 173 | + |
| 174 | + //Decrementing the length |
| 175 | + length--; |
| 176 | + return currentNode.element; |
| 177 | + }; |
| 178 | + |
| 179 | + //Function to view the LinkedList |
| 180 | + this.view = function () { |
| 181 | + var currentNode = head; |
| 182 | + var count = 0; |
| 183 | + while(count < length){ |
| 184 | + count++; |
| 185 | + console.log(currentNode.element); |
| 186 | + currentNode = currentNode.next; |
| 187 | + } |
| 188 | + }; |
| 189 | +}; |
| 190 | + |
| 191 | +//Implementation of LinkedList |
| 192 | +var linklist = new LinkedList(); |
| 193 | +linklist.add(2); |
| 194 | +linklist.add(5); |
| 195 | +linklist.add(8); |
| 196 | +linklist.add(12); |
| 197 | +linklist.add(17); |
| 198 | +console.log(linklist.size()); |
| 199 | +console.log(linklist.removeAt(4)); |
| 200 | +linklist.addAt(4,15); |
| 201 | +console.log(linklist.indexOf(8)); |
| 202 | +console.log(linklist.size()); |
| 203 | +linklist.view(); |
0 commit comments