Wednesday, May 22, 2013

C Program to delete the node in the linked list where you will be given the node to delete but not the head pointer .

1->2->3->4->5->6->7->NULL

Consider the above linked list and you are given the node 4 and you have to delete that node . In this scenario you don't have the head pointer . At some point you might think that this is not possible . Because we need to make its previous node 3 to point it to the node 5  like this      1->2->3->5->6->7->NULL and you dont have reference to node 3 . Think about it .......

Is there any way to do it !!! ??? Yes there is a way to do it .

Steps are given below .

1. You have reference to node 4 and you can traverse nodes 5,6,7 . Now copy the data from node 5 and store it in node 4 . So it will become
1->2->3->5->5->6->7->NULL . 
2.Now delete the 5th node . you have its previous node and next node . so deleting will be simple .
1->2->3->5->6->7->NULL.
But there is an Exception 
Deleting the last node is not possible .If the linked list is 1->2->3->4->NULL and if you want to delete 4th node its not possible .

Code :(in C)


Please let me know if you have any questions .