One-way linked list for polynomial addition and multiplication-write data structure by yourself
Use a single linked list to implement the polynomial data structure and code as follows (for time reasons, the polynomial multiplication function is useless and can be improved by yourself ):
Storing the structure header file polylist. h
# Ifndef _ H_POLYLIST _ # define _ H_POLYLIST_typedef struct _ Poly_Node {int ratio; // coefficient int power; // power struct _ Poly_Node * next;} Node, * pNode; typedef struct _ Poly_List {struct _ Poly_Node * next;} PolyList, * pPolyList; pPolyList init_list_header (void); pNode poly_add_node (pPolyList plist, int ratio, int power ); int partition (pPolyList plist, int power); int print_poly (pPolyList plist); int partition (pPolyList plist); pPolyList poly_addcat (pPolyList pfir, pPolyList psec); pPolyList poly_add (pPolyList pfir, pPolyList psec); # endif
The data structure function implementation file polylist. c is as follows:
/************************************ Time: 2014.12.11 Author: XIAO_PING_PING content: unidirectional linked list represents the addition and multiplication functions of polynomials: learn to write data structures ************************************ * **/# include
# Include
# Include "polylist. h "/* Create a polynomial linked list header */pPolyList init_list_header (void) {pPolyList phead; phead = (PolyList *) malloc (sizeof (PolyList )); phead-> next = NULL; return phead;}/* Add an entry to the polynomial */pNode poly_add_node (pPolyList plist, int ratio, int power) {pNode ptail, pre, p; ptail = (Node *) malloc (sizeof (Node); ptail-> ratio = ratio; ptail-> power = power; ptail-> next = NULL; if (NULL = plist-> next) {plist-> next = p Tail; printf ("insert point of the polynomial linked list: coefficient = % d, power = % d \ n", ptail-> ratio, ptail-> power); return ptail ;} pre = plist-> next; while (pre) {p = pre; pre = pre-> next;} p-> next = ptail; printf ("insert point of Polynomial linked list: coefficient = % d, power = % d \ n ", ptail-> ratio, ptail-> power); return ptail ;} /* Delete the power entry in the polynomial */int poly_delete_node (pPolyList plist, int power) {pNode pre, p; if (NULL = plist-> next) {printf ("the polynomial linked list is empty"); return-1 ;}pre = plist-> next; p = pre; wh Ile (NULL! = P-> next) {pre = p; if (power = pre-> next-> power) {pre = pre-> next; p-> next = pre-> next; free (pre); printf ("Delete the item \ n", power) whose power value is % d in the polynomial linked list );} p = p-> next;} if (power = plist-> next-> power) {p = plist-> next; plist-> next = plist-> next; free (p); printf ("Delete the item \ n", power) with the power of % d in the polynomial linked list );} return 0;}/* print polynomial */int print_poly (pPolyList plist) {pNode p; p = plist-> next; if (NULL = p) {printf ("this polynomial is null. \ N "); return-1;} printf (" traversal output polynomial: \ n "); printf (" Y = "); while (NULL! = P) {printf ("% d X % d +", p-> ratio, p-> power); p = p-> next;} return 0 ;} /* polynomial sorting */int poly_bubble_sort (pPolyList plist) {int tpower, tratio; pNode p, pre, ptemp; if (NULL = plist-> next) {printf ("the polynomial linked list is empty \ n"); return-1 ;}if (NULL = plist-> next) {printf ("only one polynomial linked list \ n"); return-2;} p = plist-> next; pre = p; while (p) {pre = p; while (pre-> next) {// ptemp = pre; if (pre-> next-> power = p-> power) {ptemp = pre-> next; p-> ratio + = pre-> next-> ratio; pre-> next = pre-> next; free (ptemp ); if (NULL = pre-> next) {break;} else if (pre-> next-> power <p-> power) {tpower = p-> power; tratio = p-> ratio; p-> power = pre-> next-> power; p-> ratio = pre-> next-> ratio; pre-> next-> power = tpower; pre-> next-> ratio = tratio; // ptemp = p; // p = pre-> next; // pre-> next = ptemp; //} else {} pre = pre-> next;} p = p-> next ;}/ * merge two polynomials, add all nodes of the second polynomial to the end of the first polynomial */pPolyList poly_addcat (pPolyList pfir, pPolyList psec) {PolyList * p = pfir; // ptotal = (PolyList *) malloc (sizeof (PolyList); Node * pre = pfir-> next; // p = pfir-> next; if (NULL = pre) {pfir-> next = psec-> next;} else {while (pre-> next) {// pre = p; pre = pre-> next ;} pre-> next = psec-> next;} free (psec); return pfir;}/* polynomial addition */pPolyList poly_add (pPolyList pfir, pPolyList psec) {PolyList * p = pfir; // ptotal = (PolyList *) malloc (sizeof (PolyList); p = poly_addcat (pfir, psec); poly_bubble_sort (p ); // return poly_bubble_sort (poly_addcat (pfir, psec); return p ;}
The test. c file is as follows:
# Include
# Include
# Include "polylist. h "int main () {pPolyList polyone, polytwo, polyadd; printf (" polynomial 1: \ n "); polyone = init_list_header (); poly_add_node (polyone, 2, 11 ); partition (polyone,); poly_add_node (polyone,); partition (polyone,); poly_add_node (polyone, 3,343); poly_add_node (polyone, 34,414); poly_add_node (polyone,); poly_add_node (polyone,); print_poly (polyone); printf ("\ n "); printf ("\ n"); printf ("polynomial 2: \ n"); polytwo = init_list_header (); poly_add_node (polytwo,); poly_add_node (polytwo ); round (polytwo, 3,343); poly_add_node (polytwo,); Round (polytwo,); poly_add_node (polytwo,); Round (polytwo,); Round (polytwo, 12,464); poly_add_node (polytwo,); poly_add_node (polytwo,); print_poly (polytwo); printf ("\ n"); printf ("\ n "); // polyadd = init_list_header (); // polyadd = poly_addcat (polyone, polytwo); // polyadd = init_list_header (); printf ("merge polynomials One and Two: \ n "); polyadd = poly_add (polyone, polytwo); print_poly (polyadd); // poly_bubble_sort (polyone); // poly_delete_node (polyone, 2); // poly_delete_node (polyone, 4 ); // poly_delete_node (polyone, 2); // poly_delete_node (polyone, 3); // getch ();}
The running results of the above files are as follows: