#include <iostream>
#include <stdlib.h>
using namespace std;
struct item
{
int a;
int m;
struct item *next;
};
item *node1()
{
item *p=NULL;
item *q=NULL;
item *first=NULL;
first=NULL;
int A;
cout<<"请输入node1的多项式:"<<endl;
cout<<"请输入a的值:";
cin>>A;
while(A!=0)
{
p=new item;
p->a=A;
cout<<"请输入m的值:";
cin>>p->m;
if(first==NULL)
first=p;
else
q->next=p;
q=p;
cout<<"请输入a的值:";
cin>>A;
}
q->next=NULL;
return (first);
}
item *node2()
{
item *r=NULL;
item *l=NULL;
item *second=NULL;
second=NULL;
int B;
cout<<"请输入多项式node2的项:"<<endl;
cout<<"请输入a的值:";
cin>>B;
while(B!=0)
{
r=new item;
r->a=B;
cout<<"请输入m的值:";
cin>>r->m;
if(second==NULL)
second=r;
else
l->next=r;
l=r;
cout<<"请输入a的值:";
cin>>B;
}
l->next=NULL;
return (second);
}
item *insert(item *mode1,item *mode2)
{
item *k=NULL;
item *n=NULL;
item *s=NULL;
item *t=NULL;
item *first=mode1;
item *second=mode2;
k=first;
s=first;
n=second;
t=second;
while (n->next!=NULL)
{
while(k->m > n->m)
{
s=k;
k=k->next;
}
t=n;
if(k->m==n->m)
k->a=(k->a+n->a);
else
{
n->next=k;
s->next=n;
}
t=t->next;
n=t;
}
if(k->m > n->m)
{
n->next=k;
s->next=n;
}
else if(k->m==n->m)
k->a=n->a+k->a;
else
{
while(k->m<n->m)
{
s=k;
k->next;
}
if(k->m=n->m)
k->a=k->a+n->a;
else
{
n->next=k;
s->next=n;
}
}
first=mode1;
return(first);
}
item print(item *head)
{
while(head!=NULL)
{
cout<<head->a<<endl;
cout<<head->m<<endl;
head=head->next;
}
}
int main(int argc, char *argv[])
{
print(insert(node1,node2));
system("PAUSE");
return 0;
}