title: My linked list is printing the last word in my text file for all the elements
body: <p>This code is reading a text file and inserting each word into the linked list.
  I am new to linked lists and have been working on this for four hours now and cannot for the life of me figure this out.</p>
  
  <p>So what is going on here? I have checked every way I know how to, and for the life of me cannot get the linked list to print correctly. I believe it has something to do with the <code>push</code>/<code>append</code> functions. Somehow it is overwriting everything previously in the linked list. Maybe the <code>printlist</code> function is overwriting everything but I don't see how it could possibly be doing that.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  
  // A complete working C program to demonstrate all insertion methods
  // on Linked List  
  // A linked list node
  struct Node;
  void push(struct Node **head_ref, char *new_data);
  void insertAfter(struct Node *prev_node, char *new_data);
  void append(struct Node **head_ref, char *new_data);
  void printList(struct Node *node);
  int LINECOUNT(FILE *(*stream), char *filename);
  
  struct Node {
      char *data;
      struct Node *next;
  };
  
  /* Given a reference (pointer to pointer) to the head of a list and 
     an int, inserts a new node on the front of the list. */
  void push(struct Node **head_ref, char *new_data) {
      /* 1. allocate node */
      struct Node* new_node = (struct Node *)malloc(sizeof(struct Node));
  
      /* 2. put in the data  */
      new_node-&gt;data  = new_data;
      printf("push data:%s ", new_data);
  
      /* 3. Make next of new node as head */
      new_node-&gt;next = (*head_ref);
  
      /* 4. move the head to point to the new node */
      (*head_ref) = new_node;
  }
  
  /* Given a reference (pointer to pointer) to the head
     of a list and an int, appends a new node at the end  */
  void append(struct Node **head_ref, char *new_data) {
      /* 1. allocate node */
      struct Node* new_node = (struct Node *)malloc(sizeof(struct Node));
      struct Node *last = *head_ref;  /* used in step 5*/
  
      /* 2. put in the data  */
      new_node-&gt;data  = new_data;
      printf("push data:%s ", new_data);
  
      /* 3. This new node is going to be the last node, so make next of
            it as NULL*/
      new_node-&gt;next = NULL;
  
      /* 4. If the Linked List is empty, then make the new node as head */
      if (*head_ref == NULL) {
         *head_ref = new_node;
         return;
      }
  
      /* 5. Else traverse till the last node */
      while (last-&gt;next != NULL)
          last = last-&gt;next;
  
      /* 6. Change the next of last node */
      last-&gt;next = new_node;
      return;
  }
  
  // This function prints contents of linked list starting from head
  void printList(struct Node *node) {
      while (node != NULL) {
          printf(" %s ", node-&gt;data);
          node = node-&gt;next;
      }
  }
  
  int LINECOUNT(FILE *(*stream), char *filename) {            
      int size = 0;
      size_t chrCount;
      char *text;
  
      if ((*stream = fopen(filename, "r")) == NULL) {
          printf("LC Could not open hw8 data file.\n");
          exit(0);
      }
      while (1) {
          text = NULL;
          getline(&amp;text, &amp;chrCount, *stream); 
          free(text); /*free text*/
          if (feof(*stream))
              break;
          size++;
      }
      rewind(*stream);
      return size;
  }
  
  /*int wordCount(FILE *(*stream), char *filename, int lineCount) {
      char ch;
      int wordcount = 0;
      int charcount = 0; 
      *stream = fopen(filename, "r");
      int x = 0;
      int lineArr[lineCount];
      for (int i = 0; i &lt; lineCount; i++) {
          lineArr[i] = 0;
      }
      if (*stream) {
          while ((ch = getc(*stream)) != EOF) {
              if (ch != ' ' &amp;&amp; ch != '\n') {
                  charcount++;
              }
              if (ch == ' ' || ch == '\n') { 
                  wordcount++;
                  lineArr[x] = lineArr[x] + 1;
              }
              if (ch == '\n') {
                  x++;
              }
          }
          if (charcount &gt; 0) {
              wordcount++;
              charcount++;
          }
      } else {
          printf("Failed to open the file\n");
      }
  
      // rewind(*stream);
      return lineArr;
  }*/
  
  int main(void) {
      char *fn = "hw8data.txt";
      int lineCount;
      FILE *stream;
  
      lineCount = LINECOUNT(&amp;stream, fn);
      //int lineArr[lineCount];
      //int lineArr[];//lineArr[0] = 4 would say the first line has 4 words. using this data for strtok
  
      //lineArr = wordCount(&amp;stream, fn, lineCount);
  
      //-------------------------------------
      char ch;
      int wordcount = 0;
      int charcount = 0; 
      stream = fopen("./hw8data.txt", "r");
      int x = 0;
      int lineArr[lineCount];
      for (int i = 0; i &lt; lineCount; i++) {
          lineArr[i] = 0;
      }
      if (stream) {
          while ((ch = getc(stream)) != EOF) {
              if (ch != ' ' &amp;&amp; ch != '\n') {
                  charcount++;
              }
              if (ch == ' ' || ch == '\n') {
                  wordcount++;
                  lineArr[x] = lineArr[x] + 1;
              }
              if (ch == '\n') {
                  x++;
              }
          }
          //if (charcount &gt; 0) { wordcount++; charcount++; }
      } else {
          printf("Failed to open the file\n");
      }
      /* Start with the empty list */
      struct Node *head = NULL;
      rewind(stream);
      char *sArr = malloc(42 * sizeof(char));
  
      fscanf(stream, "%s ", sArr);
      printf("%s ", sArr);
      push(&amp;head, sArr);
      fscanf(stream, "%s ", sArr);
      printf("%s ",sArr);
      append(&amp;head, sArr);
      printList(head);
      return 0;
  }
  </code></pre>