Realization of the and product of two exponential descending polynomial

Source: Internet
Author: User

There are two exponential regressive unary polynomial, write a procedure to seek the and of the two polynomial, and then seek their product.

"Hint" using a single linked list with a header node as the storage representation of a polynomial; To create two single-linked lists; The polynomial addition is to insert a node in a single linked list into another single linked list, pay attention to the correct modification of pointers in insert and delete operations.

#include <iostream>#include<cmath>using namespacestd;/** Data structure Exercise 1 polynomial addition and multiplication @ Liu Hui **/structnode{intdata; intindex; Node*next;}; Node*insertlist (node* head,node* p);//Insert Linked listNode *createlist ();//Create a linked listvoidPrintlist (Node *head);//Print linked listNode *addlist (node *p1,node *p2);//Implementing addition operationsNode*createlist () {intIndex,data; Node*p,*head,*Q; Head=NewNode; P=Head; cout<<"Please enter the power exponent of the polynomial A to be entered:"; CIN>>index; cout<<"Please enter the parameters for this index:"; CIN>>data;  while(index!=0) {Q=NewNode; Q->index =index; Q->data =data; P->next =Q; P=Q; cout<<"Please enter the power exponent of the polynomial A to be entered:"; CIN>>index; cout<<"Please enter the parameters for this index:"; CIN>>data; } P->next =NULL; returnhead;}//polynomial add-onsNode *addlist (Node *p1,node *p2) {    intadd; Node*temp,*head,*P3; P1= p1->Next; P2= p2->Next; Head= TEMP =NewNode; Head->next =NULL;  while(p1&&p2) {                if(p1->index==p2->index) {Add= P2->data + p1->data; if(add!=0) {P3=NewNode; P3->index = p2->index; P3->data =add; P3->next =NULL; } p1= p1->Next; P2= p2->Next; }        Else if(p1->index<p2->index) {P3=NewNode; P3->data = p2->data; P3->index = p2->index; P3->next =NULL; P2= p2->Next; }        Else{P3=NewNode; P3->data = p1->data; P3->index = p1->index; P3->next =NULL; P1= p1->Next; }        if(Head->next = =NULL) {Head->next =P3; Temp=P3; }        Else{Temp->next =P3; Temp=P3; }} Temp->next = p1?P1:P2; returnHead; }//polynomial multiplicationnode* mullist (Node *p1,node *p2) {Node*head,*temp,*s,*r,*P; Head=NewNode; Head->next =NULL; Temp=NewNode; Temp->next =NULL; P1= p1->Next; P2= p2->Next;  for(s=p1;s;s=s->next) {         for(r=p2;r;r=r->next) {Q=NewNode; Temp->next =Q; Q->data = S->data * r->data; Q->index = S->index + r->index; Q->next =NULL; Head=addlist (Temp,head); }    }    returnhead;}//Print Polynomial voidPrintlist (Node *head) {Node*p =NULL; P= head->Next; if(p==NULL) {cout<<"file is empty"; }     Else    {         Do        {            if(p->data>=0) cout<<p->data<<"x^"<<p->index; Elsecout<<p->data<<"x^"<<p->index; if(p->next!=NULL) cout<<"+"; P=p->Next; } while(P! =NULL); cout<<Endl; } }  //Main functionintMain () {inti; Node*p1=null,*p2=null,*p3=null,*p4=NULL; cout<<"create a linked list of the first polynomial:"<<"\ n"; P1=createlist (); cout<<"\ n"; cout<<"create a linked list of the second polynomial:"<<"\ n"; P2=createlist (); cout<<"the first polynomial is:";    Printlist (p1); cout<<"\ n"<<"the second polynomial is:";    Printlist (p2); P3= Addlist (P1,P2);//implement the polynomial add -oncout<<"\ n"<<"when the polynomial is added:";    Printlist (p3); cout<<Endl; P4= Mullist (P1,P2);//Implementing polynomial multiplicationcout<<"when the polynomial is multiplied:";    Printlist (p4); CIN>>i; return 0;}

To implement the and product of two exponential descending polynomial

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.