title: doubly-circular linked list in c#
body: <p>I am using a doubly-circular linked list. After adding values I am retrieving values from the list. I am using this code to add values:</p>
  
  <pre><code>public void Add(T data)
  {
      if (root == null)
      {
          this.Add(data);
          root = new LinkedListNode&lt;T&gt;(data);
          last = root;
      }
      else
      {
          last.Next = new LinkedListNode&lt;T&gt;(data);
          last.Next.Previous = last;
          last = last.Next;
      }
  }
  </code></pre>
  
  <p>I am giving the data as:</p>
  
  <pre><code>char[] dir = { 'n', 'w', 's', 'e' };
  TestProject.classes.LinkedList&lt;char&gt; list = new TestProject.classes.LinkedList&lt;char&gt;();
  
  foreach (char c in dir)
  {
      list.Add(c);
  }
  </code></pre>
  
  <p>I am retrieving the list data as:</p>
  
  <pre><code>public LinkedListNode&lt;T&gt; GetAt(int index)
  {
      var current = root;
      for (int i = 0; i &lt; index; i++)
      {
          if (current == null)
              return null;
          current = current.Next;
      }
      return current;
  }
  </code></pre>
  
  <p>The problem is it is giving the value of <code>current</code> as <code>null</code> while retrieving the node value.</p>
  
  <p>The list is an example from stackoverflow itself.</p>
  
  <p>Please help me..</p>
  
  <p>My whole linked list class is as:</p>
  
  <pre><code>public class LinkedList&lt;T&gt;
  {
      protected LinkedListNode&lt;T&gt; root = null;
      protected LinkedListNode&lt;T&gt; last = null;
      public LinkedList()
      {
      }
  
      public string ToString()
      {
          StringBuilder sb = new StringBuilder();
          var node = root;
          while (node != null)
          {
              sb.Append("{ " + node.Data.ToString() + " } ");
              node = node.Next;
          }
          return sb.ToString();
      }
  
      public T this[int index]
      {
          get
          {
              var node = GetAt(index);
              if (node == null)
                  throw new ArgumentOutOfRangeException();
              return node.Data;
          }
          set
          {
              var node = GetAt(index);
              if (node == null)
                  throw new ArgumentOutOfRangeException();
              node.Data = value;
          }
      }
  
      public LinkedListNode&lt;T&gt; GetAt(int index)
      {
          var current = root;
          for (int i = 0; i &lt; index; i++)
          {
              if (current == null)
                  return null;
              current = current.Next;
          }
          return current;
      }
  
      public void Add(T data)
      {
          if (root == null)
          {
              this.Add(data);
              root = new LinkedListNode&lt;T&gt;(data);
              last = root;
          }
          else
          {
              last.Next = new LinkedListNode&lt;T&gt;(data);
              last.Next.Previous = last;
              last = last.Next;
          }
      }
  }
  
  public class LinkedListNode&lt;T&gt;
  {
      public T Data { get; set; }
  
      public LinkedListNode(T data)
      {
          Data = data;
      }
  
      public LinkedListNode&lt;T&gt; Next { get; set; }
      public LinkedListNode&lt;T&gt; Previous { get; set; }
  }
  </code></pre>