title: What is wrong with my Singly Linked List implementation?
body: <p>Here's an attempt at implementing a singly linked list.<br>
  The problem is that when trying to print the list with <code>while (traverse != NULL)</code>, the program outputs 1, the data of the first node, but doesn't print the data of all the other nodes.  Am I linking the nodes incorrectly, and if so, where?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct node {
      int data;
      struct node *next;
  } Node;
  
  struct node *root;
  
  int main(void) {
      Node *list, *traverse;
      /* root will always be the first of the list */
      root = malloc(sizeof(*list));
  
      list = root;
  
      list-&gt;data = 1;
      list-&gt;next = NULL;
      list = list-&gt;next;
  
      list = malloc(sizeof(*list));
      list-&gt;data = 2;
      list-&gt;next = NULL;
      list = list-&gt;next;
  
      list = malloc(sizeof(*list));
      list-&gt;data = 3;
      list-&gt;next = NULL;
      list = list-&gt;next;
  
      list = malloc(sizeof(*list));
      list-&gt;data = 4;
      list-&gt;next = NULL;
      list = list-&gt;next;
  
      list = malloc(sizeof(*list));
      list-&gt;data = 5;
      list-&gt;next = NULL;
      list = list-&gt;next;
  
      traverse = root;
  
      while (traverse != NULL) {
          printf("%d\n", traverse-&gt;data);
          traverse = traverse-&gt;next;
      }   
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>$ gcc main.c &amp;&amp; ./a.out
  1
  </code></pre>
  
  <p><br>
  Expected output:</p>
  
  <pre><code>$ gcc main.c &amp;&amp; ./a.out
  1
  2
  3
  4
  5
  </code></pre>
  
  <p>Update:
  I've updated my source file just as all of you have suggested to this: <br></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct node {
      int data;
      struct node *next;
  } Node;
  
  struct node *root;
  
  int main(void) {
      Node *list, *traverse;
      /* root will always be the first of the list */
      root = malloc(sizeof(*list));
  
      list = root;
  
      list-&gt;data = 1;
      list-&gt;next = malloc(sizeof(*list));
  
      list = list-&gt;next;
      list-&gt;data = 2;
      list-&gt;next = malloc(sizeof(*list));
  
      list = list-&gt;next;
      list-&gt;data = 3;
      list-&gt;next = malloc(sizeof(*list));
  
      list = list-&gt;next;
      list-&gt;data = 4;
      list-&gt;next = malloc(sizeof(*list));
  
      list = list-&gt;next;
      list-&gt;data = 5;
      list-&gt;next = NULL;
  
      traverse = root;
  
      while (traverse != NULL) {
          printf("%d\n", traverse-&gt;data);
          traverse = traverse-&gt;next;
      }   
      return 0;
  }
  </code></pre>
  
  <p>Thank-you so much everyone!</p>