title: how to add value to every second node in the list
body: <p><strong>I try to traverse a linked list of values, and add some given value to every other node in the list. It should always start adding to the first node in the list, and then add on to every other node from there.</strong></p>
  <p>Error is the program not giving any output but it most likely something wrong with the function</p>
  <p>Here is what I tried:</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  // Declare the Node structure to be used for the linked list.
  // Each node should have an int for data, and
  // a pointer to the next node.
  struct Node {
     int data;
     Node *pNext;
  };
  
  // Displays the list number values
  void display( Node *pTemp){
      while( pTemp != NULL) {
          cout &lt;&lt; pTemp-&gt;data &lt;&lt; &quot;-&gt;&quot;;
          pTemp = pTemp-&gt;pNext;
      }
      cout &lt;&lt; endl;
  }
  
  // Create a new node, put the userInput number in it, and APPEND
  // it to the END of the list.
  void append(Node* &amp;pHead, int userInput) {
      Node *pTemp = new Node;
      pTemp-&gt;data = userInput;
      pTemp-&gt;pNext = NULL;
  
      Node *pLast = pHead;
      // Find the last node on the list
      while(pLast != NULL &amp;&amp; pLast-&gt;pNext != NULL) {
          pLast = pLast-&gt;pNext;
      }
  
      if( pLast == NULL) {
          // Make this new node the first node on an otherwise empty list
          pHead = pTemp;
      }
      else {
          // Append this new node to the end of the existing list
          pLast-&gt;pNext = pTemp;
      }
  }
  
  //-----------------------------------------------------------------------
  //       *** Do not change anything above this point. ***
  //------------------------------------------------------------------------ 
  
  // add valueToAdd to every second node in the list
  void addEveryOther(Node* &amp;pHead, int valueToAdd) {
      
      // while there is a node after pTemp-&gt;pNext
      while(pHead-&gt;pNext != NULL || pHead != NULL) {
          // add valueToAdd to pTemp-&gt;data
          pHead-&gt;data += valueToAdd;
          // move pTemp to the next node
          pHead = pHead-&gt;pNext-&gt;pNext;
      }
  }
  
  //-----------------------------------------------------------------------
  //       *** Do not change anything below this point. ***
  //------------------------------------------------------------------------
  
  int main() {
     int userInput;
     Node *pHead = NULL;  // pointer to the head of the list
     int value;
     
     // Create the linked list
     cout &lt;&lt; &quot;Enter list numbers separated by space, followed by -1: &quot;;
     cin &gt;&gt; userInput;
  
     // Keep looping and appending nodes on the list until 
     // end-of-input flag of -1 is given
     while(userInput != -1) {
          // Store this number on the list
          append(pHead, userInput);
          cin &gt;&gt; userInput;
      }
  
      cout &lt;&lt; &quot;Enter value to add on: &quot;;
      cin &gt;&gt; value;
      addEveryOther(pHead, value);
      display(pHead);
  
      return 0;
  
  }// end main()
  </code></pre>
  <p>Here is the input :</p>
  <pre><code>2 8 9 5 11 3 6 -1
  Enter value to add on: 5
  </code></pre>
  <p>and expected output :  </p>
  <pre><code>7-&gt;8-&gt;14-&gt;5-&gt;16-&gt;3-&gt;11
  </code></pre>