/*************************************** * ******************************* // @ Author lynnbest target: multiplication exp of polynomials: A (X) = 2X ^ 3 + 4 B (x) = 4X ^ 4 + 2X ^ 3 C (X) = A (x) * B (x) = 8X ^ 7 + 4X ^ 6 + 16X ^ 4 + 8X ^ 3 train of thought: 1. create two linked lists to store two polynomials. Use chained storage systems + index fields + pointer fields. complete multiplication of two polynomials 3. print the New Result *//********************************** **************************************/# include <stdio. h> # include <stdlib. h> typedef struct node {float coef; // coefficient int exp ;/ /Index struct node * next; // pointer field} listnode; listnode * CreateList (int n); int printflist (listnode * head); int InverseList (listnode * head ); listnode * MultiplisePoly (listnode * head_a, listnode * head_ B); int main () {printf ("Multiplication of polynomials in the linked list \ n "); printf ("---- by lynnbest ---- \ n"); int n; printf ("Enter the number of items in A (X) (power-down arrangement) \ n "); scanf ("% d", & n); listnode * head_a = CreateList (n); printf ("A (X) ="); printflist (head_a); printf (" Enter the number of items in B (X) (power-down arrangement) \ n "); scanf (" % d ", & n); listnode * head_ B = CreateList (n ); // InverseList (head_ B); printf ("B (X) ="); printflist (head_ B); listnode * head_c = MultiplisePoly (head_a, head_ B ); printf ("C (X) = A (X) * B (X) ="); printflist (head_c); return 0;} listnode * CreateList (int n) {listnode * head = (listnode *) malloc (sizeof (listnode), * p, * pre = head; // head points to the header node, p points to the newly opened node float coef; // coefficient int exp; // index if (NULL = head) {Printf ("opening the header node failed \ n"); exit (-1) ;}head-> next = NULL; for (int I = 0; I <n; I ++) {if (NULL = (p = (listnode *) malloc (sizeof (listnode) {printf ("new node malloc failed \ n "); exit (-1);} printf ("Enter the % d coefficient and exponent \ n", I + 1); scanf ("% f, % d ", & coef, & exp); p-> coef = coef; p-> exp = exp; // update node data p-> next = NULL; // Insert the node pre-> next = p; pre = p;} return head; // The returned heap memory is not a local variable} int printflist (listnode * head) {listnode * p = head-> next; while (p -> Next! = NULL) {printf ("% 1.1f * X ^ % d +", p-> coef, p-> exp ); // only one digit after the decimal point is output in % 1.1f format, p = p-> next;} printf ("% 1.1f * X ^ % d \ n", p-> coef, p-> exp); return 0 ;} /*************************************** ********************************* // multiply the chain: idea: 1. because both linked lists are exponentially decreasing, A (X) is decreasing, B (x) is descending, increasing, and why do this? 2. first obtain the two largest indexes and exp_max. In this case, the remaining indexes are in the range of 0 ~ Between 7. The key is that traversal and multiplication are not difficult, but how can we find all the indexes? In addition, we need to create new nodes to store the exponential solution that is not available: Use a new chain to store the results, and search down from exp_max. Every possible index must be traversed. Here, the ascending and descending order of indexes is very subtle. For (k = exp_max; k> = 0; k --) {multiply; determine whether there is a similar coefficient, and add it together;} How to judge? Is to perform step-by-step search. If the current k value is used, it indicates that the index has been found. At this time, both a and B are followed by one digit, because only such a combination may have the same coefficient if the current index is <k, it indicates that we want to increase the coefficient and. If only a increases, if it is the current index> k, it indicates that we want to decrease the coefficient and only B increases. This shows that, b: The benefits of two linked lists in one Ascending Order and one descending order. This is a good idea. 3. to sum up, 3.1 k = exp_max; 3.2 reverse B 3.3 traversal lookup is another problem. Once we find that we are doing a loop, then you can continue searching for other possibilities until all nodes are NULL *//************************** **************************************** * *****/listnode * MultiplisePoly (listnode * head_a, listnode * head_ B) // chain and multiply {listnode * head_c, * pa = head_a, * pb = head_ B, * pc, * newnode; int exp_max; // returns the maximum value of the sum of indexes. if (pa-> next! = NULL & pb-> next! = NULL) exp_max = pa-> next-> exp + pb-> next-> exp; // obtain the maximum index and else return NULL; // initialize the C header node head_c = (listnode *) malloc (sizeof (listnode); if (NULL = head_c) {printf ("failed to open linked list C \ n"); exit (-1) ;}head_c-> coef = 0.0; head_c-> exp = 0; head_c-> next = NULL; pc = head_c; InverseList (head_ B); // reverse the float ceof = 0.0; for (int k = exp_max; k> = 0; k --) {pa = head_a-> next; // restores the position of pa to while (pa! = NULL & pa-> exp> k) // first, find the location of the pa not greater than k. Then, find pa = pa-> next; pb = head_ B-> next; // restore the point of Pb To while (pa! = NULL & pb! = NULL & pa-> exp + pb-> exp <k) // locate the location of pb. The index of pa + pb is not greater than k pb = pb-> next; // The exp of pa + pb after the above two rounds <= k while (pa! = NULL & pb! = NULL) // after this cycle enters, find all the sum of the same index {if (k = pa-> exp + pb-> exp) // The objective is to find and equal to K {ceof + = pa-> coef * pb-> coef; pa = pa-> next; pb = pb-> next ;} else {if (pa-> exp + pb-> exp <k) // less than k increase pb = pb-> next; else pa = pa-> next; // if (ceof! = 0.0) // Insert the node to the c linked list if it has a coefficient. {if (NULL = (newnode = (listnode *) malloc (sizeof (listnode )))) {printf ("failed to open C node in Linked List"); exit (-1) ;}newnode-> coef = ceof; newnode-> exp = k; newnode-> next = NULL; // insert node data pc-> next = newnode; pc = newnode; // insert node ceof = 0.0 ;}} InverseList (head_ B); return head_c ;} int InverseList (listnode * head) // reverse linked list {listnode * p = head-> next, * q; // p points to the node to be reversed, q points to the head of the next node to be reversed-> next = NULL; while (p) // the current node is not empty {q = p-> next; // save the next node p-> next = head-> next; // first update the next head-> next = p; // update head-> next p = q; // next round} return 0 ;}