# 利用单链表实现一元多项式的表示及相加

#include <iostream>

using namespace std;
//定义结构体；
typedef struct Polynode
{
int coef;
int exp;
struct Polynode *next;
}Polynode,*Polylist;

//创建关于多项式结构体的单链表，以系数0结束输入
Polylist Polycreate()
{
int c,e;
scanf("%d %d",&c,&e);
while(c!=0)
{
s=(Polynode *)malloc(sizeof(Polynode));
s->coef=c;
s->exp=e;
rear->next=s;
rear=s;
scanf("%d %d",&c,&e);
}//尾插法建立单链表
rear->next=NULL;
}

Polylist Polyadd(Polylist polya,Polylist polyb)
{
int sum;
p=polya->next;
q=polyb->next;
while(p!=NULL&&q!=NULL)
{
s=(Polynode *)malloc(sizeof(Polynode));
if(p->exp<q->exp)
{
s->coef=p->coef;
s->exp=p->exp;
tail->next=s;
tail=s;
p=p->next;
}
else if(p->exp==q->exp)
{
sum=p->coef+q->coef;
if(sum!=0)
{
s->coef=sum;
s->exp=p->exp;
tail->next=s;
tail=s;
p=p->next;
q=q->next;
}
}
else
{
s->coef=q->coef;
s->exp=q->exp;
tail->next=s;
tail=s;
q=q->next;
}
}
if(p!=NULL)
tail->next=p;
else
tail->next=q;
}

//打印多项式
void show(Polylist poly)
{
Polynode *p;
int c;
int e;
p=poly->next;
while(p->next!=NULL)//避免最后一项也出现加号
{
c=p->coef;
e=p->exp;
printf("%dX^%d+",c,e);
p=p->next;
}
c=p->coef;
e=p->exp;
printf("%dX^%d",c,e);
}
int main()
{
printf("请按升幂输入多项式A,以系数为0结束：\n");
Polylist pa=Polycreate();
printf("你输入的多项式为：\n");
show(pa);
printf("\n");
printf("请按升幂输入多项式B,以系数为0结束：\n");
Polylist pb=Polycreate();
printf("你输入的多项式为：\n");
show(pb);
printf("\n");