[Application of Data Structure linked list] analysis of the problem by adding and multiplying a polynomial
Polynomial MultiplicationTo multiply the exponent of each item of a polynomial with that of another polynomial.
Code implementation:
Header file and function declaration:
# Ifndef _ POLYN_H # define _ POLYN_H # include <iostream> # include <malloc. h> # include <stdio. h> using namespace std; # define _ CRT_SECURE_NO_DEPRECATE # define NULL 0 typedef struct NODE {float coef; // coefficient int expn; // returns struct NODE * next;} NODE; NODE * Creat (int n); void print (NODE * head); NODE * AddPolyn (NODE * head1, NODE * head2); NODE * Delfirst (NODE * head, NODE * q); void InsertBefore (NODE * p1, NODE * p2); int compare (int a, int B); NODE * CreatLinkList (int n); void menu (); int inputnum (int n); // NODE * reverse (NODE * head); NODE * MulPolyn (NODE * A, NODE * B); bool SortDown (NODE * head ); // sort bool SortUp (NODE * head); // sort void Judge (NODE * head, char x); # endif
Function Definition:
# Include "polyn. h "# include <iostream> void menu () {cout <" "<endl; cout <"*********************************" <<endl; cout <"* [0] quit_system *" <endl; cout <"* [1] polynadd [2] polynmul *" <endl; cout <"*********************************" <<endl; cout <"plese chose:>";} int inputnum (int n) {while (n <= 0) {cout <"the input value is incorrect, make sure that the entered value is an integer greater than 0!: "; Cin> n;} return n;}/* Create a linked list */NODE * Creat (int n) {NODE * current, * previous, * head; int I; head = (NODE *) malloc (sizeof (NODE);/* Create a header NODE */previous = head; for (I = 0; I <n; I ++) {current = (NODE *) malloc (sizeof (NODE); cout <"Enter the coefficient and index:"; cin> current-> coef; cin> current-> expn; previous-> next = current; previous = current;} previous-> next = NULL; return head ;} /* sort */bool SortDown (NODE * head) {NODE * p = NULL; NO DE * q = NULL; for (p = head-> next; p! = NULL; p = p-> next) {for (q = p-> next; q! = NULL; q = q-> next) {if (p-> expn <q-> expn) {p-> expn = p-> expn ^ q-> expn; q-> expn = p-> expn ^ q-> expn; p-> expn = p-> expn ^ q-> expn; p-> coef = p-> coef + q-> coef; q-> coef = p-> coef-q-> coef; p-> coef = p-> coef-q-> coef ;}}return true;} bool SortUp (NODE * head) {NODE * p = NULL; NODE * q = NULL; for (p = head-> next; p! = NULL; p = p-> next) {for (q = p-> next; q! = NULL; q = q-> next) {if (p-> expn> q-> expn) {p-> expn = p-> expn ^ q-> expn; q-> expn = p-> expn ^ q-> expn; p-> expn = p-> expn ^ q-> expn; p-> coef = p-> coef + q-> coef; q-> coef = p-> coef-q-> coef; p-> coef = p-> coef-q-> coef ;}}return true;}/* compare */int compare (int a, int B) {if (a <B) return-1; else if (a> B) return 1; elsereturn 0;}/* delete NODE q */NODE * Delfirst (NODE * p1, NODE * q) {p1-> next = q-> next; return (q);}/* Insert NODE, introduce NODE p, which allows p to be inserted between p2 and p1 */void InsertBefore (NODE * p1, NODE * p2) {NODE * p; p = p1-> next; p1-> next = p2; p2-> next = p;}/* print separately for beautiful programs */void print (NODE * head) {SortDown (head ); NODE * cur; cur = head-> next; while (cur-> next! = NULL) {cout <cur-> coef <"* x ^" <cur-> expn <"+"; cur = cur-> next ;} cout <cur-> coef <"* x ^" <cur-> expn; cout <endl ;}/ * The sum of unary polynomials. Overall consideration is given, the qb index is smaller than or equal to pb (if the sum of coefficients is equal to 0 and not equal to 0 ), or greater than pb, which consists of two small modules: InsertBefore and Delfirst */NODE * AddPolyn (NODE * head1, NODE * head2) {NODE * ha, * hb, * qa, * qb; int a, B; float sum; ha = head1;/* the ha and hb point to the header node */hb = head2; if (ha! = NULL) // determine whether head1 is NULL {if (hb = NULL) {return head1;} else {qa = ha-> next; /* qa and qb point to the next node of the header node */qb = hb-> next; while (qa & qb) /* both qa and qb are not empty */{a = qa-> expn; B = qb-> expn; switch (compare (a, B) {case-1: /* qa-> expn <qb-> expn */ha = qa; qa = qa-> next; break; case 0: sum = qa-> coef + qb-> coef;/* coefficient and */if (sum! = 0.0) {/* if not 0.0 */qa-> coef = sum;/* change coefficient */ha = qa;} else {free (Delfirst (ha, qa);} free (Delfirst (hb, qb); qa = ha-> next; qb = hb-> next; /* after qb is released, assign a new value */break; case 1:/* If qa-> expn> qb-> expn */Delfirst (hb, qb); InsertBefore (ha, qb);/* before inserting qb to the next node of ha */qb = hb-> next; ha = ha-> next; break ;}} if (qb) ha-> next = qb;/* Insert the remaining pb */free (head2); return head1;} else {return head2 ;}} /* Add and create multiple unary polynomials */NODE * CreatLinkLi St (int n) {int I, a; char x; NODE * head [100], * headsum; // an array with a length of 100, headsum = NULL; for (I = 1; I <= n; I ++) {cout <"\ n enter the" <I <"Number of polynomials:"; // scanf_s ("% d", & ); cin> a; a = inputnum (a); cout <"(enter the coefficient and index in sequence based on the index size)" <endl; head [I] = Creat (); // create a cout linked list with head [I] As the header node <"the following formula is the" <I <"polynomial:" <endl; print (head [I]); cout <"check whether the input is correct. If the input is incorrect, enter n to enter the preceding formula. If the input is correct, enter y to confirm (in lower case ):"; // confirm the input of cin> x; if (x = 'N') // correct {if (I = 1) if the first item is incorrect, clear the first unary polynomial {head [I] = NULL;} else {// if not the first one, clear the input polynomial, and I minus 1 head [I] = NULL; // I -- ;}} headsum = AddPolyn (headsum, head [I]); // Add the linked lists if (x = 'n') // if the first item is incorrect, multiply the I -- {I --;} return headsum;}/* polynomial, add the exponent of each item of A polynomial to the exponent of each item of another polynomial and multiply the coefficients */NODE * MulPolyn (NODE * A, NODE * B) {NODE * pa, * pb, * pc, * u, * head; int k = 0; int maxExp = 0; // maxExp is the maximum value of two linked list indexes and float coef = 0.0; head = (NODE *) malloc (sizeof (NODE); if (! Head) return NULL; head-> coef = 0.0; head-> expn = 0; head-> next = NULL; SortDown (A); SortDown (B ); if (A-> next! = NULL & B-> next! = NULL) maxExp = (A-> next-> expn) + (B-> next-> expn); elsereturn head; pc = head; SortUp (B ); for (k = maxExp; k> = 0; k --)/* determines that the exponent range of the polynomial product is 0 to maxExp */{pa = A-> next; while (pa! = NULL & pa-> expn> k)/* locate the Pa position */pa = pa-> next; pb = B-> next; while (pb! = NULL & pa! = NULL & (pa-> expn + pb-> expn) <k)/* If and is less than K, move pb to the next node */pb = pb-> next; coef = 0.0; while (pa! = NULL & pb! = NULL) {if (pa-> expn) + (pb-> expn) = k) {coef + = (pa-> coef) * (pb-> coef); pa = pa-> next; pb = pb-> next;} else if (pa-> expn) + (pb-> expn)> k)/* If the sum is greater than K, pa is moved to the next node */pa = pa-> next; elsepb = pb-> next;/* if the sum of the sum is less than K, move pb to the next node */} if (coef! = 0.0) {/* If the coefficient is not 0, a new node is generated and the coefficient and index are assigned to the new node respectively, insert it to the linked list */u = (NODE *) malloc (sizeof (NODE); u-> coef = coef; u-> expn = k; u-> next = pc-> next; pc-> next = u; pc = u ;}} SortDown (B); return head ;}
Main function:
# Include "polyn. h "void main (int n) {char y; NODE * headsum; NODE * headmul; int select = 1; NODE * A; NODE * B; int NumA = 0; int NumB = 0; menu (); cin> select; switch (select) {case 1: /* Add */cout <"***************************** welcome to use one dollar polynomial addition program ******************* "<endl; cout <"Enter the number of polynomials (an integer greater than 0):"; cin> n; n = inputnum (n); headsum = CreatLinkList (n ); cout <endl; cout <"display of polynomials after addition:" <endl; print (headsum); cout <"**************************** Thank you for using ******* *************************** "<endl; cout <"continue? Enter (y/n): "; cin> y; if (y = 'y') {main (1) ;} break; case 2: /* multiply */cout <"***************************** welcome to use two; cout <"Enter the number of 1st polynomials:"; cin> NumA; cout <"(enter the coefficient and index in sequence based on the index size)" <endl; A = Creat (NumA); cout <"below is the 1st polynomials you entered:" <endl; print (); cout <"Enter the number of 2nd polynomials:"; cin> NumB; cout <"(enter the coefficient and index in sequence based on the index size)" <endl; B = Creat (NumB); cout <"below is the 1st polynomials you entered:" <endl; print (B); headsum = MulPolyn (, b); cout <endl; cout <"display of polynomials after multiplication:" <endl; print (headsum ); cout <"***************************** Thank you for using ****** **************************** "<endl; cout <"Continue, enter (y/n):"; cin> y; if (y = 'y') {main (1) ;}break; break; default: break ;}}
The running result is as follows: