i have function deletenode() deleting node linked list. func. called in main func. deletenode(&head, 10);, head pointer first node of linked list , 10 value deleted.
here, else if statement commented , function runs fine. when uncomment else if statement , run program crashes. why happening , solution?
void deletenode(node **h, int value) { node *current, *temp; current = *h; while(current != null) { // if node deleted first node of list if(current == *h && current->data == value) { *h = current->next; } /* // if node deleted other first node else if(current->next->data == value) { temp = current->next; current->next = temp->next; free(temp); } */ current = current->next; } }
because you're trying access current->next->data without checking if current->next null first. should modify logic either check if current->next != null before trying access data, or check current->data , remove saving previous node also.
Comments
Post a Comment