Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'll have an issue if the head node is a 97%, and you pass a node with a 97%. You need to say </p> <pre><code>while (curr_std != NULL &amp;&amp; to_add-&gt;grade &lt;= curr_std-&gt;grade){ </code></pre> <p>You'll also have an issue if the student you're editing is the first node, because this:</p> <pre><code>while((cur != NULL) &amp;&amp; (strcmp(cur-&gt;name,temp) != 0)){ </code></pre> <p>will evaluate to true, and </p> <pre><code>prev = cur; </code></pre> <p>will never get called, leaving <code>prev</code> = null. Then when you get to </p> <pre><code>prev-&gt;next = cur-&gt;next; </code></pre> <p>you have a null reference. You need to explicitly test for adding to the head node; it's its own special case.</p> <pre><code>scanf("%d", &amp;(cur-&gt;grade)); if (prev == null) { // you matched the head head = cur-&gt;next; } else { prev-&gt;next = cur-&gt;next; } </code></pre> <p><br /> <strong>EDIT</strong><br /> When you add to the head, in your code, you haven't set the head to point to your new node. You're returning the old head, which now points to the second node in the list. Try:</p> <pre><code>while (curr_std != NULL &amp;&amp; to_add-&gt;grade &lt; curr_std-&gt;grade){ prev_std = curr_std; curr_std = curr_std-&gt;next; } // if you're adding to the head, you didn't go into the above loop // curr_std is still pointing to head in this case if (curr_std == head) { head = to_add } else { prev_std-&gt;next = to_add; } to_add-&gt;next = curr_std; return head; </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