요소 후 내가 튜토리얼에서 배운 요소 전에 내가 다음과 같은 기능을 사용하고 일부 데이터를 추가하기 위해 연결리스트를 작성하는 동안 :링크 C의 add_before의 목록과 add_after 기능
struct node *addafter(struct node *start,int data,int item)
{
struct node *tmp,*p;
p=start;
while(p!=NULL)
{
if(p->info==item)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=p->link;
p->link=tmp;
return start;
}
p=p->link;
}
printf("Item Not Found:\n");
return start;
}
struct node *addbefore(struct node *start,int data,int item)
{
struct node *tmp,*p;
if(item==start->info)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->link=start->link;
tmp->info=data;
start->link=tmp;
return start;
}
p=start;
while(p->link!=NULL)
{
while(p->link->info==item)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=p->link;
p->link=tmp;
return start;
}
p=p->link;
}
printf("Item Not Found:\n");
return start;
}
내 의심의 여지가 addafter 함수에있는 이유는 정지 조건이 p! = NULL이고 addbefore 함수의 경우 p-> link! = NULL입니까? 하나만 설명해주세요!
자신이 어떻게 작동하는지 그래프로 그려 보면 이해에 도움이 될 것입니다. 간단한 설명 : while 루프 'addafter'는'p-> info'에 액세스하므로'p' 유효성 검사를 확인하십시오. 'addbefore'의'while' 루프는'p-> link'에 접근 할 것이므로'p-> link' 검증을 체크하십시오. – MYMNeo
'while (p-> link-> info == item)'잘못된 것입니다. 그 블록의 끝에서 컨트롤이 반환되기 때문에 ('return start;')'while'이 아닌'if'가되어야합니다. – suspectus