请那位高人看看我的这个链表排序,下面我以蓝色标出的地方运行出错.
出现的错误是:相加后的结果会再加一次,这是怎么回事呀
void sort(struct term *La)
{ struct term *p,*q,*s,*r;
for(p=La->next;p;p=p->next)
{ for(q=p->next;q;q=q->next)
{if(p->expn>q->expn)
{s=(struct term*)malloc(sizeof(struct term));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q->coef=p->coef;
q->expn=p->expn;
p->coef=s->coef;
p->expn=s->expn;}
else
if(p->expn==q->expn)
{s=(struct term*)malloc(sizeof(struct term));
s->coef=p->coef+q->coef;
s->expn=q->expn;
s->next=NULL;
r->next=(struct term*)malloc(sizeof(struct term));
r->coef=p->coef;
r->expn=p->expn;
r->next=NULL;
free(r);
p->coef=s->coef;
p->expn=s->expn;
free(s);
}
}
}
[原创]链表的排序实现