title: C linked list - inserting node at the end
body: <p>I am trying to read from a list that has 1 word per line and 20 lines, and then add each word to the end of a linked list. My problem is that when I print out the linked list at the end it is printing that last word in the file 20 times instead of each word once. I have been working on this for hours and can't figure out what i'm doing wrong.</p>
  
  <p>In my main I have</p>
  
  <pre><code>while (!feof(input)) {
          fscanf(input, "%s", currentName);
          head = insert(head, currentName);
  }
  print(head);
  delete(head);
  </code></pre>
  
  <p><strong>Insert Function</strong></p>
  
  <pre><code>node* insert(node* head, char* name) {
          node *temp = NULL;
          if (head == NULL) {
                  head = create_node(name);
          }
          else {
                  temp = head;
                  while(temp-&gt;next != NULL){
                          temp = temp-&gt;next;
                  }
                  temp-&gt;next = create_node(name);
              }
          return head;
  }
  </code></pre>
  
  <p><strong>Create Function</strong></p>
  
  <pre><code>node* create_node(char* name) {
          node *newNode;
          newNode = malloc(sizeof(node));
          if(newNode == NULL) {
                  printf("Failed to create node");
          }
          newNode-&gt;name = name;
          newNode-&gt;next = NULL;
          return newNode;
  }
  </code></pre>
  
  <p><strong>Print and Delete</strong></p>
  
  <pre><code>void print(node* head){
          while (head != NULL) {
                  printf("%s -&gt; ", head-&gt;name);
                  head = head-&gt;next;
          }
          printf("NULL\n");
  }
  void delete(node* head) {
          node *temp = NULL;
          while(head != NULL) {
                  temp = head;
                  head = head-&gt;next;
                  free(temp);
          }
  }
  </code></pre>