이 코드를 제대로 작동 시키려면 약 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;
}
모든 도움을 주시면 감사하겠습니다.
아마도 당신이 묻는 문제와 관련이 없지만 여전히 중요한 포인트입니다 : 절대 안전하지 않기 때문에 ** 결코 ** 사용하지 마십시오. 'char * fgets (char * s, int size, FILE * stream)'을 사용하십시오. – hlovdal
언뜻보기에 최상위 노드가 일치하면 해제되지 않습니다. 마지막 노드에 도달하면 종료해야합니다. 이것 저쪽에, 나는 삭제 부분 안에 무엇이든을 발견한다. 더 나은 링크가 생성 된 부분을 잘못된 출력으로 보냈습니다. – VoidPointer
구조를 만드는 소스 코드를 의미합니까? – user2358231