所以我有一个链表,我试图删除输入的名称,但删除功能无法在列表中找到该名称。我试图找到逻辑缺陷,我需要一些帮助!
了解一些知识: name是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;
}
答案 0 :(得分:1)
if(temp->name == input)
只是比较指针。试试if (strcmp(temp->name, input) == 0)
...
答案 1 :(得分:1)
不要使用temp->name == input
,
使用strcmp(temp->name, input)
见strcmp