title: Create function that returns the value of a linked list in c++
body: <p>If someone can help me figure out what is wrong with this code I'll be really grateful. I'm trying to make a function <code>getIth(int n)</code> that returns the value of <code>node n</code> in the list, but it is not working and I don't know why.</p>
  
  <p>node.h<br>
      <code>template&lt;typename T&gt;</code></p>
  
  <pre><code>class Node
  {
      public:
  
          T nValue;
          Node&lt;T&gt; *next;
          Node&lt;T&gt; *before;
  
          Node(T a)
          {
              nValue = a;
          }                                                                                                                
  };
  </code></pre>
  
  <p>linked_list.h</p>
  
  <pre><code>#include "node.h"                                                                                                                                                                                           
  
  #ifndef NULL
  #define NULL = 0
  #endif
  
  template &lt;typename T&gt;
  
  class LinkedList
  {
      private:
  
         Node&lt;T&gt; *head;
         Node&lt;T&gt; *last;
  
      public:
          LinkedList()
          {
              T value = -1;
              head = new Node&lt;T&gt;(value);
              head-&gt;next = NULL;
  
              last = new Node&lt;T&gt;(value);
              last-&gt;before = NULL;
          }
  
          T getIth(int n)
          {
              int contador = 0;
              T value = -1;
              Node&lt;T&gt; *tempNode = new Node&lt;T&gt;(value);
              tempNode = last;
  
              while(tempNode-&gt;next != NULL)
              {
                  contador = contador + 1;
  
                  if(contador == n)
                  {
                      value = tempNode-&gt;nValue;
                  }
  
                  tempNode-&gt;next = tempNode;
              }
  
              return value; 
         }
  }  
  </code></pre>