"Data structure experiment" polynomial multiplication

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3 4typedefstructpolynomial5 {6    intCoef;7    intexpn;8    structPolynomial *Next;9 }node;Ten  OneNode *creat () A { -Node *p,*q,*h; -     intExpn,coef; theH= (node*)malloc(sizeof(node)); -p=h; -printf"Enter your coef and expn\n"); -scanf"%d%d",&coef,&expn); +      while(coef!=0) -     { +Q= (node*)malloc(sizeof(node)); Aq->coef=coef;q->expn=expn; atp->next=Q; -p=Q; -scanf"%d%d",&coef,&expn); -     } -p->next=NULL; -     returnh; in } -  to voidPrint (node *h) + { -Node *p; thep=h; *      while(p->next!=NULL) $     {Panax NotoginsengP=p->Next; -         if(p->next!=NULL) theprintf"%dx^%d+",p->coef,p->expn); +         Else Aprintf"%dx^%d\n",p->coef,p->expn); the     } +  - } $  $Node *mul (node*ha,node*HB) - { -node*p1,*p2,*p,*h,*Q; theP1=ha;p2=HB; -H= (node*)malloc(sizeof(node));Wuyip=h; the      while(p1->next!=NULL) -     { WuP1=p1->Next; -          while(p2->next!=NULL) About         { $P2=p2->Next; -Q= (node*)malloc(sizeof(node)); -Q->coef=p1->coef*p2->Coef; -Q->expn=p1->expn+p2->expn; Ap->next=Q; +p=Q; the         } -P2=HB; $     } thep->next=NULL; the         returnh; the } the  -Node *simp (node*HC) in { theNode *p,*q,*Set; the     Set=HC; AboutP=hc->Next; the      while(Set->next!=NULL) the     { the         Set=Set-Next; +          while(p->next!=NULL) -         { theq=p;BayiP=p->Next; the             if(Set->expn==p->expn) the             { -                 Set->coef+=p->Coef; -Q->next=p->Next; theq=p; theP=p->Next; the                  Free(q); the             } -         } thep=Set-Next; the     } the     returnHC;94 } the  the intMain () the {98printf"Hello world!\n"); AboutNode *h1,*h2,*h3,*h4; -printf"The first one is coef/n");101h1=creat ();102 print (H1);103printf"The second one is expen/n");104H2=creat (); the print (H2);106printf"The third one is mul\n");107h3=Mul (H1,H2);108h4=Simp (H3);109 print (H4); the     return 0;111}

"Data structure experiment" polynomial multiplication

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.