I decided to do a project on doubly-linked list in order to understand it better. I have already made functions that insert nodes at the head and at the tail but now I'm having trouble in inserting nodes by value. Here is the function:
void f_insert_by_value(the_individual **head, char *str, int a) {
the_individual *current = *head, *temp = f_create(str, a);
if (*head == NULL) *head = temp;
else {
if (temp->age < (*head)->age) {
temp->next = (*head);
(*head)->prev = temp;
(*head) = (*head)->prev;
}
else {
while (temp->age > current->age && current->next != NULL) current = current->next;
if (current->next = NULL) {
temp->prev = current;
current->next = temp;
current = current->next;
}
else {
temp->prev = current->prev;
temp->next = current;
current->prev->next = temp;
current->prev = temp;
}
}
}
return;
}
Segmentation fault occurs on line "current->prev->next = temp". I tried to print addresses to see why this happens and found out that node which is first in the input always ends up having it's previous element pointing to NULL. Can someone explain why that happens and how can it be fixed? Thank you.