Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Of course there's hope, we all have to start somewhere! :)</p> <p>First of all, I must point out a dangerous practice in your implementation that my own students used, and it usually resulted in lots of head scratching to find the problem:</p> <pre><code>while ( headByName-&gt;nextByName != NULL ) { headByName-&gt;nextByName = tail_node; tail_node = next_node; //next_node = current_node; } </code></pre> <p>Don't use a list member like headByName as your iterating variable within your loop unless it is absolutely necessary. You should find the appropriate node first using a local variable, and then modify that node.</p> <p>That said, Rob Kennedy is right that you <em>should</em> handle the name and rating separately (in other words a 'pure' implementation would have two instances of a generic list class that is unaware of what 'name' and 'rating' mean), however I assume the interfaces for list, node and function above were given to you in the assignment (if not, disregard the rest of my answer :) ).</p> <p>Given the above interfaces, my approach would be as follows:</p> <pre><code>void list::insert(const winery&amp; winery) { node* wineryNode = new node(winery); assert (wineryNode); // Find a node in the list with a name greater than wineryNode's node* prevNode = NULL; node* searchNode = headByName; // Note: not using the list member itself to iterate but a local variable while (NULL != searchNode &amp;&amp; searchNode.winery.getName() &lt; wineryNode.winery.getName()) { // Keep iterating through the list until there are no more items, or the right node is found prevNode = searchNode; searchNode = searchNode-&gt;nextByName; } /* At this point searchNode is either NULL or it's name is greater than or equal to wineryNode's */ // Check for the case where the list was empty, or the first item was what we wanted if (NULL == prevNode) { headByName = wineryNode; } else { // prevNode is not NULL, and it's Name is less wineryNode's prevNode-&gt; nextByName = wineryNode; } wineryNode-&gt;nextByName = searchNode; /* Now you just need to insert sorted by rating using the same approach as above, except initialize searchNode to headByRating, and compare and iterate by ratings in the while loop. Don't forget to reset prevNode to NULL */ } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    3. 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