Calculation of a single-dimensional Polynomial
Take the addition operation of a polynomial as an example:
A and B can be expressed as linear linked lists:
The linked list of "and polynomial" is as follows (the long square box in the figure indicates the released node ):
# Include <stdio. h> # include <stdlib. h> typedef struct Polyn {int data; int index; struct Polyn * next;} Polyn, * Polynlist; void CreatPolyn (Polynlist & p, int m) // input the coefficients and exponent of m items and establish a polynomial P {// There is a header node Polyn * q = p; for (int I = 0; I <m; I ++) {q-> next = (Polyn *) malloc (sizeof (Polyn); q = q-> next; // printf ("% d coefficient, index:", I + 1); scanf ("% d", & q-> data, & q-> index);} q-> next = 0;} void PrintPolyn (Polynlist & p) // print the unary polynomial P {Polyn * q = p-> next; // (X ^ 0) and (x ^ 1) perform special processing if (q-> index = 0) {printf ("% d", q-> data ); // The second index may be x q = q-> next; if (q & q-> index = 1) {if (q-> data! = 1) printf ("% + dx", q-> data); // Add + to the front of the positive number; otherwise, do not add else printf ("+ x "); q = q-> next; }}else if (q-> index = 1) {if (q-> data! = 1) printf ("% dx", q-> data); else printf ("x"); q = q-> next ;} else {printf ("% dx ^ % d", q-> data, q-> index); q = q-> next;} while (q) {printf ("% + dx ^ % d", q-> data, q-> index); q = q-> next;} printf ("\ n ");} void CopyPolyn (Polynlist & q, Polyn * r) // NULL pointer must be referenced to create a new node linked list and copy (data and pointer fields) {q = (Polyn *) malloc (sizeof (Polyn); Polyn * q1 = q, * r1 = r-> next; while (r1) {q1-> next = (Polyn *) malloc (sizeof (Polyn); q1 = q1-> next; q1-> Data = r1-> data; q1-> index = r1-> index; r1 = r1-> next;} q1-> next = NULL;} void ShowMenu () {printf ("\ t \ t1. polynomial addition \ n"); printf ("\ t \ t2. polynomial subtraction \ n "); printf ("\ t \ t3. polynomial multiplication \ n"); printf ("\ t \ t4. exit \ n");} void AddPolyn (Polyn * p, polyn * p1) // completes polynomial addition, p = p + p1, and destroys p1 {Polyn * qa, * qb, * prior, * r; // set prior to use prior = p; qa = p-> next; qb = p1-> next; while (qa & qb) to insert A linked list to A node in the B linked list) {if (qa-> index <qb-> index) {prior = qa; Qa = qa-> next;} else if (qa-> index> qb-> index) {r = qb; qb = qb-> next; // extract qb to the r-> next = qa; prior-> next = r; // update prior = r ;} else {if (qa-> data + qb-> data! = 0) {// modify the system value of the node indicated by qa, and release the node qa-> data = qa-> data + qb-> data; r = qb; qb = qb-> next; free (r);} else {// release the node r = qa in the pointer qa and qb; qa = qa-> next; prior-> next = qa; // Link free (r); r = qb; qb = qb-> next; free (r );}}} // The B linked list also has the node if (qb) {prior-> next = qb; // at this time, the qa has a null pointer} free (p1 ); // Release B header node} void reverse (Polyn * p) {Polyn * q = p-> next; while (q) {q-> data =-(q-> data); q = q-> next ;}} void DestroyPolyn (Polyn * r) {Polyn * q; do {q = r; r = r-> next; free (q);} while (r);} void AdjustPolyn (Polyn * r, int data, int index) // adjust each item {Polyn * q = r-> next; while (q) {q-> data * = data; q-> index + = index; q = q-> next ;}} void SubtractPolyn (Polynlist & p, Polynlist & p1) // completes polynomial subtraction, p = p-p1, and destroys p1 {reverse (p1 ); // reverse AddPolyn (p, p1);} void MultiplyPolyn (Polynlist & p, Polyn * p1) of the data field of the B linked list // complete the polynomial Multiplication operation, p = p * p1, and destroy p1 {Polyn * q, * r = NULL, * r1 = NULL; q = p1-> next; // A (x) -- "bi * A (x) * x ^ ei CopyPolyn (r, p); AdjustPolyn (r, q-> data, q-> index ); q = q-> next; while (q) {CopyPolyn (r1, p); AdjustPolyn (r1, q-> data, q-> index); AddPolyn (r, r1); // r1 will be destroyed r1 = NULL; // r1 must be left empty q = q-> next;}/r --> p, release the r linked list DestroyPolyn (p); p = NULL; CopyPolyn (p, r); DestroyPolyn (r) ;}int main () {int m; polyn * p = (Polyn *) malloc (sizeof (Polyn); Polyn * p1 = (Polyn *) malloc (sizeof (Polyn); Polyn * p2 = NULL, * p3 = NULL; printf ("coefficient of input polynomial A (x) and B (x) by power-up, exponent \ n"); number of items of printf ("A (x: "); scanf (" % d ", & m); CreatPolyn (p, m); printf (" A (x) = "); PrintPolyn (p ); printf ("B (x) number of items:"); scanf ("% d", & m); CreatPolyn (p1, m); printf ("B (x) = "); PrintPolyn (p1); // stores the original polynomials A and B. The replication here is the newly opened node linked list CopyPolyn (p2, p); CopyPolyn (p3, p1 ); system ("pause"); system ("cls"); printf ("A (x) ="); PrintPolyn (p); printf ("B (x) = "); PrintPolyn (p1); printf (" select the following Operation Method: \ n "); ShowMenu (); do {printf (" Enter your selection: "); scanf (" % d ", & m); switch (m) {case 1: AddPolyn (p, p1); printf (" A (x) + B (x) = "); PrintPolyn (p); break; case 2: SubtractPolyn (p, p1); printf (" A (x)-B (x) = "); PrintPolyn (p); break; case 3: MultiplyPolyn (p, p1); printf (" A (x) * B (x) = "); printPolyn (p); break; default: exit (0);} fflush (stdin); DestroyPolyn (p); p1 = p = NULL; // at this time, p and p1 are free pointers and must be set to null CopyPolyn (p, p2); CopyPolyn (p1, p3) ;}while (1); return 0 ;}
Running result:
Press Enter