链表删除节点问题
#include "stdio.h"#include "conio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{char num[6];
char name[8];
struct student *next;
};
struct student *creat(void)
{struct student *p1,*p2,*head;
int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
printf("Input 0 to end.\n");
printf("Input num,name:\n");
scanf("%s,%s",&p1->num,&p1->name);
while(p1!=NULL)
{if(n==0)head=p1;
else p2->next=p1;
n++;
p2=p1;
}
p2->next=NULL;
return(head);
}
void print(struct student *p)
{while(p!=NULL)
{printf("%4s,%4s\n",p->num,p->name);
p=p->next;
}
}
main()
{struct student *ha,*hb,*p1,*p2,*p;
ha=creat();
hb=creat();
print(ha);
print(hb);
p1=ha;
p2=hb;
/*delete*/
while(p1!=NULL)
{if(strcmp(p2->num,p1->num)!=0)
p2=p2->next;
else
{if(p1==ha)
p1->next=ha;
else
p->next=p1->next;
p=p1;
p1=p1->next; 这里是不是有问题?
}
}
print(ha);
getch();
}