A single-chain table of a linear table can be added to a single-dimensional polynomial.
Definition of a polynomial: a0, a1, a2 ,..., An-1 and an are all numbers in field F (Note: 1, 2 ,......, N-1, n are the scalar) of a, n is a non-negative integer, then the expression anx ^ n + an-1x ^ N-1) +... + A2x ^ 2 + a1x + a0 (an =0) (1) is called the polynomial of the character x on the number field F or the unary polynomial. In polynomial (1), a0 is called a zero polynomial or constant term. a1x is called a single term. Generally, aix is called an I term, and ai is called a coefficient of an I term. Symbol f (x), g (x ),... . Let's talk about the idea. We can use a linked list with two data elements to implement addition operations. The data domains store coef (coefficient) and exp (INDEX) respectively, and compare the index size of the current data domains of the two linked lists during calculation, in three cases, properly assemble and store the linked list to the original linked list LA (note that the polynomial indexes in this issue are arranged in ascending order )., The Code is as follows:
# Include <iostream> using namespace std; typedef struct Polynode {int coef; int exp; struct Polynode * next;} Polynode, * Polylist; void init_linklist (Polylist * L) /* initialize a single-chain table */{* L = (Polylist) malloc (sizeof (Polynode);/* apply for Node space */(* L) -> next = NULL;/* empty table */} void polycreate (Polylist head) {Polynode * rear, * s; int c, e; rear = head; /* rear always points to the end of a single-chain table to facilitate table creation by means of tail insertion */cin> c> e;/* type the polynomial coefficient and exponent */while (c! = 0)/* If c = 0, it indicates that the input of the polynomial ends */{s = (Polynode *) malloc (sizeof (Polynode )); /* apply for a new node */s-> coef = c; s-> exp = e; rear-> next = s; /* Insert at the end of the current table */rear = s; cin> c> e;} rear-> next = NULL; /* set next of the last node of the table to NULL to show that the table ends */} void polyadd (Polylist polya, Polylist polyb)/* this function is used to add two polynomials, then, the polynomial and the polynomial are stored in the polynomial polya, And the polynomial ployb is deleted */{Polynode * p, * q, * pre, * temp; int sum; p = polya-> next;/* point p and q to the first node in the polya and polyb polynomial linked lists respectively */q = polyb-> next; pre = polya; /* r point and Polynomial */While (p! = NULL & q! = NULL)/* When neither polynomial is scanned */{if (p-> exp <q-> exp) /* If the exponent of the polynomial term pointed to by p is less than the exponent of q, add the p node to the polynomial */{pre-> next = p; pre = p; p = p-> next;} elseif (p-> exp = q-> exp)/* If the exponent is equal, then the corresponding coefficients are added */{sum = p-> coef + q-> coef; if (sum! = 0) {p-> coef = sum; pre-> next = p; pre = p; p = p-> next; temp = q; q = q-> next; free (temp);} else {temp = p; p = p-> next; free (temp);/* If the coefficient is zero, delete the nodes p and q, point the pointer to the next node */temp = q; q = q-> next; free (temp) ;}} else {pre-> next = q; pre = q; /* Add the q node to the and polynomial */q = q-> next ;}} if (p! = NULL)/* if there is still surplus in polynomial A, add the remaining knots to and polynomials */pre-> next = p; else/* otherwise, add the nodes in B to the and polynomial */pre-> next = q;} void print (Polylist L) {Polylist p; p = L-> next; while (p! = NULL) {cout <p-> coef <"" <p-> exp <"; p = p-> next ;}cout <endl ;} int main () {Polylist LA, LB; init_linklist (& LA); init_linklist (& LB); polycreate (LA); print (LA); polycreate (LB ); print (LA); polyadd (LA, LB); print (LA); return 0 ;}It's a classic story. Simple questions about the test may be used.
A single-chain table is used to add a polynomial.
I hope you can give me some extra points. I have been debugging for about two hours.
Did you write this program ??
If so, I hope you will strengthen the basic skills of C and have many ideological and Syntactic Problems.
In fact, I didn't help you with the change. Basically I helped you rewrite it.
# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
# Include <string. h>
# Include <ctype. h>
# Include <limits. h>
# Include <math. h>
# Include <process. h>
# Include <iostream. h>
Typedef struct LNode
{/* Storage Structure Definition of polynomials */
Int coef;
Int expn;
Struct LNode * next;
} LNode, * polynomail;
Void creatpolyn (polynomail & p, int m)
{
Printf ("creatpolyncreatpolyncreatpolyncreatpolyny \ n ");
Int I;
Int coef;
Int expn;
Polynomail s;
P = (polynomail) malloc (sizeof (LNode ));
P-> next = NULL;
For (I = 1; I <= m; ++ I)
{
S = (polynomail) malloc (sizeof (LNode ));
Printf ("Enter the coefficients and exponent of a polynomial :");
Scanf ("% d", & coef, & expn );
S-> coef = coef;
S-> expn = expn;
S-> next = p-> next;
P-> next = s;
}
} // CreatPolyn
Polynomail addpolyn (polynomail & pa, polynomail & pb)
{
Polynomail c, pc;
Pc = (polynomail) malloc (sizeof (LNode ));
Pc-> next = NULL;
Pa = pa-> next;
Pb = pb-> next;
While (pa & pb)
{
If (pa-> expn) = (pb-> expn ))
{
C = (polynomail) malloc (sizeof (LNode ));
C-> expn = pa-> expn;
C-> coef = (pa-> coef) + (pb-> coef ));
Pa = pa-> next;
Pb = pb-> next;
} // If
Else if (pa-> expn)> (pb-> expn ))
{
C = (polynomail) malloc (sizeof (LNode ));
C-> expn = pb-> expn;
C-> coef = pb-> coef;
Pb = pb-> next;
} // If
Else
{
C = (polynomail) malloc (sizeof (LNode ));
C->... the remaining full text>
Data structure (C Language) uses a single-chain table to store a single-dimensional polynomial, and implements the addition of two Polynomials
# Include <stdio. h> # include <stdlib. h> # include <malloc. h> typedef int ElemType;/* declaration of single-item linked list */typedef struct PolynNode {int coef; // coefficient int expn; // returns struct PolynNode * next;} PolynNode, * PolynList;/* enter values of n elements in positive order (inserted at the end of the table, create a single-chain linear table with table header structure * // * one-to-one index coefficient */void CreatePolyn (PolynList & L, int n) {int I; PolynList p, q; L = (PolynList) malloc (sizeof (PolynNode); // generate the header node L-> next = NULL; q = L; printf ("% d pairs of input data \ n", n); f Or (I = 1; I <= n; I ++) {p = (PolynList) malloc (sizeof (PolynNode); scanf ("% d ", & p-> coef, & p-> expn); // paired q-> next = p; q = q-> next ;} p-> next = NULL;} // initial condition: the single-chain table L already exists // operation result: Call the function vi () for each data element of L in sequence (). Once vi () fails, the operation fails void PolynTraverse (PolynList L, void (* vi) (ElemType, ElemType) {PolynList p = L-> next; while (p) {vi (p-> coef, p-> expn); if (p-> next) {printf ("+"); // output of "+, there is no "+"} p = p-> next;} printf ("\ n");}/* ListTraverse () function called after the last item (type must be consistent) */void visit (ElemType c, ElemType e) {if (c! = 0) {printf ("% dX ^ % d", c, e); // format each input polynomial}/* polynomial addition, principle: merge * // * parameter: two existing polynomials * // * return value: the first node of the New Polynomial */PolynList Merge ...... remaining full text>