求找错QAQ
#include<stdio.h>#include<malloc.h>
typedef struct PolyNode *Poly;
struct PolyNode{
int coef;
int expon;
struct PolyNode *next;
};
void attach(int c,int e,Poly *p);
Poly ReadPoly();
Poly Add(Poly p1,Poly p2);
void PrintPoly(Poly p);
int main()
{
Poly p1,p2,pa;
p1=ReadPoly();
p2=ReadPoly();
pa=Add(p1,p2);
PrintPoly(pa);
return 0;
}
Poly ReadPoly()
{
int N,c,e;
Poly p,rear,t;
scanf("%d",&N);
p=(Poly)malloc(sizeof(struct PolyNode));
p->next=NULL;
rear=p;
while(N--){
scanf("%d %d",&c,&e);
attach(c,e,&rear);
}
t=p;
p=p->next;
free(t);
return p;
}
void attach(int c,int e,Poly *p)
{
Poly addnode;
addnode=(Poly)malloc(sizeof(struct PolyNode));
addnode->coef=c;
addnode->expon=e;
addnode->next=NULL;
(*p)->next=addnode;
*p=addnode;
}
Poly Add(Poly p1,Poly p2)
{
Poly p,t1,t2,rear,temp;
t1=p1;
t2=p2;
p=(Poly)malloc(sizeof(struct PolyNode));
p->next=NULL;
rear=p;
while(t1&&t2){
if(t1->expon==t2->expon){
if((t1->coef+t2->coef)!=0){
attach(t1->coef+t2->coef,t1->expon,&rear);
}
t1=t1->next;
t2=t2->next;
}
else if(t1->expon>t2->expon){
attach(t1->coef,t1->expon,&rear);
t1=t1->next;
}
else {
attach(t2->coef,t2->expon,&rear);
t2=t2->next;
}
}
for(;t1;t1=t1->next)attach(t1->coef,t1->expon,&rear);
for(;t2;t2=t2->next)attach(t2->coef,t2->expon,&rear);
rear->next=NULL;
temp=p;
p=p->next;
free(temp);
return p;
}
void PrintPoly(Poly p)
{
int flag=0;
if(!p){
printf("0 0");
}
while(p){
if(!flag)flag=1;
else printf(" ");
printf("%d %d",p->coef,p->expon);
p=p->next;
}
printf("\n");
}