I have written a delete node function to delete a node in a linked list by the index number. Here is the code:
Please note that I know if the head node turns out to be the indexed position, there is no way to delete that node in the current coding. But please after correcting the present node add that part of the code separately.Code:void delete_node(struct node **start,int index_no) { int counter=0; struct node *current=*start, *prev=NULL;//Current holds start and prev holds previous node of current while(current->next!=NULL) { current=current->next; //Traversing the list prev->next=current;// updating the prev node accordingly counter++;//counter to keep track of the index if(counter==index_no)//if user input and node no matches except the head node { printf("\n Hurrah! We caught the node!!"); prev->next=current->next; //Making sure the prev node holds the address of the next to current ndoe free(current); //deleting current } } }