Reputation: 43
So I have a linkedlist and I am trying to delete the name of the input, but the delete function cannot find the name on the list. I am trying to find the logic flaw, I need some help!
some knowledge to know: name is a character array in the struct
case 4:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter the name to delete : ");
scanf("%s",&user);
if(delete(user))
printf("%s deleted successfully\n",user);
else
printf("%s not found in the list\n",user);
}
break;
int delete(const char* input)
{
struct person *temp, *prev;
temp = head;
while(temp != NULL)
{
if(temp->name == input)
{
if(temp == head)
{
head = temp->next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}
Upvotes: 0
Views: 50
Reputation: 114
Donot use temp->name == input
,
use strcmp(temp->name, input)
see strcmp
Upvotes: 1
Reputation: 29266
if(temp->name == input)
is just comparing pointers. Try if (strcmp(temp->name, input) == 0)
...
Upvotes: 1