title: C++ Linked List Node with class
body: <p>Specifically, the goal here is to create a linked structure that has some number of nodes, between 5 and 2 million. Don’t worry that this number is large or that values may wrap around past the max size of integer. If you have created your linked structure correctly, a modern computer can breeze through this code very quickly. Notice that the comments describe exactly how this main should work. Here are the highlights:</p>
  
  <p>Create three loops
  The first loop creates the linked structure, hooking together the “next” fields of each node and giving each node an integer value between 0 and the randomly chosen size.
  The second loop adds up all of the nodes and counts them. Counting the nodes in this case should be used only as check to make sure you are not missing one.
  The third loop traverses all nodes again, this time deleting them.</p>
  
  <p>Node.h</p>
  
  <pre><code>class Node {
  public:
      Node();
      Node(const Node&amp; orig);
      virtual ~Node();
      bool hasNext();
      Node* getNext();
      void setNext(Node* newNext);
      int getValue();
      void setValue(int val);
  private:
      Node* next;
      int value;
  };
  
  #endif
  </code></pre>
  
  <p>Node.cpp</p>
  
  <pre><code>include "Node.h"
  include &lt;iostream&gt;
  
  Node::Node() {
      next = NULL;
  }
  
  Node::Node(const Node&amp; orig) {
      next = orig.next;
      value = orig.value;
  }
  
  Node::~Node() {
  
  }
  
  bool Node::hasNext(){
      if (next != NULL)
          return true;
      else
          return false;
  }
  
  Node* Node::getNext(){
      return next;
  }
  
  void Node::setNext(Node* newNext){
      if(newNext == NULL)
          next = NULL;
      else
          next = newNext-&gt;next;
  }
  
  int Node::getValue(){
      return value;
  }
  
  void Node::setValue(int val){
      value = val;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>include &lt;cstdlib&gt;
  include &lt;iostream&gt;
  include "Node.h"
  include &lt;time.h&gt;
  
  using namespace std;
  
  int main(int argc, char** argv) {
      //This is the node that starts it all
      Node *tail;
      Node* head = new Node();
  
      //select a random number between 5 and 2,000,000
      srand(time(NULL));
      int size = (rand() % 2000000) + 5;
      int total = 0;
      int counter = 0;
      //print out the size of the list that will be created/destroyed
      cout &lt;&lt; "The total size is: " &lt;&lt; size &lt;&lt; endl;
  
      head-&gt;setValue(0);
      tail = head;
      Node *newNode = new Node;
      for (int i = 1; i &lt; size; i++){  
          Node *newNode = new Node;
          newNode-&gt;setValue(i);
          newNode-&gt;setNext(NULL);
          tail-&gt;setNext(newNode);
          tail = newNode;
      }
  
      //Create a list that counts from 0 to 2,000,000
      //Link all of the nodes together
      //A for loop is easiest here
      cout &lt;&lt; head-&gt;getNext()-&gt;getValue();
  
      Node* current = head;
      while (current != NULL){ 
          counter += current-&gt;getValue();
          cout &lt;&lt; current-&gt;getValue();
          current = current-&gt;getNext();
          total++;
      }
  
      //Traverse the list you created and add up all of the values
      //Use a while loop
  
      //output the number of nodes. In addition, print out the sum
      //of all of the values of the nodes.
      cout &lt;&lt; "Tracked " &lt;&lt; total &lt;&lt; " nodes, with a total count of " &lt;&lt; counter &lt;&lt; endl;
  
      //Now loop through your linked structure a third time and
      //delete all of the nodes
      //Again, I require you use a while loop
  
      cout &lt;&lt; "Deleted " &lt;&lt; total &lt;&lt; " nodes. We're done!" &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>It is printing out the total size then...
  I am getting a Seg fault:11.
  I am also missing some parts in the main, I am confused on how to write these as well.</p>