In: Computer Science
In a double linked chain, each node can point to the previous node as well as the next node. Illustrate and list the steps necessary to add a node to the end of the doubly linked chain.
In case of any query do comment.Thanks
Below steps should be followed to add a node to the last of doubly linked list:
1. Create a new Node with the given data (let's say newNode here)
2. set the next pointer of the newNode to null ( newNode -> next = null )
3. Now get a pointer to Head. (let's say current)
4. Now we need to find the last node of the doubly linked list which will have next pointer to null, Check next of current is null, this means Head is null then make node as Head using below:
a. set the prev pointer of newNode to null
b. set head to newNode
5. If in step 4, next is not null of the current then traverse the list till the last when you found next of the current is null.
6. Now set the next of current node (which is last node now) to newNode.
7. Set the prev of newNode to current node (which we found as a last node in step 6).