여기 목록을 반복적으로 역전하려고합니다. 그러나 문제는리스트가 3-> 2-> 4-> NULL이라는 말의 크기가 크다는 것입니다. 결국 3 -> NULL, 즉 하나의 요소 만있는 목록이됩니다. 코드의 문제점을 알려주십시오.목록 반전 - 논리 오류
struct node *reverselist(struct node *head)
{
struct node *list=head;
if(head==NULL)
{
printf("\nThe list is empty\n");
return head;
}
if(head->next==NULL)
{
printf("\nThe list has only one element\n");
return head;
}
struct node *cur=head;
struct node *new=head->next;
while(cur->next!=NULL)
{
cur->next=new->next;
new->next=cur;
new=cur->next;
}
return list;
}
좋은 사람. 변수 이름을 실행하는 데 머리를 사용하지 않을 것입니다. 그것은 독자를 혼란스럽게합니다. –
@PeterMiehle Thanx, 네, 조금 혼란스럽고'head'를 움직일 때 읽을 수 없습니다. 최적화하기 위해 노력했습니다. 머리를 움직일 때 더 많은 읽을 수있는 변수를 사용하는 것을 선호합니다. –