//头文件
#include <stdio.h>
#include <iostream>
#include <malloc.h>
using namespace std;
struct Term
{
float coef; //系数
int expn; //指数
struct Term *next; //指针域
};
typedef struct{//链表类型
Term *link;
int len; //指示线性链表中数据元素的个数
}LinkList;
typedef LinkList polynomial; //用带表头结点的有序链表表示多项式
Term *ha,*hb,*qa,*qb,*p;
LinkList La,Lb;
void CreateList_L(LinkList &L,int n)
{//逆位序输入n个元素的值,建立带表头结点的单链线性表L
// L=(head)malloc(sizeof(LinkList));
L.link=(Term*)malloc(sizeof(Term));
L.link->next =NULL; //先建立一个带头结点的单链表
L.len=0;
cout<<"请按照从高位输入顺序输入该多项式所有项的系数与指数!"<<endl;
for(int i=n;i>0;i--){
p=(Term*)malloc(sizeof(Term)); //生成新结点
cin>>p->coef; //输入系数
cin>>p->expn; //输入指数
p->next=L.link->next;
L.link->next=p; //插入到表头
}
}//CreateList_L
void AddPolyn(polynomial &La,polynomial &Lb)
{//多项式加法:La=La+Lb,利用两个多项式的结点构成“和多项式”。
ha=La.link;hb=Lb.link;
qa=ha->next;qb=hb->next;
while(qa&&qb)
{
if((*qa).expn<(*qb).expn)
{
ha=qa,qa=ha->next;
}
else if((*qa).expn==(*qb).expn)
{
float sum=(*qa).coef+(*qb).coef;
if(sum==0)
{
hb->next=qb->next;
free(qb);