Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked list maintain the root pointer address to read it later after building it
    primarykey
    data
    text
    <p>I am a new learner and trying to build linked list. I am able to do so but I am trying to keep the pointer of root or first node so after building linked list I can read the list or execute the pattern matching but I am not able to do it successfully. Can you please help here?</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct node { int x; struct node *next; }; int main(){ int d; struct node *linked; struct node *head; linked = malloc (sizeof(struct node)); head = linked; &lt;&lt;&lt;&lt; As this is pointer, in while loop whenever malloc executes it changes the value of head as well. printf ("Location of head %p \n", head-&gt;next); 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-&gt;next); printf ("To enter further value enter non zero: "); scanf ("%d", &amp;d); if (d==0) linked-&gt;next = NULL; } //linked-&gt;next = NULL; printf("Value of Next is %p\n", linked-&gt;next); printf ("Location of head %p \n", head-&gt;next); } </code></pre> <p>Output:</p> <blockquote> <p>MacBook-Air:cprog jimdev$ ./a.out </p> <p>Location of head 0x7fff90ab952c &lt;&lt;&lt;&lt; this value should not be change but here in sub sequent output it is.</p> <p>Enter the value of X: 0</p> <p>Location of linked 0x7ff0624039c0 </p> <p>Location of head 0x7ff0624039c0 &lt;&lt;&lt;&lt; different value then previous</p> <p>To enter further value enter non zero: 3</p> <p>Enter the value of X: 3</p> <p>Location of linked 0x7ff0624039d0 </p> <p>Location of head 0x7ff0624039d0 &lt;&lt;&lt;&lt; different value then previous</p> <p>To enter further value enter non zero: 0</p> <p>Value of Next is 0x0</p> <p>Location of head 0x0</p> </blockquote> <p>I just tried this new one it does the printf of linked list elements as well, let me know if you guys think of any improvement. I know recursion is quick and neat way to achieve it but I wanted to try out something with while loop.</p> <h1>include </h1> <h1>include </h1> <p>struct node {</p> <pre><code> int x; struct node *next; }; </code></pre> <p>int main () {</p> <pre><code> int d, hold, i; struct node *list; struct node *head; struct node *current; list = (node *)malloc(sizeof(struct node)); head = list; printf ("Location of list is %p \n", head); d = 1; </code></pre> <p>while (d>0){</p> <pre><code> printf ("Enter the value of X: "); scanf ("%d", &amp;list-&gt;x); printf ("Location of list is %p\n", list); current = (node *)malloc (sizeof (struct node)); list-&gt;next = current; list = current; printf ("Location of head is %p\n", head); printf ("Enter zero to terminate the loop: "); scanf ("%d", &amp;d); } list-&gt;next = NULL; printf ("Value of last next is %d\n", list-&gt;next); current = head; i = 1; while (current-&gt;next != 0){ printf ("Location of list is %p \n", current); printf ("Value of linked list %d elements %d \n", i, current-&gt;x); current = current-&gt;next; i++; } scanf ("%d", &amp;hold); </code></pre> <p>}</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. 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