title: Linked list issue from reading a file c
body: <p>I try to design a linked list from a file, but there's some problem when setting the <strong>'first node' of the linked list</strong>. The data structure of the linked list is shown as the attached picture below:
  <a href="https://i.stack.imgur.com/2pEPE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2pEPE.png" alt="Data structure of linled list"></a></p>
  
  <p>In my code, I put two type of structures, one for name, another for score, as below:</p>
  
  <pre><code>typedef struct nodeScore{
  
      int score;
      struct nodeScore* nextScore;
  
  }nodeScore;
  
  typedef struct nodeName{
  
      char* firstName;
      char* lastName;
  
      nodeScore* nextScore;
      struct nodeName* nextName;
  
  }nodeName;
  </code></pre>
  
  <p>And there's a function to read the score from file line by line, (each line contains 'First name', 'Last name' and up to 4 'scores'), and return the head of linked list:</p>
  
  <pre><code>nodeName* storeFile(const char* fileName, nodeName* nodeN){
  
  FILE *pFile = fopen(fileName, "r");
  
  char input[512];
  
  char* firstName;
  char* lastName;
  int score;
  
  int line=0;
  
  nodeName* prevName;
  nodeScore* prevScore;
  
  while(fgets(input, 512, pFile)){
  
      printf("Line %d now.\n", ++line);
  
      nodeName* ptrN = (nodeName*)malloc(sizeof(nodeName));//allocate for new node name.
  
      firstName = strtok(input, " ");//first space for first name.
      ptrN-&gt;firstName = firstName;
  
      lastName = strtok(NULL, " ");//second space for last name.
      ptrN-&gt;lastName = lastName;
  
      if(line == 1){
          prevName = ptrN;
          nodeN = ptrN;//allocate nodeN the return value to the first line, first node of the linked list.
  
      }else{
          prevName-&gt;nextName = ptrN;
          prevName = ptrN;
  
      }
  
  
      ptrN-&gt;nextName = NULL;
      ptrN-&gt;nextScore = NULL;
  
      while(score = atoi(strtok(NULL, " \n"))){//store multiple scores until     next char is space or next new line.
  
          if(ptrN-&gt;nextScore == NULL){//if no link to another score.
              nodeScore* ptrS = (nodeScore*)malloc(sizeof(nodeScore));//allocate for new score node.
  
              ptrN-&gt;nextScore = ptrS;
              ptrS-&gt;score = score;
              ptrS-&gt;nextScore = NULL;
              prevScore = ptrS;//record latest 'tail' of linked list.
  
  
          }else{
              nodeScore* ptrS = (nodeScore*)malloc(sizeof(nodeScore));
  
              prevScore-&gt;nextScore = ptrS;
              ptrS-&gt;score = score;
              ptrS-&gt;nextScore = NULL;
              prevScore = ptrS;//record latest 'tail' or linked list.
  
          }
  
  
      }//done the loop for storing multi-scores.
  
  }//done the loop for reading lines from file.
  
  return nodeN;
  }
  </code></pre>
  
  <p>And finally the main function:</p>
  
  <pre><code>int main(){
      char file1[]={"HW5_26.txt"};
      nodeName* n1;
  
  printf("\nStart reading file '%s' and store it into linked-list.\n\n", file1);
  
  n1 = storeFile(file1, n1);
  
  printf("%s", n1-&gt;firstName);//try to see who's the first person(1st node, should be Albert Einstein), here is when I'm confused.
  
  return 0;
  }
  </code></pre>
  
  <p>In the end of my return value, I alway get the result of <strong>'Sarah' the last node</strong>, however, I already set an 'if filter' to make the return value only from the 1st line of data, I don't know which part went wrong, I'll be so appreciate if anyone can give me some advice or idea, thank you.</p>
  
  <p>The txt file is like this:</p>
  
  <pre><code>Albert Einstein 52 67 63 
  Steve Abrew 90 86 90 93 
  David Nagasake 100 85 93 89 
  Mike Black 81 87 81 85 
  Andrew Dijkstra 90 82 95 87 
  Joanne Nguyen 84 80 95 91 
  Chris Walljasper 86 100 96 89 
  Fred Albert 70 68 
  Dennis Dudley 74 79 77 81 
  Leo Rice 95 
  Fred Flinstone 73 81 78 74 
  Frances Dupre 82 76 79 
  Dave Light 89 76 91 83 
  Hua Tran 91 81 87 94 
  Sarah Trapp 83 98 94 93
  </code></pre>
  
  <p>And my headers:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  </code></pre>