Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>int printList(struct listNode * Head){ struct listNode *current = Head; while (Head != NULL){ printf("%d \n", *current); current = current-&gt;next; } </code></pre> <p>You're pretty close. </p> <p>Take a look at the condition on your while loop - the reason your program crashes is that 'Head' is never updated, so the condition is always true. So the program just keeps setting 'current' equal to 'current->next' without stopping until you get to the end of your list, at which point 'current->next' is NULL and the program crashes. </p> <p>If you change your while loop to check whether 'current' is NULL instead of 'Head', it will stop when it reaches the end of the list and your program won't crash.</p> <p>EDIT: Adding some pointers on fixing the extra zeroes showing up the linked list.</p> <pre><code>struct listNode Head = {0, NULL}; </code></pre> <p>At the beginning of your program, you're creating a node in your linked list with the value 0. So you've always got at least one 0 no matter what your input is. You might consider initializing Head to NULL instead. If you do that, you'll have to check for that condition in your insertNode function.</p> <p>You're also getting some extra zeroes because you're checking your loop condition ('while(x > 0)') <em>before</em> you get the input that you use to make that decision ('scanf("%d", &amp;x);'). You might want to consider changing that order by using a 'do...while' instead of 'while'. Take a look at <a href="http://www.cprogramming.com/tutorial/c/lesson3.html" rel="nofollow">http://www.cprogramming.com/tutorial/c/lesson3.html</a> for an explanation of 'do...while' with examples. </p>
 

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