To view the code previously written you can look at the previous posting entitled “Building a Singly Linked List in JavaScript”. A modification is being made to the original code. Since new nodes will be added multiple times (as opposed to initially when it was only done once) we are first creating a new class called “Node” with a value and initially pointing to null. This is done so that the coding does not have to be repeated and only a new instance of this class needs to be created.
In the prepend method, first a new instance of node is created with the value being supplied as the parameter. The previous head of the Linked List now becomes the node pointed to by the new node and the new node then becomes the head of the Linked List. The length is increased by one and the Linked List is returned.
The methods to insert a new node in a certain spot and to delete a node at a particular spot both require finding that spot and both methods share this code. A separate method is therefore created to carry out this operation. The method is called here “proceedToIndex” and has as a parameter the index that we are looking for. A variable called “counter” is created with an initial value of 0. Utilizing a while loop, the node being iterated on is constantly moved forward and the counter is increased until the counter equals the index that was entered as an argument and then returns the node located at that index.
The method to insert the new node can now be created. We first use an "if loop" to ensure that if the index number given is equal to or higher than the actual number of nodes then the append method is activated to add the value as a new tail for the Linked List. If the index argument is smaller than the number of nodes then the remainder of the insert method is activated. First a new instance of Node is instantiated. Next a variable “first” is created which utilizes the “proceedToIndex” method to locate the node which is at the point of insertion. The parameter is index-1 because the Linked Link index starts at 0 and therefore a submitted number of “4” would be at index”3” in the Linked List. The variable “holdingPointer” is what the previously identified node points to. What it points to is updated as the new node which then points to the node that was previously immediately after the node before the new node. The length of the Linked List is the increased by one. In summary, if the new node “N” is being inserted after index 5 containing node “P” that was originally followed at index 6 with node “F”, the pointer in node “P” (index 5) is changed from “F” and now points to “N” (which now at index 6) and the pointer of “N” now points to “F” (which is now at index 7).
The method to delete a node is simpler. First, we proceed to the appropriate index utilizing the “proceedToIndex” method. The variable “deleteableNode” representing the node to be deleted is established as the node after the previously identified one. The pointer of the node previous to the node to be deleted is now changed to point to the node after the node to be deleted, thus eliminating the node between the two from the Linked List. The length of the Linked List is then decreased by one.
As a simplification, a method is being created so that the linked list appears as an array in the terminal. First an empty array is created. The current node is started as the head and then each next node becomes the current node in turn and is pushed into the array until the last node is passed which means that the current node is now null. The loop ends at that point.
Thank you for reading!
Top comments (0)