#include
#include
#include
#define OverFlow -1
#define OK 1
typedef struct
{
int coe;
int exp;
}Elemtype;
typedef struct node
{
Elemtype data;
struct node *next;
}PNode,*Linklist;
int Contruct_Tables(Linklist *Head)
{
Linklist p;
p=(Linklist)malloc(sizeof(PNode));
if(p==NULL)
return OverFlow;
p->next=NULL;
*Head=p;
return OK;
}
int Insert_Last(Linklist Head, int n)
{
Elemtype x;
int i=0;
Linklist p,q;
int k=0;
p=(Linklist)malloc(sizeof(PNode));
if(p==NULL)
return OverFlow;
while(i
{
printf("输入系数:\n");
scanf("%d",&x.coe);
printf("输入指数:\n");
scanf("%d",&x.exp);
p->data.coe=x.coe;
p->data.exp=x.exp;
q=Head;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
i++;
}
return OK;
}
Linklist Add(Linklist Head1,Linklist Head2)
{
Linklist p=Head1;
Linklist q=Head2;
Linklist r;
while(p->next!=NULL)
{
p=p->next;
}
p->next=q->next;
free(Head2);
p=Head1->next;
while(p->next!=NULL)
{
while(q->next!=NULL)
{
q=Head1->next;
r=q;
q=q->next;
if(p->data.exp==q->data.exp)
{p->data.coe=p->data.coe+q->data.coe;
r->next=q->next;
free(q);
}
}
}
p=p->next;
return Head1;
}
int Show_Linklist(Linklist Head)
{
Linklist p=Head->next;
Linklist q=p;
Linklist r=Head;
while(p->next!=NULL)
{
while(q->next!=NULL)
{
q=q->next;
if(p->data.exp>q->data.exp)
{
p->next=q->next;
q->next=p;
r->next=q;
}
}
r=r->next;
p=p->next;
}
p=Head->next;
if(p==NULL);
printf("空表");
while(p!=NULL&&p->next!=NULL)
{
printf("%dx^%d+",p->data.coe,p->data.exp);
}
if(p!=NULL&&p->next==NULL)
printf("%dx^%d",p->data.coe,p->data.exp);
return OK;
}
int main()
{
int Contruct_Tables(Linklist *Head);
int Insert_Last(Linklist Head,int n);
Linklist Add(Linklist Head1,Linklist Head2);
int Show_Linklist(Linklist Head);
int n;
int p;
Linklist Head1;
Linklist Head2;
printf("建立表1\n");
Contruct_Tables(&Head1);
printf("输入元素个数:\n");
scanf("%d",&n);
Insert_Last(Head1,n);
printf("建立表2\n");
Contruct_Tables(&Head2);
printf("输入元素个数:\n");
scanf("%d",&p);
Insert_Last(Head2,p);
Add(Head1,Head2);
Show_Linklist(Add(Head1,Head2));
return OK;
}