这段程序:
struct student {int info; struct student *link; }; struct student *del(struct student *head, int num) { struct student *p1,*p2=0; if(!head) printf("\nlist null!\n"); else { p1=head; while((p1->info!=num)&&p1) { p2=p1; p1=p1->link; }
printf("line 25: %d,%d\n",p2->info,p1->info);
if(p1->info==num) { printf("num==p1->info\n"); printf("delete:%d\n",num); if(p1==head) { printf("p1==head\n");
head=p1->link;
free(p1); num==0; /*如果不加这一句的话,num的值会无端端变成0*/ } else { printf("p1!=head\n");
(p2->link)=(p1->link);
p2=head; while(p2) { printf("line 50: %d\n",p2->info); p2=p2->link; }
free(p1);
putchar('\n'); p2=head; while(p2) { printf("line 59: %d\n",p2->info); /*如果输入2,这里会死循环;如果输入3、4、5,这里的数据会出错。*/ p2=p2->link; }
} printf("delete:%d\n",num);
} else printf("%d not been found!\n",num); } return (head); } main() { ST k[5],*p=k; int i;
for(i=0;i<5;i++) { k[i].info=i+1; k[i].link=&k[i+1]; } k[i-1].link=0;
while(p) { printf("line 86: %d\n",p->info); p=p->link; }
p=k;
printf("请输入要删除的节点\n"); scanf("%d",&i); p=del(p,i); while(p) { printf("%d\n",p->info); p=p->link; } getch(); }