从文件中读取数据建立顺序表,求两个集合的并运算 有一个错误,不知道为什么,求大神解答!
#include <stdio.h>#include <stdlib.h>
#include <malloc.h>
typedef struct SeqList
{
int data;
struct SeqList *next;
}seqlist;
void Print(seqlist *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
void Addlist(seqlist *la,seqlist *lb)
{
seqlist *lc,*ld;
lc=(seqlist *)malloc(sizeof(seqlist));
while(la!=NULL&&lb!=NULL)
{
if(la->data<lb->data)
{
ld=(seqlist *)malloc(sizeof(seqlist));
ld->data=la->data;
lc->next=ld;
lc=ld;
la=la->next;
}
else if(la->data>lb->data)
{
ld=(seqlist *)malloc(sizeof(seqlist));
ld->data=lb->data;
lc->next=ld;
lc=ld;
lb=lb->next;
}
else
{
ld=(seqlist *)malloc(sizeof(seqlist));
ld->data=la->data;
lc->next=ld;
lc=ld;
la=la->next;
lb=lb->next;
}
}
while(la==NULL)
{
ld=(seqlist *)malloc(sizeof(seqlist));
ld->data=lb->data;
lc->next=ld;
lc=ld;
lb=lb->next;
}
while(lb==NULL)
{
ld=(seqlist *)malloc(sizeof(seqlist));
ld->data=la->data;
lc->next=ld;
lc=ld;
la=la->next;
}
lc->next=NULL;
Print(lc);
}
int main()
{
seqlist *p,*q,*pp,*qq,*ppp,*qqq;
p=(seqlist *)malloc(sizeof(seqlist));
q=(seqlist *)malloc(sizeof(seqlist));
ppp=p;
qqq=q;
FILE *fpa,*fpb;
int c,i;
if((fpa=fopen("a.txt","r"))==NULL)
{
printf("can't open file a.txt\n");
exit(1);
}
if((fpb=fopen("b.txt","r"))==NULL)
{
printf("can't open file b.txt\n");
exit(1);
}
fscanf(fpa,"%d\n",&c);
printf("%d\n",c);
for(i=0;i<c;i++)
{
pp=(seqlist *)malloc(sizeof(seqlist));
fscanf(fpa,"%d ",&pp->data);
printf("%d ",pp->data);
p->next=pp;
p=pp;
}
fclose(fpa);
printf("\n");
fscanf(fpb,"%d\n",&c);
printf("%d\n",c);
for(i=0;i<c;i++)
{
qq=(seqlist *)malloc(sizeof(seqlist));
fscanf(fpb,"%d ",&qq->data);
printf("%d ",qq->data);
q->next=qq;
q=qq;
}
fclose(fpb);
printf("\n");
Addlist(ppp->next,qqq->next);
return 0;
}