Note that there are some explanatory texts on larger screens.

plurals
  1. POlinked list in c, unexpected results
    primarykey
    data
    text
    <p>I'm new at programming and am trying to learn linked lists. I've decided to experiment with linked lists by writing a simple program that will read from a file, one character at a time, and insert each character into a linked list. Then I print the linked list out. Simple, right? Well, maybe not if it's your first time. I'm following the online tutorials oh, so carefully, but my output isn't what it's supposed to be. (The program compiles and runs with no errors or warnings. I'm using Code Blocks.) Instead of getting any characters, I get two numbers, and that's all.</p> <p>Here is the code I wrote:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; typedef struct Tokens_read_in{ char character; int number; char whichOne[5]; struct Tokens_read_in* next; }Tokens; int main() { //declare variables char c; //create a struct for the array that will hold the tokens Tokens* token_array; token_array = malloc(sizeof(Tokens)); token_array-&gt;next = NULL; //open the input file FILE *ifp; //input file pointer char *filename = "input.txt"; ifp = fopen(filename, "r"); if(!ifp){ printf("Error in opening '%s' for reading!", filename); exit(0); } while(!feof(ifp)){ //prepare to read in file one character at a time c = getc(ifp); //create a struct for the current token that is read in Tokens* current_token = token_array; //let the current_token point to the beginning of token_array current_token = token_array; //let the current_token point to the LAST of token_array while(current_token-&gt;next != NULL){ current_token = current_token-&gt;next; } //create a node at the end of token_array current_token-&gt;next = malloc(sizeof(Tokens)); //move the current_token to the last (new) of token_array current_token = current_token-&gt;next; if(current_token == NULL){ printf("Out of memory"); exit(0); } //plug character into current_token current_token-&gt;next = NULL; //letter if(isalpha(c)){ printf("%c", c); current_token-&gt;character = c; strcpy(current_token-&gt;whichOne, "char"); } //number else if(isdigit(c)) { printf("%d", (int)c); current_token-&gt;number = (int)c; strcpy(current_token-&gt;whichOne, "num"); } //space //this does not need to go into the token array else if (c == ' '){ printf(" "); } //newline //this does not need to go into the token array else if (c == '\n'){ printf("\n"); } //anything else else if ((!isdigit(c) &amp;&amp; !isalpha(c))){ printf("%c", c); current_token-&gt;character = c; strcpy(current_token-&gt;whichOne, "char"); } //now that the current_token is plugged into token_array, free current_token free(current_token); }//end while(!feof(ifp)) //print the token_array Tokens* conductor; conductor = token_array; while(conductor != NULL){ if(strcmp(conductor-&gt;whichOne, "num")){ printf("%d ", conductor-&gt;number); } else if(strcmp(conductor-&gt;whichOne, "char")){ printf("%c ", conductor-&gt;character); } conductor = conductor-&gt;next; } //done printing, so free conductor free(conductor); //done with program, so free token_array free(token_array); //close input file fclose(ifp); return 0; }//end main </code></pre> <p>And here is the input file I'm using (named input.txt):</p> <pre><code>&lt;I don't know why every beginner program says hello world, but hello world anyway.&gt; </code></pre> <p>I really appreciate anyone who looks at this and points me into the right direction.</p>
    singulars
    1. This table or related slice is empty.
    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