Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked list Node Insert to Front - all content value changing each time I add a value
    primarykey
    data
    text
    <p>I am making a singly linked list whose node has a string and a pointer to next node. I have written a function to insert to front of the linked list. The problem is that whenever I insert a new value to the linked list, it changes the value of all the nodes. I don't know where I'm going wrong. Please help. Here is the code</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; typedef struct Elem { char *word; struct Elem *next; } Elem; void printList (Elem **list) { if(!(*list)){ printf("List is empty"); return ; } Elem *curr; curr = *list; while(curr) { printf("%s -- ",(curr)-&gt;word); curr = (curr)-&gt;next; } } void insert_in_front(Elem **list, char *value) { if(!*list) { printf("List is empty... creating first node\n"); (*list) = (Elem*) malloc(sizeof(Elem)); (*list)-&gt;word = value; (*list)-&gt;next = NULL; return ; } printf("The word in list is %s\n",(*list)-&gt;word); Elem *curr = (Elem*) malloc(sizeof(Elem)); if(!curr) exit(-1); curr-&gt;word = value; curr-&gt;next = (*list); printf("the address of curr is : 0x%x\n",curr); (*list) = curr; printf("the address of list is : 0x%x\n",(*list)); } int main(void) { Elem *newList; newList = NULL; char inp[15]; while(1) { printf("Enter the string : "); scanf("%s",&amp;inp); printf("input is %s",inp); printf("\nthe address of newList is : 0x%x\n",newList); insert_in_front(&amp;newList, &amp;inp); printf("the address of newList is : 0x%x\n",newList); printList(&amp;newList); printf("the address of newList is : 0x%x\n",newList); printf("\n"); } return 0; } </code></pre> <p>You can copy paste the code to run. The output is as follows : Please excuse the debug messages. I just wanted to see if the pointer is pointing to new location after each insert.</p> <pre><code>Enter the string : hello input is hello the address of newList is : 0x0 List is empty... creating first node the address of newList is : 0x251b010 hello -- the address of newList is : 0x251b010 Enter the string : world input is world the address of newList is : 0x251b010 The word in list is world the address of curr is : 0x251b030 the address of list is : 0x251b030 the address of newList is : 0x251b030 world -- world -- the address of newList is : 0x251b030 Enter the string : testing input is testing the address of newList is : 0x251b030 The word in list is testing the address of curr is : 0x251b050 the address of list is : 0x251b050 the address of newList is : 0x251b050 testing -- testing -- testing -- the address of newList is : 0x251b050 Enter the string : </code></pre> <p>Thanks in advance!</p>
    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.
 

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