title: Back Traversal issue in linked list
body: <p>following is C++ written linked list with Node class and main function. List is traversing forward using &quot;next()&quot; function but producing execution time error when traverse back using &quot;back()&quot;.</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Node {
      public:
          int object;
          Node *nextNode;
          Node *prevNode;
          
      public:
          
          int get(){
              return object;
          }
          
          void set(int object){
              this-&gt;object = object;
          }
          
          Node* getNext(){
              return nextNode;
          }
          
          void setNext(Node *nextNode){
              this-&gt;nextNode = nextNode;
          }
          
          Node* getPrev(){
              return prevNode;
          }
          
          void setPrev(Node *prevNode){
              this-&gt;prevNode = prevNode;
          }
          
      
  };
  
  
  class List {
      public:
          Node* headNode;
          Node* currentNode;
          int size;
      
      public:
          
          List(){
              headNode = new Node();
              headNode-&gt;setNext(NULL);
              headNode-&gt;setPrev(NULL);
              currentNode = NULL;
              int size = 0;   
          }
          
          void add(int addObject){
              Node* newNode = new Node();
              newNode-&gt;set(addObject);
              
              if(currentNode != NULL){
                  newNode-&gt;setNext(currentNode-&gt;getNext());
                  newNode-&gt;setPrev(currentNode);
                  currentNode-&gt;setNext(newNode);
                  currentNode = newNode;
                              
              }
              else {
                  newNode-&gt;setNext(NULL);
                  newNode-&gt;setPrev(headNode);
                  headNode-&gt;setNext(newNode);
                  currentNode = newNode;
              }
              
              size++;
      
          }
          
          int get(){
              if(currentNode != NULL) {
                  return currentNode-&gt;get();
              }
          } 
          
          bool next(){
              if(currentNode == NULL) return false;
              
              currentNode = currentNode-&gt;getNext();
              
              if(currentNode == NULL) return false;
              else                    return true;
          
          }
          
          bool back(){
              if(currentNode == NULL) return false;
              currentNode = currentNode-&gt;getPrev();
              
              if(currentNode == NULL) return false;
              else return true;
          }
          
          void start(){
              currentNode = headNode;
          }
          
          void remove() {
              if (currentNode != NULL &amp;&amp; currentNode != headNode){
                  delete currentNode;
                  size--;
              }
          }
          
          int length() {
              return size;
          }
          
  };
  
  
  int main(){
      
      List list;
      
      list.add(5);
      list.add(13); 
      list.add(4);
      list.add(8);
      list.add(48);
      list.add(12); 
      
      list.start(); 
      
      while(list.next()){
          cout&lt;&lt;endl&lt;&lt;&quot;Element: &quot; &lt;&lt; list.get() &lt;&lt; endl;
      }
       
      cout&lt;&lt;endl&lt;&lt;&quot;BACK&quot;&lt;&lt;endl;
  
      while(list.back()){
          cout&lt;&lt;endl&lt;&lt;&quot;Element: &quot; &lt;&lt; list.get() &lt;&lt; endl;
      } 
  }
  </code></pre>
  <p>Back() function should traverse the list in opposite direction (from end to start).. reverse manner. Sometime this code hangs the CPU and sometimes it only runs next() function, and for back() function it remains silent without doing anything.</p>