Data structure and algorithm analysis 3.7-polynomial multiplication linked list implementation

Source: Internet
Author: User

Ideas :

<1> for each of the expressions in P (x), the result of multiplying each of the expressions in Q (x) is saved in another linked list;

<2> Sort the list of saved results, and go to weight, that is, to remove the same factor, but the coefficients are added .  

Code :

#include <list>using namespacestd;structnode{intcoefficient =0; intexponent =0;};BOOLCOMPFN (ConstNode &AMP;LHS,ConstNode &rhs) {returnLhs.exponent >rhs.exponent;} List<Node> multiplypolynomial (list<node> &polyfirst, List<node> &Polysecond) {List<Node>Polyprod;  for(Auto i = Polyfirst.cbegin (); I! = Polyfirst.cend (); + +i) { for(Auto J = Polysecond.cbegin (); J! = Polysecond.cend (); + +j) {Node prod; Prod.coefficient= I->coefficient * j->coefficient; Prod.exponent= I->exponent + j->exponent;        Polyprod.push_back (PROD);    }} polyprod.sort (COMPFN); if(Polyprod.size () >1) {Auto I=Polyprod.begin ();  while(I! =Polyprod.end ()) {Auto J=i; if(+ + J! = Polyprod.end () && i->exponent = = j->exponent) {i->coefficient + = j->coefficient;            Polyprod.erase (j); }            Else++i; }    }    returnPolyprod;}

Data structure and algorithm analysis 3.7-polynomial multiplication linked list implementation

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.