```cpp
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef int typelem;
typedef struct node1{
int length;
struct node2 *next;
}headlist,*head_list;
typedef struct node2{
typelem data1;
typelem data2;
struct node2 *next;
}nodelist;
int Initlist(headlist &L)
{
L.length=0;
L.next=NULL;
return 1;
}
int paixu(headlist *L)
{
if(!L)
return 0;
int i, j;nodelist *p,*p1;
typelem t;
for(i=0;i<L->length-1;i++)
{
p=L->next;
p1=p->next;
for(j=0;j<L->length-1-i;j++)
{
if(p->data1<p1->data1)
{
t=p->data1;
p->data1=p1->data1;
p1->data1=t;
t=p->data2;
p->data2=p1->data2;
p1->data2=t;
}
p=p->next;
p1=p1->next;
}
}
return 1;
}
nodelist *find(headlist *L,typelem e1)
{
nodelist *p1=L->next;
while(p1)
{
if(p1->data1==e1)
break;
p1=p1->next;
}
if(p1==NULL)
return NULL;
else
return p1;
}
int Listsert(headlist *p,typelem e1,typelem e2)
{
if(!p)
return 0;
nodelist *p1;
p1=(nodelist *)malloc(sizeof(nodelist));
p1->data1=e1;p1->data2=e2;
p->length++;
p1->next=p->next;
p->next=p1;
return 1;
}
int Listcreate(headlist *L)
{
if(!L)
return 0;
typelem e1,e2;int len,i;
cout<<"Input length:";
cin>>len;
for(i=0;i<len;i++)
{ cout<<"input data1(指数):";
cin>>e1;
cout<<"input data2(系数):";
cin>>e2;
if(find(L,e1))
{
cout<<"指数已存在,请重新输入!\n";
i--;
}
else
Listsert(L,e1,e2);
}
paixu(L);
return 1;
}
int Listdelete(headlist *L,typelem e1)
{
if(!L||L->length==0)
return 0;
if(L->next->data1==e1)
{
nodelist *p6=L->next;
L->next=p6->next;
free(p6);
L->length--;
return 1;
}
if(L->length==1)
{
return 0;
}
nodelist *p,*p1;
p=L->next;
p1=p->next;
while(p1->data1!=e1)
{
p=p->next;
p1=p1->next;
if(p1==NULL)
return 0;
}
p->next=p1->next;
L->length--;
free(p1);
return 1;
}
int Listadd(headlist *L1,headlist *L2,headlist *L3)
{
if(!(L1&&L2&&L3))
return 0;
if(L3->length||L1->length==0||L2->length==0)
return 0;
nodelist *p1,*p2,*p3,*p4;
p1=L1->next;p2=L2->next;
while(!(p1==NULL&&p2==NULL))
{
p4=(nodelist *)malloc(sizeof(nodelist));
if(p2==NULL)
{
goto look1;
}
else if(p1==NULL)
{
goto look2;
}
else if(p1->data1>p2->data1)
{look1:
p4->data1=p1->data1;
p4->data2=p1->data2;
p1=p1->next;
}
else if(p1->data1<p2->data1)
{look2:
p4->data1=p2->data1;
p4->data2=p2->data2;
p2=p2->next;
}
else
{
p4->data1=p1->data1;
p4->data2=p1->data2+p2->data2;
p1=p1->next;p2=p2->next;
}
if(L3->length==0)
L3->next=p4;
else
{
p3->next=p4;
}
p3=p4;
L3->length++;
}
p3->next=NULL;
return 1;
}
void print(headlist *L)
{
nodelist *p1=L->next;int j=1;
while(p1!=NULL)
{
cout<<j++<<".data1:"<<p1->data1<<" "<<"data2:"<<p1->data2<<'\n';
p1=p1->next;
}
}
void MergeList(LinkList la, LinkList lb, LinkList lc)
{
LNode *p;
while(la != null && lb != null){
p = (LNode *) malloc (sizeof(LNode));
if(la->data > lb->data){
p->data = lb->data;
lb = lb->next;
}
else{
p->data = la->data;
la = la->next;
}
p->next=lc->next;
lc->next=p;
}
while(la != null){
p = (LNode *) malloc (sizeof(LNode));
p->data = la->data;
la = la->next;
lc->next = p;
lc = p;
}
while(lb != null){
p = (LNode *) malloc (sizeof(LNode));
p->data = lb->data;
la = lb->next;
lc->next = p;
lc = p;
}
}
int main()
{
headlist L1,L2,L3;
Initlist(L1);Initlist(L2);Initlist(L3);
Listcreate(&L1);Listcreate(&L2);
Listadd2(&L1,&L2,&L3);
print(&L3);
}