Note that there are some explanatory texts on larger screens.

plurals
  1. POC issue - Can't figure how to assign pointer to beginning of list
    primarykey
    data
    text
    <p>I have a simple assignment that the professor wants us to do. Basically to pull in some numbers from a text file and load into a linked list. I don't want to get to much into the details but I have a basic question.</p> <p>He provided us with a function like so:</p> <pre><code>INTLIST* init_intlist( int n ) { INTLIST *lst; lst = (INTLIST *)malloc(sizeof(INTLIST)); lst-&gt;datum = n; lst-&gt;next = NULL; return lst; } </code></pre> <p>This function is used to initialize the linked list with the first element. Then he asked us to define a function with this signature:</p> <pre><code>int insert_intlist( INTLIST *lst, int n ) </code></pre> <p>So I assume he just wants us to add to the linked list so I tried this:</p> <pre><code>int insert_intlist( INTLIST *lst, int n ) { INTLIST* lstTemp; lstTemp = (INTLIST *)malloc(sizeof(INTLIST)); lstTemp-&gt;datum = n; lstTemp-&gt;next = lst; lst = lstTemp; free(lstTemp); } </code></pre> <p>So what my thought process was is that it creates a temporary node, assigns the data value (Datum) and assigns the next pointer to point to where the current pointer is pointing at. Then I reassign the main pointer to this newly created temp node.</p> <p>That way we have for instance 2 nodes:</p> <p>[New Temp Node] -> [Prev Initialized Node]</p> <p>When I step through the code it looks great...</p> <p>Then back in main I have just a function to print the list:</p> <pre><code> while (lst!=NULL) { printf("The value is:%d", lst-&gt;datum); lst=lst-&gt;next; } </code></pre> <p>The problem is this only seems to print one digit (namely the first digit that I am reading in from the file, which I think is the last one in the list or at least I thought it was the last one in the list).</p> <p>But it should keep going through as I have 10 digits in the file. I know the code is very dirty and I will clean it up...here is my entire main function if anyone needs more info:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "intlist.h" int main(int argc, char *argv[]) { char c; /* Character read from the file. */ FILE* ptr; /* Pointer to the file. FILE is a structure defined in &lt;stdio.h&gt; */ int index=0; //INTLIST* aList[10]; //will use later /* Open the file - no error checking done */ ptr = fopen("1.txt","r"); /* Read one character at a time, checking for the End of File. EOF is defined in &lt;stdio.h&gt; as -1 */ if(ptr==NULL) { printf("Error: can't open file.\n"); /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */ return 1; } //aList[index] = malloc(sizeof(INTLIST)); WE NEED THIS LATER ON.... INTLIST *lst=NULL; while ((c = fgetc(ptr)) != EOF) { if (c != ' ') { //make sure it isnt a space int i = c - '0'; //get the value from the text file if(c=='\n') { // aList[index]=lst; // index++; // aList[index] = malloc(sizeof(INTLIST)); while (lst!=NULL) { printf("The value is:%d", lst-&gt;datum); lst=lst-&gt;next; } free(lst); free(aList[index]); return 0; //new line in the file //create another linked list } if (lst==NULL) lst = init_intlist(i); else insert_intlist( lst, i); } } fclose(ptr); system("PAUSE"); return 0; } </code></pre> <p>Here is intlist.h for anyone who may need it:</p> <pre><code>#ifndef __intlist_h__ #define __intlist_h__ /* each entry in the list contains an int */ typedef struct intlist { int datum; struct intlist *next; } INTLIST; INTLIST *init_intlist( int n ); /* initializes the intlist with initial datum n */ int insert_intlist( INTLIST *lst, int n ); /* Inserts an int (n) into an intlist from the beginning*/ void list_append(INTLIST *list, void *datum); /* Inserts entry to the end of the list */ INTLIST* list_front(INTLIST *list); /*return the element at the front of the list, and remove it from the list*/ void list_map( INTLIST *list, void (*f)(void *) ); /*Applies a function to each element of the list */ void list_delete( INTLIST *list ); /* Deletes (and frees) all entries in the list */ #endif </code></pre>
    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.
 

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