title: C++ linked list implementation, goes to infinite loop while traversing. Guessing something is wrong with the contructors
body: <p>I'm new to C++. I'm trying to implement a linked list. The output goes to an infinite loop when the traverse function is called(output shown at the bottom). Had no errors when I used 'new' in the insertNodeAtEnd function instead of contructors, but I read up that it's generally not a good practice in C++ and it's better to get accustomed to using constructors.
  What am I doing wrong?</p>
  <pre><code>#include &lt;iostream&gt;
  
  struct Node
  {
      int data;
      Node *next;
      Node(int data)
      {
          this-&gt;data = data;
          this-&gt;next = NULL;
      }
  
      ~Node() {}
  };
  
  class LinkedList
  {
    public:
      LinkedList()
      {
          std::cout &lt;&lt; &quot;Linked list created \n&quot;;
      }
  
      static Node *head;
      static Node *tail;
      static int numberOfNodes;
  
      static int getNumberOfNodes()
      {
          return numberOfNodes;
      }
  
      static void insertNodeAtEnd(int data)
      {
          Node newNode(data);
          if (head == NULL)
          {
              head = tail = &amp;newNode;
              return;
          }
  
          tail-&gt;next = &amp;newNode;
          tail = &amp;newNode;
          numberOfNodes++;
          return;
      }
  
      static void traverse()
      {
          if (numberOfNodes == 0)
          {
              std::cout &lt;&lt; &quot;Linked list is empty \n&quot;;
          }
          Node *curr = head;
          while (curr != NULL)
          {
              std::cout &lt;&lt; curr-&gt;data &lt;&lt; std::endl;
              curr = curr-&gt;next;
          }
          return;
      }
  
      ~LinkedList()
      {
          std::cout &lt;&lt; &quot;Linked list destroyed \n&quot;;
      }
  };
  
  Node *LinkedList::head = NULL;
  Node *LinkedList::tail = NULL;
  int LinkedList::numberOfNodes = 0;
  
  int main()
  {
      LinkedList linkedlist;
      linkedlist.insertNodeAtEnd(40);
      linkedlist.insertNodeAtEnd(50);
      linkedlist.insertNodeAtEnd(60);
      linkedlist.insertNodeAtEnd(70);
      linkedlist.traverse();
  }
  </code></pre>
  <p>And here's the output. (Infinite loop, had to terminate in the console.)</p>
  <pre><code>Linked list created 
  70
  70
  70
  70
  70
  70
  70
  </code></pre>