title: singly linked list c++ constructor, destructor and printing out
body: <p>I am a beginner learning c++, and currently making a singly linked list. I have faced some problems and I thought for a very long time, searched a lot but still do not have an answer for this code so I am begging for some help..</p>
  
  <p>So this is my linked.h</p>
  
  <pre><code>template &lt;class T&gt;
  class Node {
      public:
          T data;
          Node&lt;T&gt;* next;
  };
  
  template &lt;class T&gt;
  class List {
      private:
          Node&lt;T&gt; *head;
      public:
          List() : head(NULL) {};
          ~List() {
              Node&lt;T&gt;* ptr, tmp;
              for(ptr = head-&gt;next; ptr == NULL; ptr = head-&gt;next) {
                  delete ptr;
              }     
          }
          List(T* arr, int n_nodes) {
              head = NULL; 
              Node&lt;T&gt; *tmp = head;
              for(int i = 0; i &lt; n_nodes; i++) {
                  Node&lt;T&gt;* node = new Node&lt;T&gt;;
                  node-&gt;data = arr[i];
                  if(head == NULL) {
                      head-&gt;next = node;
                      tmp = node;
                  }
                  else {
                      tmp-&gt;next = node;
                      node-&gt;next = NULL;
                      tmp = node;
                  }
              }
          }
          friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, List&lt;T&gt;&amp; rhs) {                                                                                                                                 
              Node&lt;T&gt;* cur = rhs.head;
              out &lt;&lt; cur;
              while(cur != NULL) {       
                  if(cur-&gt;next != NULL) { 
                      out &lt;&lt; cur-&gt;data &lt;&lt; ", ";
                      cur = cur-&gt;next;
                  }
                  else
                      out &lt;&lt; cur-&gt;data &lt;&lt; " ";
              }
              return out;
          }
  }; 
  </code></pre>
  
  <p>and this is my main.cc file.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "linked.h"
  
  int main() {
      int array[5] = {12, 7, 9, 21, 13};
      List&lt;int&gt; li(array, 5);
      std::cout &lt;&lt; li;
  
      return 0;
  }
  </code></pre>
  
  <p>I keep on getting segmentation fault when running the constructor and I don't get why. Where am I making a mistake? Any help would be appreciated!</p>