node *add(node *&head1,node *head2){//一元多项式相加 (将一元多项式相加的结果存放在head1中)
node *a,*b,*p;
a = head1->next;
b = head2->next;
p = head1;
while(a != NULL && b != NULL){
if(a->exponent == b->exponent){
a->coefficient = a->coefficient+b->coefficient;
p->next = a;
p = p->next;
a = a->next;
b = b->next;
}
else if(a->exponent < b->exponent){
p->next = a;
p = p->next;
a = a->next;
}
else{
p->next = b;
p = p->next;
b = b->next;
}
}
if(a != NULL){//比较结束之后a或b如果有一个还未空就执行下述语句
p->next = a;
}
else{
p->next = b;
}
return head1;
}
一元多项式相加链表
最新推荐文章于 2023-09-23 19:46:02 发布