Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd new struct at the end of the list in C
    primarykey
    data
    text
    <p>I would like to insert the struct at the end of the list, and I can not get the code compiled. Here are some of the pseudo code I had for the insert() function. They rest of the code should remain the same. </p> <pre><code> (check it succeeded) set the data for the new person if the current list is empty (i.e. NULL) do the same as insert_start i.e. set the new person's "next" link to point to the current list (i.e. NULL) return the (start of the) new list i.e. a pointer to the new person otherwise use a loop to find the last item in the list (i.e. the one which has a "next" link of NULL) set the "next" link of this item to point to the new person so the new person becomes the last item in the list (i.e. the new person should have a "next" link of NULL) return the (start of the) list </code></pre> <p>Compile errors: </p> <pre><code>error: 'true' undeclared (first use in this function) insert_end.c:47:13: note: each undeclared identifier is reported only once for each function it appears in insert_end.c:57:3: warning: 'return' with a value, in function returning void make: *** [insert_end] Error 1 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* these arrays are just used to give the parameters to 'insert', to create the 'people' array */ char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim", "Harriet"}; int ages[7]= {22, 24, 106, 6, 18, 32, 24}; /* declare your struct for a person here */ typedef struct Record{ char *name; int age; struct Record *next; } Record; //set the head pointer at the start of the list Record *headptr = NULL; static void insert (Record *p, char *s, int n) { /* allocate heap space for a record */ Record *ptr =(Record*) malloc(sizeof(Record)); if(ptr == NULL){ abort(); printf("memory allocation fail"); exit(1); }else{ printf("memory allocation to person - %s - \n", s); } ptr-&gt;name=s; ptr-&gt;age=n; ptr-&gt;next = NULL; if(p-&gt;next==NULL) { p-&gt;next= ptr; }else{ Record *current = p; while(true){ if(current-&gt;next ==NULL) { current-&gt;next = ptr; break; } current= current-&gt;next; } } return 0; } int main(int argc, char **argv) { /* declare the people array here */ Record *p; headptr = NULL; //insert the members and age into the unusage array. for (int i=0; i &lt; 7; i++) { insert (p, names[i], ages[i]); /* do not dereference the pointer */ } /* print out a line before printing the names and ages */ printf("\n"); //set the pointer at the start of the list p = headptr; /* print the people array here*/ for (int i=0; i &lt; 7; i++, p = p-&gt;next) { printf("The name is: %s, the age is:%i\n", p-&gt;name, p-&gt;age); } /* This is the third loop for call free to release the memory allocated by malloc */ /* the free()function deallocate the space pointed by ptr. */ for(int i=0; i&lt;7;i++){ free(p); } } </code></pre>
    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.
    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