Home > Back-end >  The addition of a polynomial, ask everybody to give directions
The addition of a polynomial, ask everybody to give directions

Time:09-29

#include
# include & lt; Stdlib. H>

# define TRUE 1
# define FALSE 0
# define OK 1
# define the ERROR - 1
# define OVERFLOW - 2
typedef int Status;

Typedef struct
{
//item, according to polynomial entries as the data elements of LinkList
Int coef.//COEFFICIENT COEFFICIENT
Int expn;//index EXPONENT
} the term ElemType;

Typedef struct LNode//node type
{
ElemType data;
Struct LNode * next;

} * Link, * Position;
Typedef struct//type list
{
Link a head, tail;//in the table refer to linear chain head nodes and the last
Int len.//points to the linear list the number of data elements in the
} LinkList;

Typedef LinkList polynomial;//in order list according to polynomial with header node

Please fill in the answer here */*/
The Status InitList (Link & amp; L)//build node
{
L=(struct LNode *) malloc (sizeof (struct LNode));//generated head node
if(! L)
return ERROR;
L - & gt; Next=NULL;
return OK;

}
The Position GetHead (Link) L//returns the Position of the head node in the L
{
The Link p;
P=L;
return p;
If (p==NULL)
return NULL;


}
The Status SetCurElem (Link & amp; P, ElemType e)//e update node p points to the value of the data element
{
E=p - & gt; The data;
return OK;


}
ElemType GetCurElem (Link p)//returns the value of the p points to data
{
The return p - & gt; The data;

}
The Status MakeNode (Link & amp; P, ElemType e)//build node
{
P=(Link) malloc (sizeof (LNode));
if(! P)
return ERROR;
P - & gt; data=https://bbs.csdn.net/topics/e;
P - & gt; Next=NULL;
return OK;

}
The Status InsFirst (Link & amp; L, h Link, the Link s)//will be referred to in s after the node is inserted into the h
{
S - & gt; Next=h - & gt; Next;
H - & gt; Next=s;
return OK;


}
Int CMP (term a, term b)
{
If (a.e xpnreturn -1;
Else if (a.e XPN==b.e XPN)
return 0;
The else
return 1;

}
The Status DelFirst (Link & amp; L, h Link, the Link & amp; Q)//remove h after the first node and return to q
{
Q=h - & gt; Next;
H - & gt; Next=q - & gt; Next;
Free (q);
return OK;

}
Void FreeNode network (Link & amp; P)//release node
{
P=NULL;

}

Position NextPos (Link L, Link p)//return p points to the node's immediate successor Position
{
If (p - & gt; Next!=NULL)
The return p - & gt; Next;
return NULL;


}
The Status LocateElem (Link L, ElemType e, the Position & amp; Q, the Status (* compare) (ElemType ElemType))
{
int i;
The Link p;
I=1;//I the initial value for the first element of a sequence of
P=L - & gt; Next;//p of the location of the initial value for the first element
While (p& & ! Compare (*) (p - & gt; Data, e))
{
++i;
P=p - & gt; Next;
}
If (p)
return TRUE;
The else
return FALSE;


}
Void CreatePolyn (Link & amp; P, int m)
{
InitList (P);
The Link h=GetHead (P);
ElemType e;
The Link q, s;
E.c. with our fabrication: oef=0.0;
E.e XPN=1;
SetCurElem (h, e);
int i;
for(i=1; I<=m; I++)
{
The scanf (" % d % d ", & amp; E.c. with our fabrication: oef, & amp; E.e XPN);
if(! LocateElem (P, e, q, CMP))
{
If (MakeNode (s, e))
InsFirst (P, q, s);

}
}
}
The Status Append (Link & amp; L, the Link s)
{
The Link p;
P=L;
While (p)
{
P=p - & gt; Next;
}
P - & gt; Next=s;
return OK;


}
The Status ListEmpty (Link L)
{
If (L==NULL)
return TRUE;
return FALSE;

}
Void AddPolyn (Link & amp; Pa, the Link & amp; Pb)
{
The Link ha, hb, qa and qb;
ElemType a, b;
ElemType sum;
Ha=GetHead (Pa);//ha, hb to Pa and Pb head node;
Hb=GetHead (Pb);
Qa=NextPos (Pa, ha);//qa and qb to Pa and Pb of the current node
Qb=NextPos (Pb, hb);
While (qa& & Qb)//are non-null
{
A=GetCurElem (qa);
B=GetCurElem (qb);
The switch (CMP (a, b))
{
Case 1://Pa small
Ha=qa;
Qa=NextPos (Pa, qa);
break;
Case 0:
The sum. Coef=a.c oef + biggest oef.
The sum. Expn=a.e XPN;
If (sum coef!=0.0)
{
SetCurElem (qa, sum);//modify Pa coefficient value;
Ha=qa;

}
The else//delete node
{
DelFirst (Pa, ha, qa);
FreeNode network (qa);


}
DelFirst (Pb, hb, qb);//delete nodes in a Pb
FreeNode network (qb);
Qb=NextPos (Pb, hb);
Qa=NextPos (Pa, ha);
break;
Case 1://pb index value of the small
DelFirst (Pb, hb, qb);
InsFirst (Pa, ha, qb);
Qb=NextPos (Pb, hb);
Ha=NextPos (Pa, ha);
break;




}
if(! ListEmpty (Pb))
{
Append (Pa, qb);

}
FreeNode network (hb);

}


}

Int main ()
{
The Link Pa, Pb;

Int m, n;
The Position ha, hb, qa and qb;
//term a;
The scanf (" % d ", & amp; M);
CreatePolyn (Pa, m);
The scanf (" % d ", & amp; N);
CreatePolyn (Pb, n);
AddPolyn (Pa, Pb);
If (Pa==NULL)
{
Printf (" 0 \ n ");
return 0;
}
Ha=GetHead (Pa);//ha and hb to Pa and Pb respectively head node
Qa=NextPos (Pa, ha);
While (qa)
{
Printf (" % d, % d \ n ", qa - & gt; Data. Coef, qa - & gt; Data. The expn);
Ha=qa;
Qa=NextPos (Pa, ha);
}
return 0;
}
  • Related