title: how to keep the head of a linked list in c language?
body: <p>i have managed to read data from a file and store them into a linked list, but the problem is that i lost the head, so when ever i try to print the data it print the last one then the one before and so on.</p>
  
  <p>the file is something like this : </p>
  
  <p>tom;14</p>
  
  <p>sara;19</p>
  
  <p>this is the code i use to read the data : </p>
  
  <pre><code>   if ( f != NULL ) {
      while ( fgets( buffer, sizeof( buffer ), f ) != NULL ) {
          person*n= (person*) malloc( sizeof( process ) );
  
          name = strtok( buffer, ";\n\r\t" );
          strcpy( n-&gt;NAME, name);
  
          age= strtok( NULL, ";\n\r\t" );
          n-&gt;AGE= atoi(age);
          n-&gt;next= head;
          head = n;
      }
  
      fclose( f );
  </code></pre>
  
  <p>the output is like this : </p>
  
  <p>sara; 19</p>
  
  <p>tom;14</p>
  
  <p>when ever i use head in a print function it starts printing from the last element, so my guess is that head didn't point to the first element when i finished reading the data. My solution was to use another pointer , so i used head2 like this :</p>
  
  <p>i put head2 = NULL, and when reading i did this : </p>
  
  <pre><code>if(head2==NULL) head2=n;
  </code></pre>
  
  <p>but again, i faced another problem, because when i use head2 in a print function it only print the first element : </p>
  
  <p>tom;14</p>
  
  <p>Any help on this problem please ?</p>