/* Your function will be put here */
Polynomial Add( Polynomial a, Polynomial b )
{
if(!a->Next)return b;
if(!b->Next)return a;
Polynomial head = malloc(sizeof(Polynomial));
Polynomial p = head->Next;
a = a->Next;
b = b->Next;
while(a&&b)
{
Polynomial pc = malloc(sizeof(Polynomial));
if(a->Exponent==b->Exponent)
{
pc->Coefficient = a->Coefficient + b->Coefficient;
if(!pc->Coefficient)
{
a=a->Next;
b=b->Next;
continue;
}
pc->Exponent = a->Exponent;
pc->Next = NULL;
p->Next = pc;
p = pc;
a=a->Next;
b=b->Next;
}
else
{
if(a->Exponent > b->Exponent)
{
pc->Coefficient = a->Coefficient;
pc->Exponent = a->Exponent;
pc->Next = NULL;
p->Next = pc;
p = pc;
a = a->Next;
}
else
{
pc->Coefficient = b->Coefficient;
pc->Exponent = b->Exponent;
pc->Next = NULL;
p->Next = pc;
p = pc;
b = b->Next;
}
}
}
while(a)
{
Polynomial pc = malloc(sizeof(Polynomial));
pc->Coefficient = a->Coefficient;
pc->Exponent = a->Exponent;
pc->Next = NULL;
p->Next = pc;
p = pc;
a = a->Next;
}
while(b)
{
Polynomial pc = malloc(sizeof(Polynomial));
pc->Coefficient = b->Coefficient;
pc->Exponent = b->Exponent;
pc->Next = NULL;
p->Next = pc;
p = pc;
b = b->Next;
}
if(p==head->Next)
p ->Next = NULL;
return head;
}