Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code has several problems. First, you're not checking the <em>last</em> node. A loop condition like this:</p> <pre><code>while (travel2-&gt;next != NULL &amp;&amp; travel1-&gt;next != NULL) </code></pre> <p>will break as soon as one of the enumerators reaches the last node, but never checks it. Further, it will also mean two sets of a <em>single</em>-node-each will always compare true.</p> <p>Next, You have a hard return after only the first iteration, so there is no conceivable way this <em>ever</em> returned false on two sets that started with the same node value.</p> <pre><code>travel2 = travel2-&gt;next; travel1 = travel1-&gt;next; return true; // this doesn't belong here. </code></pre> <p>Next, you pass your parameter <strong>by-value</strong>, which means the copy constructor is being invoked. I don't know whether you implemented it or not (and if you didn't, you have an entire different problem), but there is no reason to duplicate the list just to see if it is equal to <code>*this*</code>. The function should take a const-reference as the parameter.</p> <pre><code>bool set::equalset(const set&amp; second) </code></pre> <p>Finally, your exit condition is correct, but you cannot assume the lists were both exhausted. you have to verify it. You can do this by returning <strong>false</strong> if <em>either</em> traveler is non-null (and one of them will be if the lists are uneven.</p> <p>Putting it all together:</p> <pre><code>bool set::equalset(const set&amp; second) { const Data *travel1 = top; const Data *travel2 = second.top; while (travel1 &amp;&amp; travel2) { if (!in(travel2-&gt;value)) //in checks if the value is in the set return false; if (!second.in(travel1-&gt;value)) //same idea here return false; travel2 = travel2-&gt;next; travel1 = travel1-&gt;next; } return !(travel1 || travel2); } </code></pre> <hr> <p><strong>Optimized for Sorted Lists</strong></p> <p>If you keep the lists <em>sorted</em> during input and removal methods, you can significantly make this easier, seen below:</p> <pre><code>bool set::equalset(const set&amp; second) { const Data *travel1 = top; const Data *travel2 = second.top; while (travel1 &amp;&amp; travel2 &amp;&amp; travel1-&gt;value == travel2-&gt;value) { travel1 = travel1-&gt;next; travel2 = travel2-&gt;next; } return !(travel1 || travel2); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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