Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>2 things:</p> <ol> <li><p>you are outputting <code>head-&gt;next</code> instead of just <code>head</code></p></li> <li><p>you are not updating <code>linked</code> to <code>linked-&gt;next</code></p></li> </ol> <p>The head node of a linked list is the first element of a list. head->next, when the linked list is well-formed, is the second element. Either way it shouldn't be changing as the list is made after it is created.</p> <p>If you are not updating <code>linked</code> to be <code>linked-&gt;next</code> you are just throwing away the memory you just allocated in <code>linked-&gt;next</code> and not creating a list.</p> <p>This works:</p> <pre><code>int d; struct node *linked; struct node *head; linked = malloc(sizeof(struct node)); head = linked; printf ("Location of head %p \n", head);//changed head-&gt;next to head d = 1; while (d&gt;0){ printf ("Enter the value of X: "); scanf ("%d", &amp;linked-&gt;x); linked-&gt;next = malloc (sizeof(struct node)); printf ("Location of linked %p \n", linked-&gt;next); printf ("Location of head %p \n", head);// changed head-&gt;next to head printf ("To enter further value enter non zero: "); scanf ("%d", &amp;d); linked = linked-&gt;next; //this was added if (d==0) linked-&gt;next = NULL; } </code></pre> <p>Your code, since it wasn't updating <code>linked</code>, kept <code>head</code> and <code>linked</code> equal. Your list looked like this:</p> <p>Node {x:next}</p> <pre><code> -&gt; {x2:NULL} -&gt; {x3:NULL} {x1:node2} -&gt; {x4:NULL} ^^ ^^ (head and linked) you're outputing this node both times </code></pre> <p>Since you don't have any pointers to node2 or node3 you lose these nodes (a memory leak). </p> <p>You had it almost right. This is what a list generated with this code looks like:</p> <pre><code>{x1:node2} -&gt; {x2:node3} -&gt; {x3:node4} -&gt; {x4:node5} ^^ ^^ ^^ head head-&gt;next linked </code></pre> <p><strong>update:</strong></p> <p>I changed the diagram for your code to better reflect what is happening.</p> <p>The <code>ptr-&gt;varname</code> operator looks in location pointed to by <code>ptr</code> for the value of <code>varname</code>. So if the pointers are the same value then you are looking in the same space for the variables. In your case, <code>head=linked</code> means that if you set <code>linked-&gt;next = ptr</code> then <code>head-&gt;next = ptr</code>, and since <code>linked</code> is never updated to be <code>linked-&gt;next</code> (and therefore <code>head-&gt;next</code>) then you are just assigning and reassigning <code>linked-&gt;next</code></p>
    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.
    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