c Algorithm and data structure-application of linear table, polynomial summation---shinepans

Source: Internet
Author: User

/*---On-machine job, two-item addition---*//*---by Pinchamp---*//*---date: 2014-5-8. ---*//*---title:---*///if there are two sparse polynomial a and B, the design algorithm completes the following task//1. Enter and set up the polynomial A and the b;//2. Two polynomial and polynomial c;//3. Two polynomial d;//output 4 polynomial a,b,c,d--   ; #include <stdio.h> #include <stdlib.h> #include <string.h>typedef struct node{float A;     coefficient int m; exponential struct Node *next;}   Lnode, *linklist; List type, if using a nested structure how to use a pointer to the way to visit void Initialize (Lnode *plist); Linklist Polyadd (Lnode *plist1, Lnode *plist2), int Cmp (int a, int b), void Append (Lnode *pa, Lnode *pb), int listisempty (con St linklist *plist); void Emptythelist (Lnode *plist); void Printlist (Lnode *plist); void Polysort (Lnode *plist); int GetLength (Lnode *plist); void Polymerge (Lnode *plist); float hornorevaluate (Lnode *plist, float x); Linklist polymultiply (Lnode *plist1, Lnode *plist2); int getmaxexpn (Lnode *plist);/*** main function */int main (void) {Lnode *P1 = ( lnode*) malloc (sizeof (Lnode)); Lnode *p2 = (lnode*) malloc (sizeof (Lnode)); Lnode *RESULT1 = (lnode*) malloc (sizeof (Lnode)); Lnode *result2 = (lnode*) malloc (sizeof (Lnode)); Initialize (p1); Initialize (p2);     Polysort (p1); Polynomial sort Polysort (p2);p rintf ("\NP1 ="); Printlist (p1);p rintf ("\np1 (value) =%f", Hornorevaluate (P1, 2));p rintf ("\NP2 ="); Printlist (p2);p rintf ("\np2 (value) =%f", Hornorevaluate (P2, 2)); result1 = Polyadd (P1, p2); result2 = Polymultiply (P1, p2)      ;//printf ("\n%d", GetLength (p1));//polymerge (p1);   Merger of similar terms//unitepoly (P1); Merge similar terms//emptythelist (p1);p rintf ("\np1 + p2 ="); Printlist (RESULT1);p rintf ("\np1 * p2 ="); Printlist (RESULT2); GetChar (); GetChar (); return 0;} /***operation: Initializes a polynomial *precondition:*postcondition:*/void Initialize (lnode *plist) {int I, n;float c;int e; Lnode *prev = NULL, *current = Null;plist->next = Null;prev = plist;printf ("\nplease input the length of the polynomial : "); scanf_s ("%d ", &n);p list->a = 0;plist->m = N; List length printf ("Please input the coefficient and the exponent of each term: \ n"); for (i = 1; I <= n; i++) {current = (Lnod e*) malloc (sizeof (Lnode)); scanf_s ("%2f%d", &c, &e); current->a = C;current->m = E;prev->next = Current;prev = Current;/*plist->next = Current;plist = Current;*/}current->nex t = NULL; Polysort (plist);} /***operation: Add a total of two polynomial *precondition:*postcondition:*notes: can be used to combine the two polynomial (splicing) and then merge similar terms to achieve the addition */linklist Polyadd (Lnode * Plist1, Lnode *plist2) {Lnode *pa, *PB; Lnode *result = (lnode*) malloc (sizeof (lnode)); int A, b;result->next = NULL; Lnode *r = Result;pa = PLIST1-&GT;NEXT;PB = Plist2->next;while (PA && pb) {Lnode *current = (lnode*) malloc (Sizeo F (Lnode)); a = Pa->a;b = Pb->m;switch (Cmp (A, B)) {case-1:current->a= Pa->m;current->a = Pa->m;pa = PA-&G T;next;break;case 0:current->a = pa->a + pb->a;current->m = Pa->m;pa = PA-&GT;NEXT;PB = pb->next;break; Case 1:current->a = pb->a;current->m = PB-&GT;M;PB = Pb->next;break;}  R->next = Current;r = r->next; The result polynomial pointer moves back current->next = NULL;} if (!pa | |!pb) {if (!PA) Append (R, Pb), if (!PB) Append (R, PA);} Free (current); return result;} /*** TwoInteger size */int Cmp (int a, int b) {if (a>b) return 1;if (a<b) return-1;else return 0;} /*** link PB remaining node to PA */void Append (lnode *pa, Lnode *pb) {Lnode *r = PA;  Lnode *p = Pb;while (p) {Lnode *current = (lnode*) malloc (sizeof (Lnode)); current->a= p->a;current->m = P->m;p =     P->next;r->next = current; Segivecurrent->next = NULL;}} /***operation: Infers whether the polynomial is empty */int listisempty (const linklist *plist) {if (*plist = = NULL) return 1;elsereturn 0;} /***operation: Emptying a polynomial list *precondition:plist points to a polynomial listing *postcondition: The list is emptied and released */void emptythelist (Lnode *plist) { Lnode *psave;while (plist! = NULL) {Psave = Plist->next;free (plist);p list = Psave;}} /***operation: Output A polynomial list */void printlist (Lnode *plist) {Lnode *p = Plist;p = p->next;//Skip pointer while (P! = NULL) {if (p-&gt ; Next! = NULL) printf ("%0.1f*x^%d +", P->a, P->m); elseprintf ("%0.1f*x^%d;", P->a, p->m);p = P->next;} printf ("\ n");} /***operation: Sort unordered polynomial list of input *precondition: unordered polynomial linked list *postcondition: Incremental order polynomial list */void polysort (Lnode *pliST) {Lnode *pa = plist->next, *PB = pa->next; Lnode *temp = (lnode*) malloc (sizeof (Lnode)), int length = GetLength (plist), int i;for (i = 0; i<length; i++) {while (Pb) {i F (Pa->m > Pb->m) {temp->a = pa->a; temp->m = Pa->m;pa->a = pb->a; Pa->m = Pb->m;pb->A = temp->a; Pb->m = temp->m;} PA = PA-&GT;NEXT;PB = Pa->next;}         PA = PLIST-&GT;NEXT;PB = pa->next; These two sentences are used to point the PA,PB again at the head node}}/***operation: Merge the exponent of the input polynomial (merge similar terms) *precondition: Ordered polynomial list *postcondition: Ordered polynomial list without similar terms */ void Polymerge (Lnode *plist) {Lnode *prev, *current;int l = getlength (plist); int i;prev = Plist->next;current = Prev-&gt    ; next;for (i = 0; i<l; i++) {while (current) {if (prev->m = = current->m) {prev->a + = current->a; Why "Prev->coef + = Current->coef" is Wrong?prev->next = Current->next;free (current); current = Prev->ne   Xt;continue; //! Without this sentence, the function would be wrong and a problem}prev = Prev->next;current = Prev->next;} if (current) {prev = Plist->next;current = Prev->next;}}} /*//merge similar terms void Unitepoly (Lnode *h)//merge similar terms {lnode *p1,*p2,*q1,*q2,*temp;q1=h;p1=q1->next;while (p1!=null) {p2=p1- >next;q2=p1;while (p2!=null) {if (P1-&GT;EXPN==P2-&GT;EXPN) {p1->coef=p1->coef+p2->coef;if (P1->coef ==0) {temp=p2;q2->next=p2->next;free (temp); Temp=p1;q1->next=p1->next;p1=q1;free (temp); break;} Else{temp=p2;q2->next=p2->next;p2=p2->next;free (temp);}} Else{q2=p2;p2=p2->next;}} Q1=p1;p1=p1->next;}}  *//*** list Length */int getlength (Lnode *plist) {Lnode *p = plist;int Lenght = 0;p = p->next; Skip node while (p) {lenght++;p = P->next;}   return Lenght;//return lenght-1; If you do not jump over the node, the return value is reduced by one}/***operation: two polynomial product *precondition:*postcondition:*/linklist polymultiply (Lnode *plist1, LNode * PLIST2) {Lnode *pa, *PB; Lnode *result = (lnode*) malloc (sizeof (Lnode));  Lnode *r = result; No less! Otherwise, result does not refer to the head pointer pa = plist1->next; PB = Plist2->next;while (PA) {while (Pb) {Lnode *current = (lnode*)malloc (sizeof (Lnode)); current->a= Pa->m * pb->a;  Multiplying coefficients current->m = pa->m + pb->m; exponential addition r->next = Current;current->next = Null;r = R-&GT;NEXT;PB = Pb->next;}  PA = PA-&GT;NEXT;PB = plist2->next; PB again points to the head node}polymerge (result); return result;} /***operation:computing the value of the polynomia*precondition:ordered polynomial linklist*postcondition:the value of The Polynomial*/float hornorevaluate (Lnode *plist, float x) {//int max = GETMAXEXPN (plist) + 10;int n = 0;int i;float Resul   t = 0;//float Poly[max]; VC6 sidn ' t support Vlafloat poly[20]; Lnode *p = plist->next;memset (Poly, 0, sizeof (Poly)), while (p) {if (p->m = = N) {poly[n++] = P->a;p = P->next;}  elsepoly[n++] = 0;} Transform linklist to array to store the Polynomial/*for (i=0;i<n;i++)//debug Output intermediate variable for easy debugging {printf ("[%d]:%0.2f", i,poly[ I]);} printf ("\ n"); */result = poly[n-1];for (i = n-1; i>0; i--)//cycle times and subscript relationships never wrong! {result = Result*x + poly[i-1];//printf ("[%d%0.2f]", i,resULT); Debug output Intermediate variable for easy commissioning}return result;} /***operation:*precondition:*postcondition:*/int getmaxexpn (Lnode *plist) {lnode *p = plist->next;int max = p->m; while (p) {if (P->m > Max) max = P->m;p = P->next;} return Max;}

c Algorithm and data structure-application of linear table, polynomial summation---shinepans

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.