title: Adding value to singly-linked list
body: <p>I'm in a C++ data structures class and a recent homework assignment has been bugging me for days. It has to deal with working with singly-linked lists, and one of the methods we have to write has to do with adding values to the list.</p>
  
  <p>First off, we create the dummy node <code>head</code>:</p>
  
  <pre><code>template&lt;typename T&gt;
  SLList&lt;T&gt;::SLList()
  {
    head = new Node; // Node() is ok too
  }
  </code></pre>
  
  <p>If I'm correct, this shouldn't hold a value, should it? So head should be <code>NULL</code>. This is what I've managed to create (with my professor's help) so far.</p>
  
  <pre><code>template&lt;typename T&gt;
  void SLList&lt;T&gt;::add(const T&amp; val) {
      bool duplicate = false;
      //Node *ptr = head-&gt;next;
      //cout &lt;&lt; head-&gt;data;
  
      for (Node *ptr = head-&gt;next; ptr != NULL; ptr = ptr-&gt;next) {
      //while (ptr == NULL) {
          //ptr = ptr-&gt;next;
          // if ptr is initialized as head, the first node, then ptr != NULL
          // if ptr is initialized as head-&gt;next, which it should in order to
          // traverse from the node AFTER the head, then ptr == NULL
          // not exactly sure why.
          if (ptr-&gt;data == val) {
              duplicate = true;
              break;
          }
      }
      //Node *temp = new Node(val); // create new node with new element.
      //temp-&gt;next = ptr-&gt;next;
      //ptr-&gt;next = temp;
  
      if (duplicate) {
          cout &lt;&lt; "Duplicate entry found: " &lt;&lt; val &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>I've tried working with <code>for</code> and <code>while</code> loops, since a lot of google results used <code>while</code> loops in their answers. What I'm trying to do is attempt to add a value to the list. If the value is already in that list, it doesn't get added. I'm attempting this with:</p>
  
  <pre><code>SLList&lt;int&gt; iList;
  iList.add(5);
  iList.add(6);
  iList.add(6);
  </code></pre>
  
  <p>When I asked for help, I was told that I should initialize <code>*ptr</code> as <code>head-&gt;next</code> to start traversing the list for the node AFTER the dummy node. I used Visual Studio's debugger and found that after initializing it to <code>head-&gt;next</code> the program never went into the for loop, which I'm assuming is because the node after the dummy node fails the if statement <code>ptr != NULL</code>. I'm not exactly sure why that is, as when I had <code>Node *ptr = head</code> it would traverse through the list just fine (although the following line <code>if (ptr-&gt;data == val)</code> would consider every entry a duplicate entry).</p>
  
  <p>I'm probably understanding this wrong, but shouldn't the <code>head</code> node have a value of <code>NULL</code>? So it shouldn't pass the <code>if</code> statement in the <code>for</code> loop.</p>
  
  <p>There also seems to be a problem with the three lines after the <code>for</code> loop, since when the program gets down there, it just crashes and fails to add any values to the list, but I figured I'd just take it one step at a time.</p>
  
  <p>There's probably a lot here I'm misunderstanding, so any help at all would be appreciated.</p>