#include<iostream>
using namespace std;
struct dlist{
int exp;
int data;
dlist *next;
};
void foundList(dlist *&lista,int a[],int n){
dlist *s,*p;
lista=new dlist;
p=lista;
lista->next=NULL;
for(int i=0;i<2*n;i++){
s=new dlist;
s->data=a[i];
i++;
s->exp=a[i];
s->next=p->next;
p->next=s;
p=s;
}
p->next=NULL;
}
void disPlay(dlist *lista){
dlist *la;
la=lista->next;
cout<<la->data<<"X^"<<la->exp;
la=la->next;
while(la!=NULL){
if(la->data>0){
if(la->data==1){
cout<<"+X^"<<la->exp;
}
if(la->data==-1){
cout<<"-X^"<<la->exp;
}
else{
cout<<"+"<<la->data<<"X^"<<la->exp;
}
}
else
{
if(la->data==1){
cout<<"X^"<<la->exp;
}
if(la->data==-1){
cout<<"-X^"<<la->exp;
}
else
{cout<<la->data<<"X^"<<la->exp;}
}
la=la->next;
}
}
void Union(dlist *lista,dlist *listb,dlist *&listc){
dlist *la,*lb,*p;
listc=new dlist;
listc->next=NULL;
p=listc;
la=lista->next;
lb=listb->next;
while(la!=NULL&&lb!=NULL){
if(la->exp<lb->exp){
dlist *s;
s=new dlist;
s->exp=la->exp;
s->data=la->data;
p->next=s;
p=s;
la=la->next;
}
if(la->exp>lb->exp){
dlist *s;
s=new dlist;
s->exp=lb->exp;
s->data=lb->data;
p->next=s;
p=s;
lb=lb->next;
}
else{
int add=la->data+lb->data;
if(add==0){
la=la->next;
lb=lb->next;
}
else{
dlist *s;
s=new dlist;
s->exp=la->exp;
s->data=add;
p->next=s;
p=s;
la=la->next;
lb=lb->next;
}
}}
if(la!=NULL){
p->next=la;
}
if(lb!=NULL){
p->next=lb;
}
}
int main(){
int a[6]={2,1,3,5,-6,8};
int b[6]={3,2,4,3,5,8};
dlist *lista,*listb,*listc;
foundList(lista,a,3);
foundList(listb,b,3);
Union(lista,listb,listc);
cout<<"方程:";
disPlay(lista);
cout<<endl<<"方程:";
disPlay(listb);
cout<<endl<<"和为:";
disPlay(listc);
}
using namespace std;
struct dlist{
int exp;
int data;
dlist *next;
};
void foundList(dlist *&lista,int a[],int n){
dlist *s,*p;
lista=new dlist;
p=lista;
lista->next=NULL;
for(int i=0;i<2*n;i++){
s=new dlist;
s->data=a[i];
i++;
s->exp=a[i];
s->next=p->next;
p->next=s;
p=s;
}
p->next=NULL;
}
void disPlay(dlist *lista){
dlist *la;
la=lista->next;
cout<<la->data<<"X^"<<la->exp;
la=la->next;
while(la!=NULL){
if(la->data>0){
if(la->data==1){
cout<<"+X^"<<la->exp;
}
if(la->data==-1){
cout<<"-X^"<<la->exp;
}
else{
cout<<"+"<<la->data<<"X^"<<la->exp;
}
}
else
{
if(la->data==1){
cout<<"X^"<<la->exp;
}
if(la->data==-1){
cout<<"-X^"<<la->exp;
}
else
{cout<<la->data<<"X^"<<la->exp;}
}
la=la->next;
}
}
void Union(dlist *lista,dlist *listb,dlist *&listc){
dlist *la,*lb,*p;
listc=new dlist;
listc->next=NULL;
p=listc;
la=lista->next;
lb=listb->next;
while(la!=NULL&&lb!=NULL){
if(la->exp<lb->exp){
dlist *s;
s=new dlist;
s->exp=la->exp;
s->data=la->data;
p->next=s;
p=s;
la=la->next;
}
if(la->exp>lb->exp){
dlist *s;
s=new dlist;
s->exp=lb->exp;
s->data=lb->data;
p->next=s;
p=s;
lb=lb->next;
}
else{
int add=la->data+lb->data;
if(add==0){
la=la->next;
lb=lb->next;
}
else{
dlist *s;
s=new dlist;
s->exp=la->exp;
s->data=add;
p->next=s;
p=s;
la=la->next;
lb=lb->next;
}
}}
if(la!=NULL){
p->next=la;
}
if(lb!=NULL){
p->next=lb;
}
}
int main(){
int a[6]={2,1,3,5,-6,8};
int b[6]={3,2,4,3,5,8};
dlist *lista,*listb,*listc;
foundList(lista,a,3);
foundList(listb,b,3);
Union(lista,listb,listc);
cout<<"方程:";
disPlay(lista);
cout<<endl<<"方程:";
disPlay(listb);
cout<<endl<<"和为:";
disPlay(listc);
}