#include <iostream>
#include <malloc.h>
using namespace std;
typedef struct
{
    float coef;
    int expn;
}ElemType;
typedef struct PolyNode
{
    ElemType data;
    struct PolyNode *next;
}PolyNode,*Polynomial;
void CreatPolyn(Polynomial &P,int m)
{
    Polynomial q;
    q=P->next;
    for(int i=0;i<m;i++)
    {
        cin>>q->data.coef;
        cin>>q->data.expn;
        q=q->next;
    }
}
int PrintLength(Polynomial P)
{
    Polynomial q;
    q=P->next;
    int length=0;
    for(q;!(q->next);q=q->next)
        length++;
    return length;
}
void  PrintPolyn(Polynomial P)
{
    Polynomial q;
    q=P->next;
    for(q;!q->next;q=q->next)
    {
       cout<<P->data.coef;
       cout<<P->data.expn;
    }
}
void DestoryPolyn(Polynomial &P)
{
    P->next=NULL;
    free(P);
}
void AddPolyn(Polynomial &Pa,Polynomial &Pb)
{
    Polynomial qa;
    Polynomial qb;
    Polynomial P;
    qa=Pa->next;
    qb=Pb->next;
    P=Pa->next=qa;
    while(!qa&&!qb)
    {
        while((qa->data.expn)<(qb->data.expn))
        {
            P->data=qa->data;
            P=P->next;
            qa=qa->next;
        }
        while((qa->data.expn)>(qb->data.expn))
        {
            P->data=qb->data;
            P=P->next;
            qb=qb->next;
        }
        while((qa->data.expn)=(qb->data.expn))
        {
            P->data.coef=qa->data.coef+qb->data.coef;
            P=P->next;
            qa=qa->next;
            qb=qb->next;
        }
    }
    while(!qa)
        {
            P->data.coef=qb->data.coef;
            P=P->next;
            qb=qb->next;
        }
       while(!qb)
        {
            P->data.coef=qa->data.coef;
            P=P->next;
            qa=qa->next;
        }
        Pa=P;
        free(Pb);
}
void SubtractPolyn(Polynomial &Pa,Polynomial &Pb)
{
    Polynomial qa;
    Polynomial qb;
    Polynomial P;
    qa=Pa->next;
    qb=Pb->next;
    P=Pa->next=qa;
    while(!qa&&!qb)
    {
        while((qa->data.expn)<(qb->data.expn))
        {
            P->data.coef=qa->data.coef;
            P=P->next;
            qa=qa->next;
        }
        while((qa->data.expn)>(qb->data.expn))
        {
            P->data.coef=-(qb->data.coef);
            P=P->next;
            qb=qb->next;
        }
        while((qa->data.expn)=(qb->data.expn))
        {
            P->data.coef=qa->data.coef-qb->data.coef;
            P=P->next;
            qa=qa->next;
            qb=qb->next;
        }
    }
    while(!qa)
        {
            P->data.coef=-(qb->data.coef);
            P=P->next;
            qb=qb->next;
        }
       while(!qb)
        {
            P->data.coef=qa->data.coef;
            P=P->next;
            qa=qa->next;
        }
        free(Pb);
}
int main()
{
    Polynomial Pa,Pb;
    int m,n;
    cout<<"input the length of Pa:";
    cin>>m;
    CreatPolyn(Pa,m);
    cout<<"input the length of Pb:";
    cin>>n;
    CreatPolyn(Pb,n);
    cout<<"output the add of Pa&Pb:";
    AddPolyn(Pa,Pb);
    PrintPolyn(Pa);
    int r=PrintLength(Pa);
    cout<<"output the length of new Pa:"<<r<<endl;
    DestoryPolyn(Pa);
    return 0;

}