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 &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