Reprint please indicate: http://blog.csdn.net/tengweitw/article/details/40476935
In general, a unary n-th polynomial can be written as:
of which, pIis an index of eIthe non-0 coefficient of the item, and satisfies
Therefore, we can use the linear table (defined: a linear table is a finite sequence of n data elements, such as arrays, vectors, linked lists, etc.) to represent:
Each of the index I can be expressed by the number of its coefficient pi.
in general applications, the number of polynomial is relatively large, so that the length of the linear table is difficult to determine, so we can consider the linked list, vectors can also (c + +). Example: If we use an array to represent the following polynomial:
Visible, we need a size of 1549 array to represent, and the actual useful information only 4 elements in the array, the other place is 0, so the space wasted. And if we don't know the exponent of the highest order of the polynomial in advance, then we need to define a large enough array to store it, which obviously wastes a lot of memory space. We can use the linked list to solve the above problem:
In the computer, we use a node to store a polynomial, in order to save space and consistent with the writing habits, only need to retain the non-0 coefficient of the item . Each node is divided into coefficients, exponents, and pointers three fields, as shown in, where the pointer next indicates the position of the next item.
For example, the following polynomial is a-B, respectively:
The circular list can be represented as follows:
Two polynomial add the arithmetic rule is very simple, for all exponents of the same item, the corresponding coefficients are added, if and not zero, then the composition and one of the polynomial ; Copies all items with different indices to and from the polynomial. when implemented, we use the above polynomial, a and a test sample. You can use a different list to store the polynomial method, or the method of merging one polynomial into another polynomial . For example, we will store the a+b and polynomial in a in a later way. The concrete program is implemented as follows (I use a circular link list):
#include <stdio.h> #include <stdlib.h> #include <conio.h>typedef struct pnode//use a linked list to store the polynomial information {float coef;//polynomial coefficients int exp;//polynomial exponential struct pnode *next;} Polynode;polynode *create () {float coef;int exp;polynode *head,*s,*r;head= (polynode*) malloc (sizeof (Polynode)); head- >coef=0;head->exp=-1;r=head;printf ("Please enter the coefficients and exponents of each item: \ n"), while (1) {scanf ("%f%d", &coef,&exp); if (coef!= 0)//Input 0-end input {s= (polynode*) malloc (sizeof (Polynode)); s->coef=coef;//s is used to save the current node s->exp=exp;r->next=s;r=s;} Elsebreak;} r->next=head;//Construction Cycle list return head;} Polynode*polyadd (polynode* pa,polynode* pb)//polynomial addition {Polynode *p,*q,*r,*s;float x;p=pa->next;//points to the first item of the polynomial respectively q=pb- >NEXT;S=PA;//S is used to save the current node while ((P!=PA) && (Q!=PB))//Does not end, back to the index of the chain header {if (p->exp<q->exp)//p is less than the exponent of Q, Put p into the linked list {s=p;p=p->next;} else if (p->exp>q->exp)//p exponent is greater than Q, put Q into the list {r=q->next;q->next=p;s->next=q;s=q;q=r;} else//Merge {x=p->coef+q->coef;if (x!=0) {p->coef=x;s=p;} when both indices are combined else//If the merge result is 0, remove the node {S->next=p-> Next;free (P);} P=s->next;r=q;q=q->next;free (R);}} if (Q!=PB)//If the number of items in polynomial B is less than the case of polynomial a (R=q;while (R->NEXT!=PB) R=r->next;s->next=q;r->next=pa;} Return PA;} void output (Polynode *head)//output polynomial information {Polynode *p;printf ("coefficients and exponents respectively:");p =head->next;while (p!=head) {printf ("%.1f, %d ", p->coef,p->exp);p =p->next;} printf ("\ n");} void Main () {polynode* ha,*hb;printf ("\ n establishes polynomial a:"), Ha=create (), Output (ha);p rintf ("\ nthe polynomial B:"); Hb=create (); Output ( HB); Ha=polyadd (HA,HB);p rintf ("\ n polynomial a+b:"); Output (ha);}
"Introduction to Algorithms" Polynomial summation