从链表a中删除与b链表相同的结点,求高手帮忙检错
提示: 作者被禁止或删除 内容自动屏蔽
#include <stdio.h> #include <string.h> #define LA 4 #define LB 5 #define NULL 0 struct student { int num; char name[8]; struct student *next; }a[LA],b[LB]; int main(void) { struct student a[LA]={{101,"wang"},{102,"li"},{105,"zhang"},{106,"wei"}}; struct student b[LB]={{103,"zhang"},{104,"ma"},{105,"chen"},{107,"guo"},{108,"lui"}}; int i; struct student *p,*p1,*p2,*head1,*head2; /*初始化*/ head1=a; head2=b; printf("\nlist a:\n"); for(p1=head1,i=1;p1<a+LA;i++) { p=p1; p1->next=a+i; printf("%8d%8s\n",p1->num,p1->name); p1=p1->next; } p->next=NULL; printf("\nlist b:\n"); for(p2=head2,i=1;p2<b+LB;i++) { p=p2; p2->next=b+i; printf("%8d%8s\n",p2->num,p2->name); p2=p2->next; } p->next=NULL; printf("\n"); /*删除*/ p1=head1; while(p1 != NULL) { p2=head2; while(p2 != NULL && p1->num != p2->num) p2=p2->next; if(p2!=NULL && p1->num == p2->num) // 如果p2指向NULL,会访问出错,添加条件p2!=NULL { if(p1==head1) head1=p1->next; else p->next=p1->next; } p=p1; p1=p1->next; } p1=head1; printf("\nresult:\n"); while(p1!=NULL) { printf("%7d %7s\n",p1->num,p1->name); p1=p1->next; } return 0; }