I'm trying to create program where you enter '+word' and it adds the word and when you enter '-word' it takes the word out of the linked list.
Inserting the word works fine for me but removing it causes a segmentation fault. I'm not sure where the fault lies. Also, is there a way where you can get a hint of where the segmentation fault is?
void
remove_from_list(struct linked_list *list, char *data)
{
struct node *current_node = list->head;
struct node *previous_node = NULL;
while (current_node != NULL) {
if (current_node->data == data) {
break;
}
previous_node = current_node;
current_node = current_node->next;
}
if (previous_node == NULL) {
list->head = list->head->next;
} else {
previous_node->next = current_node->next;
}
free(current_node);
if (list->tail == current_node)
list->tail = previous_node;
}
int
main(void)
{
struct linked_list list = { .head = NULL, .tail = NULL };
char word[50];
do {
printf("Enter string: ");
fgets(word, 50, stdin);
if (word[0] == '+')
add_to_list(&list, word);
else if (word[0] == '-')
remove_from_list(&list, word);
} while (word[0] != '\n');
print_list_rec(&list);
free_list(&list);
return 0;
}
strncmp) to find whether a node holds your string. All that this:current_node->data == datadoes is compare the pointers.dataand see if you get the same issues.