I am writing a function which removes a specific node from the linked list. The function works for most cases but does not work when the node to delete is either the head node, or the only node in the linked list. I tried setting the current to equal NULL when this case happens but that creates a segmentation fault, where is my logic wrong?
void deleteNodeAfter(Node *head, Node *delete) {
bool remove = false;
Node *current = head;
Node *delNode = delete;
if (current->songName == delNode->songName){
current= NULL;
remove = true;
}
while (!remove) {
if (current->link->songName == delNode->songName) {
current->link = delNode->link;
remove = true;
// free(delNode);
} else {
current = current->link;
}
}
}
if (current->link->songName == delNode->songName)does what to expect it to do. - Jasenheador not. See: How to create a Minimal, Complete, and Verifiable example. - David C. Rankin