title: Cannot insert and print out linked list
body: <p>I'm trying to create a simple linked list class. The program runs but it gives me wrong output. And it seems like linked list doesn't insert the new nodes as I want.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class Node
  {
  public:
      std::string name;
      Node *next;
  };
  
  class ProductName
  {
  private:
      Node *head;
  public:
      ProductName()
      {
          head = NULL;
      }
      void insertNode(std::string input)
      {
          Node *temp;
          temp = new Node;
          temp-&gt;name = input;
          temp-&gt;next = head;
          head = temp;
      }
      void printOut()
      {
          Node *p;
          p = head;
          while (p-&gt;next != NULL)
          {
              std::cout &lt;&lt; p-&gt;name &lt;&lt; " ";
              p = p-&gt;next;
          }
      }
  };
  
  int main()
  {
      ProductName object;
      object.insertNode("Hello");
      object.insertNode("world!");
      object.printOut();
  }
  </code></pre>
  
  <p>I expect the output is <code>Hello world!</code>, but it prints out a string of random character <code>005C4BA0</code></p>
  
  <p>Edit: I forgot the pointer... It's <code>p-&gt;name</code> not <code>p</code> in the print function. However, now my result is <code>world!</code>. </p>