#include <iostream>
#include <stdlib.h>
using namespace Std;
typedef struct lnode{
float Coef; Coefficient
int expn; Index
struct Lnode *next; Pointer field
}lnode, *slink;
int main ()
{
Unary polynomial addition, input two unary polynomial LA, Lb, output two unary polynomial, and LC
Slink La, Lb, Lc, PA, Pb, PC, RA, RB, q;
int m, N, J;
cout << "Please enter the number of items in a unary polynomial la:";
Cin >> m;
La = new Lnode;
RA = La;
if (! La) exit (0); Storage space allocation failed
cout << "input" << m << "coefficient index of items:";
for (j = 0; J < m; J + +)
{
PA = new Lnode; To generate a new node
CIN >> pa->coef>> pa->expn;
Ra->next = PA; RA = pa; Insert to table header
}
Ra->next = NULL;
cout << "Please enter the number of items in a unary polynomial lb:";
CIN >> N;
Lb = new Lnode;
RB = Lb;
if (! Lb) exit (0); Storage space allocation failed
cout << "Input" << n << "coefficient index of items:";
for (j = 0; J < N; j + +)
{
PB = new Lnode; To generate a new node
CIN >> Pb->coef >> pb->expn;
Rb->next = PB; RB = PB; Insert to table header
}
M + = n;
Rb->next = NULL;
PA = la->next; PB = lb->next; Lc = PC = La; Q = lc->next;
while (PA && pb)//Insert PA,PB node in C at once
{
if (PA->EXPN < PB->EXPN)
{
Pc->next = PA; PC = PA; PA = pa->next;
}
else if (pa->expn > PB->EXPN)
{
Pc->next = PB; PC = PB; PB = pb->next;
}
Else
{
Pa->coef = Pa->coef + pb->coef; Pc->next = PA; PC = PA; PA = pa->next; PB = pb->next;
m--;
}
}
Pc->next = PA? PA:PB;
Delete Lb; Releasing the LB head node
j = 0;
cout << "21-Element polynomial and: Lc =";
while (J < m)
{
cout << q->coef << "x^" << q->expn;
if (J < m-1)
cout << "+";
Q = q->next;
j + +;
}
cout << Endl;
}//mergelist_l
One of the data structures meta-polynomial addition (c + + version)