建立链表合并一元稀疏多项式为什么会停止工作
#include<stdio.h>#include<stdlib.h>
typedef struct Node{
float coef;
int exp;
struct Node *next;
}Node,*Linklist;
Node *List(){
struct Node *p,*q;
struct Node *head;
head=(Node *)malloc(sizeof(Node));
if(head == NULL){
printf("memory error!");
exit(0);
}
head->next=NULL;
int shuru=0;
float xishu=0;
int zhishu=0;
int i=1;
p=head;
printf("请输入多项式n项\n");
scanf("%d",&shuru);
for(i=1;i<=shuru;i++){
q=(Linklist)malloc(sizeof(Node));
printf("输入第%d项\n",i);
scanf("%f %d",&xishu,&zhishu);
p->coef=xishu;
p->exp=zhishu;
p->next=q;
p=q;
}
q->next=NULL;
}
Node *change(Linklist p){
Node *q = p->next;
float xishu=0;
int zhishu=0;
while(q!=NULL){
if(q->next->exp > q->exp){
xishu=q->next->coef;q->next->coef=q->coef;q->coef=xishu;
zhishu=q->next->exp;q->next->exp=q->exp;q->exp=zhishu;
q=q->next;
}
else q=q->next;
}
}//变为指数为降序
void add(struct Node *La,struct Node *Lb){
Node *pa=La->next;
Node *pb=Lb->next;
Node *Lc=(Node *)malloc(sizeof(Node));
Node *r=Lc->next;
Node *m=Lc->next;
float x;
int i=0;
change(La);
change(Lb);
while (pa!=NULL && pb!=NULL){
if(pa->exp > pb->exp){
r->coef = pa->coef;
r->exp = pa->exp;
pa = pa->next;
r = r->next;
}
else if(pa->exp == pb->exp){
r->coef = pa->coef + pb->coef;
r->exp = pa->exp;
pa = pa->next;
pb = pb->next;
r = r->next;
}
else{
r->coef = pb->coef;
r->exp = pb->exp;
pb = pb->next;
r = r->next;
}
}
r->next=pa?pa:pb;
while(m!=NULL){
i++;
m = m->next;
}
m=Lc->next;
printf("%d,",i);
while(m!=NULL){
if(m->next==NULL){
printf("%f,%d\n",m->coef,m->exp);
}
else{
printf("%f,%d",m->coef,m->exp);
}
m = m->next;
}
free(La);
free(Lb);
}//将两个式子合并并输出
int main(){
Node *p1 = List();
Node *p2 = List();
add(p1,p2);
return 0;
}