title: Simple linked list in C++
body: <p>I am about to create a linked that can insert and display until now:</p>
  
  <pre><code>struct Node {
      int x;
      Node *next;
  };
  </code></pre>
  
  <p>This is my initialisation function which only will be called for the first <code>Node</code>:</p>
  
  <pre><code>void initNode(struct Node *head, int n){
      head-&gt;x = n;
      head-&gt;next = NULL;
  }
  </code></pre>
  
  <p>To add the <code>Node</code>, and I think the reason why my linked list isn't working correct is in this function:</p>
  
  <pre><code>void addNode(struct Node *head, int n){
      struct Node *NewNode = new Node;
      NewNode-&gt; x = n;
      NewNode -&gt; next = head;
      head = NewNode;
  }
  </code></pre>
  
  <p>My <code>main</code> function:</p>
  
  <pre><code>int _tmain(int argc, _TCHAR* argv[])
  {
      struct Node *head = new Node;
  
      initNode(head, 5);
      addNode(head, 10);
      addNode(head, 20);
      return 0;
  }
  </code></pre>
  
  <p>Let me run the program as I think it works. First I initialise the head <code>Node</code> as a <code>Node</code> like this:</p>
  
  <pre><code>head = [ 5 |  NULL ]
  </code></pre>
  
  <p>Then I add a new node with n = 10 and pass head as my argument.</p>
  
  <p>NewNode = [ x | next ] where next points at head. And then I change the place where head is pointing to NewNode, since NewNode is the first Node in LinkedList now.</p>
  
  <p>Why isn't this working? I would appreciate any hints that could make me move in the right direction. I think LinkedList is a bit hard to understand.</p>
  
  <p>When I'm printing this, it only returns 5:</p>