我这个 一元多项式相加 结果的系数为什么不变呢?(万分感谢)

gengyigang gengyigang 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include "stdlib.h"
typedef struct LNode
{
int coef;
int expn;
struct LNode *next;
}LNode,*ListNode;
ListNode CreateFromTail()
{
ListNode l,p,tail;
int m;
int n;
l=(ListNode)malloc(sizeof(LNode));
l->next=NULL;
tail=l;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
while(m)
{
p=(ListNode)malloc(sizeof(LNode));
p->coef=m;
p->expn=n;
tail->next=p;
tail=p;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
}
tail->next=NULL;
return l;
}
ListNode MergeList(ListNode la,ListNode lb)
{
ListNode ra,rb,pa,pb;
int sum;
ra=la;
rb=lb;
pa=la->next;
pb=la->next;
while(pa&&pb)
{
if(pa->expn>pb->expn)
{
ra=ra->next;
pa=pa->next;
}
else if(pa->expn <pb->expn)
{
rb->next=pb->next;
pb->next=ra->next;
ra->next=pb;
ra=ra->next;
pa=ra->next;
pb=rb->next;
}
else
{
if((pa->coef+pb->coef)!=0)
{
sum=pa->coef+pb->coef;
pa->coef=sum;
ra=ra->next;
pa=pa->next;
rb=rb->next;
pb=pb->next;
}
else
{
pa=pa->next;
pb=pb->next;
ra->next=pa;
rb->next=pb;
}
}
}
if(pb)
ra->next=pb;
free(lb);
return la;
}
main()
{
ListNode m,n,l,p;
printf("the first equation:");
m=CreateFromTail();
printf("\n");
printf("the second equation:");
n=CreateFromTail();
l=MergeList(m,n);
p=l->next;
while(p!=NULL)
{
printf(" %d %d\t",p->coef,p->expn);
p=p->next;
}
}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
gengyigang
数据结构与算法 33,027 成员 35,334 内容