title: Creating a linked list using a while loop
body: <p>I need to create a linked list of a class member (Binary), and I'm running into an infinite loop problem. The Binary class only contains the degree (int) and a next node pointer. 
  Within the Binary Class, the implementation for the Binary linked list is being performed inside the set_bit method. The set_bit method takes in two ints, and they are either a 1/0 int (bit) and a degree int. The bit is not needed at this point though. </p>
  
  <p>The set_bit method is below: </p>
  
  <pre><code>void Binary::set_bit(int b, int d){
      BinaryNode* current = firstTerm;
      BinaryNode* toSet;
  
      if (current == NULL) {
  
          firstTerm = new BinaryNode(d, NULL);
          current = firstTerm;
  
          cout &lt;&lt; "\nd: " &lt;&lt; d &lt;&lt; " &lt;--&gt; current degree: " &lt;&lt; current-&gt;degree &lt;&lt; endl;
          system("pause");
  
      } else {
          while (current != NULL){
              firstTerm = new BinaryNode(d, current);
              cout &lt;&lt; "\nd: " &lt;&lt; d &lt;&lt; " &lt;--&gt; current degree: " &lt;&lt; current-&gt;degree &lt;&lt; endl;
              cout &lt;&lt; "first term: " &lt;&lt; firstTerm-&gt;degree &lt;&lt; endl;
              system("pause");
          }
      }
  }
  </code></pre>
  
  <p>on the main.cpp file, I am trying to set the following bits: </p>
  
  <pre><code>b1.set_bit(1, 2);
  b1.set_bit(1, 5);
  b1.set_bit(1, 0);
  b1.set_bit(0, 2);
  </code></pre>
  
  <p>The method is setting the first bit (2), and gets to the next (5), then begins an infinite loop trying to set the bit. 
  Where am I going wrong on this? 
  I've asked my lab instructor for help and he provided me the following code: 
  Why did the code that the lab instructor not work either? </p>
  
  <pre><code>void Binary ::set_bit( int b , int d ){
    BinaryNode * current = firstTerm;
    if (current == NULL ){
      firstTerm = new BinaryNode(d,NULL); // Corrected Line
    }
    while (current != NULL ){
      firstTerm = new BinaryNode(d,firstTerm); // Corrected Line 
    }
  }
  </code></pre>
  
  <p>Thanks</p>