Home > Back-end >  (trouble gives the modified code) for the following program singly linked lists to polynomial summat
(trouble gives the modified code) for the following program singly linked lists to polynomial summat

Time:03-16

# include & lt; iostream>
using namespace std;
Typedef struct Node//Node type
{
Int expo;
int coef;
Struct Node * next;
} Node, * LinkList;

Void InitList (LinkList& L)//initialization list
{
L=new Node;
L - & gt; Next=NULL;
}

Void CreateList (LinkList& L, int n)
{
LinkList p, q;
Q=L;
For (int I=1; I & lt;=n; I++)
{
InitList (p);
Cin & gt;> P - & gt; Coef & gt;> P - & gt; Expo;
Q - & gt; Next=p;
Q=p;
}
}

Void ShowList (LinkList& L)
{
LinkList p;
P=L - & gt; Next;
While (p!=NULL)
{
Cout & lt;

P=p - & gt; Next;
}
}

Void AddList (LinkList& L1, LinkList& L2)
{
LinkList p1, p2, p1_pre, temp;//p1_pre for p1 precursors before
P1_pre=L1;
P1=L1 - & gt; Next;
The p2=L2 - & gt; Next;
While (p1!=NULL & amp; & The p2!=NULL)//L1L2 are not empty
{
If (p1 - & gt; Expo==p2 - & gt; Expo)
{
Int e=p1 - & gt; Coef + p2 - & gt; Coef.
Temp=p2;
L2 - & gt; Next=p2 - & gt; Next;
The p2=L2 - & gt; Next;

If (e==0)
{
Temp=p;
P1_pre - & gt; Next=p1 - & gt; Next;
P1=p1_pre - & gt; Next;

}
The else
{
P1 - & gt; Coef=e;
}
}
Else if (p1 - & gt; Expo & lt; The p2 - & gt; Expo)
{

The p2 - & gt; Next=p1_pre - & gt; Next;
P1_pre=p2;
The p2=p2 - & gt; Next;
}
Else if (p1 - & gt; Expo> The p2 - & gt; Expo)
{
P1=p1 - & gt; Next;
P1_pre=p1_pre - & gt; Next;
}
}
If (p2!=NULL)
{
P1_pre - & gt; Next=p2;
L2 - & gt; Next=NULL;
}
}

Int main ()
{
LinkList L1 and L2.
Int n1, and n2;
InitList (L1);
InitList (L2);
Cin & gt;> N1 & gt;> N2.
CreateList (L1, n1);
CreateList (L2, n2);
AddList (L1 and L2);
ShowList (L1);
return 0;
}

CodePudding user response:

Fyi:
//list of one yuan polynomial addition subtraction multiplication 
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
Typedef struct node {
Float coef.//coefficient
Int expn;//index
Struct node * next;
}
PolyNode;//polynomial polynomial node node
Typedef PolyNode * Polynomial;
Polynomial createPolynomial () {//create a Polynomial
PolyNode * p * q, * head=(PolyNode *) malloc (sizeof (PolyNode));//head node
The head - & gt; Next=NULL;
Float coef.
Int expn;
Printf (" input the polynomial of each coefficient and index, and each row, enter 0 0 over! \n");
While (the scanf (" % f, % d ", & amp; Coef, & amp; Expn) & amp; & Coef) {//by default, according to the descending index
If (the head - & gt; Next) {
P=the head;
While (p - & gt; Next & amp; & Expn & lt; P - & gt; Next - & gt; Expn)
P=p - & gt; Next;
If (p - & gt; Next) {
If (expn==p - & gt; Next - & gt; Expn) {//have the same index directly add to the original polynomial coefficient
P - & gt; Next - & gt; Coef +=coef;
If (p - & gt; Next - & gt; Coef & gt; 0.000001 & amp; & P - & gt; Next - & gt; Coef & lt; 0.000001) {//if the combined coefficient is 0, then abandon the node
Q=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
Free (q);
}
} else {
Q=(PolyNode *) malloc (sizeof (PolyNode));
Q - & gt; Coef=coef;
Q - & gt; Expn=expn;
Q - & gt; Next=p - & gt; Next;
P - & gt; Next=q;
}
} else {
P - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
P=p - & gt; Next;
P - & gt; Coef=coef;
P - & gt; Expn=expn;
P - & gt; Next=NULL;
}
} else {
The head - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
The head - & gt; Next - & gt; Coef=coef;
The head - & gt; Next - & gt; Expn=expn;
The head - & gt; Next - & gt; Next=NULL;
}
}
Return the head;
}
Polynomial multiply (Polynomial poly real, float coef, int expn) {//Polynomial with specified monomial multiplication, the monomial for coefx ^ expn
PolyNode * p, * q * Poly real=(PolyNode *) malloc (sizeof (PolyNode));
P=Poly real;
Q=poly real - & gt; Next;
While (q) {
P - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
P=p - & gt; Next;
P - & gt; Coef=(q - & gt; Coef * coef);
P - & gt; Expn=(q - & gt; Expn + expn);
Q=q - & gt; Next;
}
P - & gt; Next=NULL;
Return Poly real;
}
Void the add (Polynomial poly1, Polynomial poly2) {//add to the poly1 poly2
PolyNode * p * q * r;
R=poly1;
P=poly1 - & gt; Next;//points to the first node
Q=poly2 - & gt; Next;
Poly2 - & gt; Next=NULL;
While (p & amp; & Q) {
If (p - & gt; Expn & gt; Q - & gt; Expn) {
R - & gt; Next=p;
P=p - & gt; Next;
R=r - & gt; Next;
} else if (p - & gt; Expn & lt; Q - & gt; Expn) {
R - & gt; Next=q;
Q=q - & gt; Next;
R=r - & gt; Next;
} else {
PolyNode * t;
P - & gt; Coef +=q - & gt; Coef.
if (! (p - & gt; Coef & gt; 0.000001 & amp; & P - & gt; Coef & lt; 0.000001))//coefficient 0
{
R - & gt; Next=p;
R=r - & gt; Next;
P=p - & gt; Next;
} else {
T=p;
P=p - & gt; Next;
Free (t);
}
T=q;
Q=q - & gt; Next;
Free (t);
}
}
If (p)
R - & gt; Next=p;
If (q)
R - & gt; Next=q;
}
Polynomial polySubtract (Polynomial poly1, Polynomial poly2) {//Polynomial subtraction poly1 - poly2 to form a new Polynomial
//take opposite, poly2 coefficient of forming a new polynomial
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related