多项式合并的问题,请帮忙!!!
#include<stdio.h>#include<stdlib.h>
struct list
{
int exp; //指数
int ceof; //系数
struct list*next;
};
struct list* creat() //建立
{
struct list*p1,*p2,*sq;
p2=sq=(struct list*)malloc(sizeof(struct list));
p1=(struct list*)malloc(sizeof(struct list));
scanf(" %d,%d",&p1->exp,&p1->ceof);
while(p1->exp!='#')
{
p2->next=p1;
p2=p1;
p1=(struct list*)malloc(sizeof(struct list));
scanf(" %d%d",&p1->exp,&p1->ceof);
}
p2->next=NULL;
return sq;
}
int compare(int x,int y) //比较两多项式的指数
{
if(x<y)
return 1;
if(x>y)
return -1;
if(x=y)
return 0;
}
void main()
{
struct list*l1,*l2,*pa,*pb,*ha,*hb,*t;
int a,b,flag,sum;
printf("多项式1:\n");
l1=creat();
printf("多项式2:\n");
l2=creat();
pa=l1->next;
pb=l2->next;
ha=l1;
hb=l2;
while((pa!=NULL)&&(pb!=NULL))
{
a=pa->exp;
b=pb->exp;
flag=compare(a,b);
switch(flag) //通过比较,将新的多项式接在l1上
{
case 1:
ha=pa;
pa=pa->next;
break;
case -1:
sum=(pa->ceof+pb->ceof);
if(sum!=0)
{pa->ceof=sum;
ha=pa;
pa=pa->next;
}
else
{
ha->next=pa->next;
free(pa);
hb->next=pb->next;
free(pb);
pb=hb->next;
}
break;
case 0:
pb->next=ha->next;
ha->next=pb;
ha=pb;
pb=hb->next;
break;
}
if(pb!=NULL)
ha->next=pb;
}
t=l1->next;
while(t!=NULL)
{
printf(" %d,%d\n",t->exp,t->ceof);
t=t->next;
}
}
我想实现两多项式的合并,但从输入就有问题,一直停不下来,好像陷入死循环,死机了,请高手帮我!!!