# 单链表实现多项式的存储和加法

63 篇文章 3 订阅
25 篇文章 3 订阅

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

typedef struct node
{
int coef,exp;
struct node *next;

{
int c,e;
cin>>c>>e;
while(c!=0&&e!=0)
{
q->coef=c;
q->exp=e;
q->next=NULL;
p->next=q;
p=p->next;
cin>>c>>e;
}
}

{
pa=LA->next;
pb=LB->next;
pc=LC;
while(pa!=NULL&&pb!=NULL)
{
if(pa->exp<pb->exp)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
if(pa->exp>pb->exp)
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
if(pa->exp==pb->exp)
{
int m=pa->coef+pb->coef;
//cout<<"asjkdf"<<pa->coef<<"jswfaio"<<pb->coef<<endl;
//cout<<"M= "<<m<<endl;
if(m!=0)
{
pa->coef=m;
pc->next=pa;
pc=pa;
pa=pa->next;
pb=pb->next;
free(t);
}
else
{
pb=pb->next;
free(t);
t=pa;
pa=pa->next;
free(t);
}
}
}
if(pa!=NULL)
{
pc->next=pa;
}
else
{
pc->next=pb;
}
}

{
while(p!=NULL)
{
cout<<p->coef<<" "<<p->exp<<"    ";
p=p->next;
}
cout<<endl;
}

int main()
{
h1=Create();
h2=Create();
cout<<"The first equation is: "<<endl;
Display(h1);
cout<<"The second equation is: "<<endl;
Display(h2);
cout<<"The result equation is: "<<endl;
return 0;
}


• 0
点赞
• 9
收藏
觉得还不错? 一键收藏
• 1
评论
12-17 1682
09-25 2478
11-24 1357
10-31
05-22
04-10
11-08
07-06
09-20 341
10-18 1322
01-29 5267

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。