title: Inserting Into a Generic Linked List
body: <p>I'm having an issue inserting a new object into a linked list. I use a custom method called Insert that takes the object type and the index where it should be placed. However, when I insert the object, the list doesn't update. Any idea why?</p>
  
  <p>The list:</p>
  
  <pre><code>        GenericLinkedList&lt;string&gt; bet = new GenericLinkedList&lt;string&gt;();
          bet.Add("a");
          bet.Add("b");
          bet.Add("c");
          bet.Add("d");
  
          //bet.Remove();
          bet.Insert("x", 2);
  </code></pre>
  
  <p>The Generic Linked List Class:</p>
  
  <pre><code>public class GenericLinkedList&lt;T&gt; where T : IComparable&lt;T&gt; 
  {
  
      public GenericLinkedList()
      {
          count = 0;
          head = null;
      }
  
      #region Nested Node Class
      private class Node&lt;T&gt;
      {
          private T data;
          public T Data
          {
              get { return data; }
              set { data = value; }
          }
  
          private Node&lt;T&gt; next;
          public Node&lt;T&gt; Next
          {
              get { return next; }
              set { next = value; }
          }
  
          public Node&lt;T&gt; Previous { get; set; }
  
  
          public Node(T obj_t)
          {
              next = null;
              data = obj_t;
          }
          public Node()
          {
              next = null;
          }
  
  
      }
      #endregion
  
      private Node&lt;T&gt; head;
      private Node&lt;T&gt; tail;
  
  
      private int count;
      public int Count
      {
          get
          {
              int num;
              if (count &lt; 0)
                  num = 0;
              else {
                  num = count;
              }
              return num;
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>The Insert Method:</p>
  
  <pre><code>    public void Insert(T data, int index)
      {
          Node&lt;T&gt; replacedItem = new Node&lt;T&gt;();
          Node&lt;T&gt; newItem = new Node&lt;T&gt;(data);
  
          if (head == null)
          {
              head = new Node&lt;T&gt;(data);
              tail = head;
          }
          else
          {
              Node&lt;T&gt; current = head;
              Node&lt;T&gt; tempNode = new Node&lt;T&gt;();
  
  
              if (index &gt; 0 &amp;&amp; index &lt;= count + 1)
              {
                  int c = 0;
                  while (current != null)
                  {
                      if (c == index)
                      {
                          tempNode = current;
                          current = newItem;
                          current.Next = tempNode;
                      }
                      else
                      {
                          current = current.Next;
                      }
                      c++;
                  }
              }
              count++; 
          }
      }
  </code></pre>
  
  <p>The Add Method: </p>
  
  <pre><code>  public void Add(T data)
      {
          count++;
  
          if (head == null)
          {
              head = new Node&lt;T&gt;(data);
              tail = head;
          }
          else
          {
              tail.Next = new Node&lt;T&gt;(data);
              tail.Next.Previous = tail;
              tail = tail.Next;
          }
      }
  </code></pre>