有两个链表a和b,设结点中包含学号和姓名,从a链表中删除和b链表中相同学号的结点?为什么小弟我这个程序没效果,而且输入0也不会结束输入
有两个链表a和b,设结点中包含学号和姓名,从a链表中删除和b链表中相同学号的结点?为什么我这个程序没效果,而且输入0也不会结束输入。
#include<stdio.h>
#define LEN sizeof(struct student)
#define NULL 0
int n;
struct student
{char num[10];
char name[10];
struct student *next;
}*ahead,*bhead;
main()
{struct student *creat(void);
void del(struct student *ahead,struct student *bhead);
void print(struct student *ahead);
ahead=creat();
bhead=creat();
del(ahead,bhead);
print(ahead);
}
struct student *creat(void)
{struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%s %s",p1->num,p1->name);
head=NULL;
while(strcmp(p1->num,"0")!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%s %s",p1->num,p1->name);
}
p2->next=NULL;
return(head);
}
void del(struct student *ahead,struct student *bhead)
{struct student *p,*ap1,*bp1;
ap1=ahead;
while(ahead->next!=NULL)
{ bp1=bhead;
while(bp1!=NULL&&strcmp((bp1->num,ap1->num)==1))
bp1=bp1->next;
if(strcmp(bp1->num,ap1->num)==0)
if(ap1==ahead)
ahead=ap1->next;
else p->next=ap1->next;
p=ap1;
ap1=ap1->next;
}
}
void print(struct student *ahead)
{struct student *p;
p=ahead;
while(p!=NULL)
{printf("%s%s",p->num,p->name);
p=p->next;
}
}
------解决方案--------------------
http://topic.****.net/u/20120420/18/64ea3205-17ac-4b9a-b053-5e43427f1473.html
#include<stdio.h>
#define LEN sizeof(struct student)
#define NULL 0
int n;
struct student
{char num[10];
char name[10];
struct student *next;
}*ahead,*bhead;
main()
{struct student *creat(void);
void del(struct student *ahead,struct student *bhead);
void print(struct student *ahead);
ahead=creat();
bhead=creat();
del(ahead,bhead);
print(ahead);
}
struct student *creat(void)
{struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%s %s",p1->num,p1->name);
head=NULL;
while(strcmp(p1->num,"0")!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%s %s",p1->num,p1->name);
}
p2->next=NULL;
return(head);
}
void del(struct student *ahead,struct student *bhead)
{struct student *p,*ap1,*bp1;
ap1=ahead;
while(ahead->next!=NULL)
{ bp1=bhead;
while(bp1!=NULL&&strcmp((bp1->num,ap1->num)==1))
bp1=bp1->next;
if(strcmp(bp1->num,ap1->num)==0)
if(ap1==ahead)
ahead=ap1->next;
else p->next=ap1->next;
p=ap1;
ap1=ap1->next;
}
}
void print(struct student *ahead)
{struct student *p;
p=ahead;
while(p!=NULL)
{printf("%s%s",p->num,p->name);
p=p->next;
}
}
------解决方案--------------------
http://topic.****.net/u/20120420/18/64ea3205-17ac-4b9a-b053-5e43427f1473.html