title: C++ Add node to beginning of linked list fails after 1 time. Trouble with other methods too
body: <p>So I have a linked list program semi working. I am just having some trouble with certain methods.... The ones I have documented bekiw are working except for delete from end which is acting weird.</p>
  
  <p>I am using NetBeans on Mavericks with G++ as my compiler and C++11
  <a href="https://www.dropbox.com/s/u39gfqa1wtb6hhm/linker.zip" rel="nofollow">Here is a zip of all of the program files</a></p>
  
  <p>Here is a list of the methods I am trying to make:</p>
  
  <pre><code>//working
  int size() const;
  /kind of 
  void addToStart(Node *);
  //working
  void addToEnd(Node *);
  //working
  void printList();
  //working
  bool removeFromStart();
  //kind of working
  bool removeFromEnd();
  
  //Still working on these
  void removeNodeFromList(int);
  void removeNodeFromList(string);
  </code></pre>
  
  <p>For now, I have to run removeFromEnd() twice in order for it to work. Meaning, I run it once at the beginning of the program and it does nothing, but every subsequent time, it actually does the deleting.</p>
  
  <p>For addToStart() it works if I only run it once. I.E:</p>
  
  <ul>
  <li>I can run it once at the beginning of the program and print out the list</li>
  <li>I can run it once AFTER using addToEnd, but if I try it a second time, and I try to print out the list, it just keeps spitting out the value I tried to add.</li>
  </ul>
  
  <p>addToEnd() works perfectly find if I just keep running that, but it fails if I:</p>
  
  <p>Start out by using addToEnd() to add items, then use addToStart() ONCE and THEN try to use addToEnd() again. When I print out the list, it only prints out two objects and each of those is a copy of the last value I tried to insert.</p>
  
  <pre><code>void LinkedList::addToEnd(Node* ne) 
  {
      Node** q = &amp;myHead;
      while (*q) 
      {
          q = &amp;(*q)-&gt;next;
      }
  
      *q = new Node(ne-&gt;itemName, ne-&gt;itemNo);
  }
  
  void LinkedList::printList() 
  {
      Node* p = myHead;
      while (p != NULL) 
      {
          cout &lt;&lt; p-&gt;itemNo &lt;&lt; " " &lt;&lt; p-&gt;itemName;
          cout &lt;&lt; endl;
          p = p-&gt;next;
      }
  
      cout &lt;&lt; endl &lt;&lt; endl;
  }
  
  bool LinkedList::removeFromStart() 
  {
      if (myHead == NULL) 
      {
          cout &lt;&lt; "List is already empty";
      }
      else 
      {
          myHead = myHead-&gt;next;
      }
  }
  
  bool LinkedList::removeFromEnd() 
  {
      if (myHead == NULL) 
          return false;
  
      //Empty the list if there's only one element
      if (myHead-&gt;next == NULL) 
      {
          delete myHead;
          myHead = NULL;
          myTail = NULL;
          return true;
      }
  
      // Find the last item in the list
      Node *temp = myHead;
      while (temp-&gt;next != myTail) 
      {
          temp = temp-&gt;next;
      }
  
      delete myTail;
      temp-&gt;next = NULL;
      myTail = temp;
      return true;
  }
  </code></pre>
  
  <p>Also, still trying to figure out the remove ones</p>
  
  <pre><code>void LinkedList::removeNodeFromList(int i) {
  
  
  //Save the values
  Node* p = myHead;
  Node* temp = myHead-&gt;next;
  
  
      while (p) {
  
          if (p-&gt;itemNo == i) {
  
              p=temp;
          } else {
              p = p-&gt;next;
          }
  
      }
  
  }
  </code></pre>