[求助]运行完后是循环,错在哪里
#include<stdio.h>#include<stdlib.h>
typedef struct PNode
{float coef;
int exp;
struct PNode *next;
}linklist;
linklist *creat()
{linklist *head,*p,*q;
float oef;
int xp;
p=head=(linklist*)malloc(sizeof(linklist));
scanf("%f%d",&oef,&xp);
while(xp>=0)
{q=(linklist*)malloc(sizeof(linklist));
q->coef=oef;q->exp=xp;
p->next=q;
p=q;
scanf("%f%d",&oef,&xp);
}
p->next=NULL;
return(head);
}
linklist *plus(linklist *a,linklist *b)
{int i;
linklist *p,*q,*c;
linklist *m,*n;
m=c=(linklist *)malloc(sizeof(linklist));
p=a->next;
q=b->next;
while(p&&q)
{n=(linklist*)malloc(sizeof(linklist));
if(p->exp==q->exp)
if(p->exp+q->exp!=0) {n->coef=p->coef+q->coef;n->exp=p->exp;p=p->next;q=q->next;}
else {p=p->next;q=q->next;}
else if(p->exp<q->exp) {n->coef=p->coef;n->exp=p->exp;p=p->next;}
else {n->coef=q->coef;n->exp=q->exp;q=q->next;}
m->next=n;
m=n;
}
while(p)
{n=(linklist *)malloc(sizeof(linklist));
n->coef=p->coef;n->exp=p->exp;
p=p->next;
m->next=n;
m=n;
}
while(q)
{n=(linklist *)malloc(sizeof(linklist));
n->coef=q->coef;n->exp=q->exp;
q=q->next;
m->next=n;
m=n;
}
m->next=NULL;
return(c);
}
main()
{linklist *a,*b,*c,*p,*q;
a=creat();
b=creat();
c=plus(a,b);
p=c->next;
while(p)
{if(p->coef>0) printf("+");
printf("%fe%d",p->coef,p->exp);
}
printf("\n");
}
运行完后是死循环,请帮忙检查一下错误