#include<iostream.h>
#include<stdlib.h>
#include"stdio.h"
typedef struct
{ float coef;//结点类型
int expn;
}polynomial;
typedef struct LNode
{ polynomial data;//链表类型
struct LNode *next;
}LNode,*Link;
void createLink(Link &L,int n);
void printList(Link L);
void addPolyn(Link &pc,Link pa,Link pb);
void substractPolyn(Link &pc,Link pa,Link pb);
void copyLink(Link &pc,Link pa);
void mulPolyn(Link &pc,Link pa,Link pb);
int locateLink(Link pa,Link e);
void destroyLink(Link &L);
int compareNum(int i);
void destroyLink(Link &L)
{
Link p;
p=L->next;
while(p)
{
L->next=p->next;
delete p;
p=L->next;
}
delete L;
L=NULL;
}
void createLink(Link &L,int n)
{
if(L!=NULL){destroyLink(L);}
Link p,newp;
//int k=0;
L=new LNode;
L->next=NULL;
(L->data).expn=-1;//创建头结点
p=L;
/*newp=new LNode;
cout<<"请输入第1项的系数和指数:"<<endl;
cout<<"系数:";
cin>>(newp->data).coef;
cout<<"指数:";
cin>>(newp->data).expn;
p->next=newp;
newp->next=NULL;*/
for(int i=1;i<=n;i++)
{
newp=new LNode;
cout<<"请输入第"<<i<<"项的系数和指数:"<<endl;
cout<<"系数:";
cin>>(newp->data).coef;
cout<<"指数:";
cin>>(newp->data).expn;
if(newp->data.expn<0){cout<<"您输入有误,指数不允许为负值!"<<endl;delete newp;i--;continue;}
newp->next=NULL;
p=L;
if(newp->data.coef==0){cout<<"系数为零,重新输入!"<<endl;delete newp;i--;continue;}
// if(i==1) {p->next=newp;continue; }
while((p->next!=NULL)&&((p->next->data).expn<(newp->data).expn))
{p=p->next;}
if(!locateLink( L, newp))
{
newp->next=p->next;
p->next=newp;
}
else
{
cout<<"输入的该项指数与多项式中已存在的某项相同,请重新创建一个正确的多项式"<<endl;
delete newp;
destroyLink(L);
createLink(L,n);
break;
}
}
}
/*判断指数是否与多项式中已存在的某项相同*/
int locateLink(Link L,Link e)
{
Link p;
p=L->next;
while(p!=NULL