Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: STL linked list - += duplicating node
    text
    copied!<p>I am working on a Polynomial class which uses the STL linked list. One of the functions requires me to add two Polynomial's together. For some reason, the += operator seems to be duplicating the node, as opposed to merely modifying the contents.</p> <p>Here is the class declaration:</p> <pre><code>class Polynomial { public: Polynomial(pair&lt;double,int&gt;); //Specified constructor void add(const Polynomial&amp;); void print(); private: Polynomial(); //Default constructor list&lt;pair&lt;double,int&gt; &gt; terms; }; </code></pre> <p>This is the add member function:</p> <pre><code>void Polynomial::add(const Polynomial&amp; rhs) { list&lt;pair&lt;double,int&gt; &gt;::const_iterator r; list&lt;pair&lt;double,int&gt; &gt;::iterator l; for(r=rhs.terms.begin(); r!=rhs.terms.end(); r++) { bool match=0; //Check to see if we have an existing nth order node for(l=terms.begin(); l!=terms.end(); l++) { //If we do, just add the coefficients together if(l-&gt;second == r-&gt;second) { l-&gt;first += r-&gt;first; match = 1; } } //If there was no matching existing node, we need to find out //where to insert it into the list. if(!match) { l=terms.begin(); bool inserted=0; //Sentinel for the loop while(l!=terms.end() &amp;&amp; !inserted) { //If there's only one term in the list //Just compare and stick it in front or behind the existing node if(terms.size()==1) { int this_exp = l-&gt;second; int exp_to_ins = r-&gt;second; if(exp_to_ins &gt; this_exp) terms.push_back((*r)); if(exp_to_ins &lt; this_exp) terms.push_front((*r)); inserted = 1; } //If there's more than one node, we need to traverse the list if(terms.size()&gt;1) { if(l!=terms.begin()) { int this_exp = l-&gt;second; l++; int next_exp = l-&gt;second; int exp_to_ins = r-&gt;second; //If the new node value is between the current and next node //Insert between them. if((this_exp &lt; exp_to_ins) &amp;&amp; (exp_to_ins &lt; next_exp)) { terms.insert(l,(*r)); inserted = 1; } } else if(l==terms.begin()) { int this_exp = l-&gt;second; int exp_to_ins = r-&gt;second; //This will be the smallest order node //Put it in the top spot if(this_exp &gt; exp_to_ins) { terms.push_front((*r)); inserted = 1; } l++; } } } //If we've traversed the list and can't find the right place //this must be the greatest order node in the list //so just tack it on the end. if(!inserted) terms.push_back((*r)); } } } </code></pre> <p>Works fine with ordering the nodes in the correct order, but we have an existing nth order node, rather than just adding the coefficients together, it keeps the original node but seems to make a second node with the coefficients added together, and I have no idea why.</p> <p>If I run the print function, for what should be F(x) = -2x^7 + 3x^6 - 11x^5 - 2x^4, instead I get F(x) = -2x^7 + 3x^6 - 11x^5 - 10x^5. If I call the size() function on the list, I get 4. But if I run the following code to print out the info from the nodes in the list:</p> <pre><code>stringstream test; for(i=terms.end(); i!=terms.begin(); i--) { test &lt;&lt; "Coefficient: " &lt;&lt; i-&gt;first &lt;&lt; " "; test &lt;&lt; "Exp: " &lt;&lt; i-&gt;second &lt;&lt; endl; } cout &lt;&lt; "Size: " &lt;&lt; terms.size() &lt;&lt; endl; cout &lt;&lt; test.str(); </code></pre> <p>The following is output:</p> <p>Coefficient: -10 Exp: 5 Coefficient: -2 Exp: 7 Coefficient: 3 Exp: 6 Coefficient: -11 Exp: 5</p> <p>Any help greatly appreciated.</p> <p>EDIT: This is the test program.</p> <pre><code>Polynomial p(pair&lt;double, int&gt;(-10, 5)); p.add(Polynomial(pair&lt;double,int&gt; (-2,4))); p.add(Polynomial(pair&lt;double,int&gt; (3,6))); p.add(Polynomial(pair&lt;double,int&gt; (-2,7))); p.add(Polynomial(pair&lt;double, int&gt; (-1,5))); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload