我已经尝试了大约 5 个小时以使此代码正常工作,并且代码是根据数小时的互联网研究编写的。我已经修改了好几次,所有这些都给了我分段错误,所以这是唯一可以运行的版本。正在发生的事情是,代码正在循环执行,并且不仅删除了您要删除的元素,还删除了它之前的所有元素。所以,如果你想删除最后一个元素,列表中的所有内容都会删除。或者,如果您想删除第二个元素,则执行第一个和第二个,依此类推。由于某种原因,它认为输入的每个名称都是顶级名称。
static void menu_delete_employee(void)
{
char deletename[MAX_NAME_LENGTH+1];
char namecheck[MAX_NAME_LENGTH+1];
int errorcheck = 0;
int foundit = 0;
fprintf(stderr, "Enter the name of the employee you wish to delete\n");
gets(deletename);
employee_list = top;
employee_list->name;
do
{
strcpy (namecheck, employee_list->name);
printf("namecheck = %s\n", namecheck);
errorcheck = (strcmp (namecheck, deletename));
printf("errorcheck = %i\n", errorcheck);
switch (errorcheck)
{
case 0:
{
printf("This is the right name\n");
foundit = 1;
if (employee_list->prev == NULL)
{
printf("top name\n");
top = employee_list->next;
}
else
{
if (employee_list->next == NULL)
{
printf("last one\n");
temp = employee_list->prev;
temp-> next = NULL;
free (employee_list);
}
else
{
printf("somewhere in the middle");
temp = employee_list->prev;
temp->next = employee_list->next;
employee_list->next->prev = temp;
free (employee_list);
}
}
printf("delete successful\n");
break;
}
default:
{
printf("not this one\n");
errorcheck = 0;
employee_list = employee_list->next;
break;
}
}
}
while (foundit == 0);
if (foundit == 0)
printf("Name not recognised\n.");
return;
}
任何帮助将非常感激。