title: Simple List Merge C# (Without Generics)
body: <p>I have created a simple list class from scratch. This is for a class assignment that I have been working on for about a week - very new to lists. We can not use generics  so trying to research my question below has not been fruitful. Although I did get to watch 7 tutorials on youtube by BetterCoder and I found some stuff in my book but nothing with an example of "merging".</p>
  
  <p>I have three classes - my node, my list, and my program. In my list class, I am working on building a Merge() method which eventually will compare the data in the two lists and merge them into an ordered list. </p>
  
  <p>Right now for some reason my Merge method - which is very basic to help me understand what is happening - is not working correctly. It has both lists passed to it, and is adding the data from listTwo to listOne BUT for some reason when it's printing to the console the second Node's Data shows twice :</p>
  
  <p>EX: 1 -> 2 -> 2</p>
  
  <p>instead of printing the head (1), the next (2) and then the next (3) which it should be.</p>
  
  <p>EX: 1 -> 2 -> 3</p>
  
  <p>In the program class I have proven with a write line that (listOne.firstNode.Next.Next.Data) = 3 . Which it should be.</p>
  
  <p>Can someone help me figure out if the nodes in list one aren't pointing to each other correctly or whatever is going on?</p>
  
  <ul>
  <li><p>My Merge Method must be passed both list objects (listOne and listTwo) and eventually I need to make those passed as references but I haven't figured that out quite yet and will focus on that later I suppose.  </p>
  
  <pre><code>    namespace LinkedList
          {
    //This is my Node Class
              class Node
              {
                  public object Data { get; set; }
  
                  public Node Next { get; set; }
  
                  public Node(object dataValue) : this(dataValue, null) { }
  
                  public Node(object dataValue, Node nextNode)
                  {
                      Data = dataValue;
                      Next = nextNode;
                  }
              }
  
  
  
          //This is my List Class
  
              class List
              {
                  public Node firstNode;
                  public int count;
  
                  public List()
                  {
                      firstNode = null;
                  }
  
                  public bool Empty
                  {
                      get { return this.count == 0; }
                  }
  
                  public int Count
                  {
                      get { return this.count; }
                  }
  
                  public object Add(int index, object o)
                  {
                      if (index &lt; 0)
                          throw new ArgumentOutOfRangeException("Index: " + index);
  
                      if (index &gt; count)
                          index = count;
  
                      Node current = this.firstNode;
  
                      if (this.Empty || index == 0)
                      {
                          this.firstNode = new Node(o, this.firstNode);
                      }
                      else
                      {
                          for (int i = 0; i &lt; index - 1; i++)
                              current = current.Next;
  
                          current.Next = new Node(o, current.Next);
                      }
  
                      count++;
  
                      return o;
                  }
  
                  public object Add(object o)
                  {
                      return this.Add(count, o);
                  }
  
                  public object Merge(List a, List b)
                  {
                      a.Add(b.firstNode.Data);
  
                      return a;
                  }
  
                  public void Print()
                  {
                      while (this.count &gt; 0)
                      {
                          Console.Write(firstNode.Data + "-&gt;");
  
                          if(firstNode.Next != null)
                          firstNode.Data = firstNode.Next.Data;
  
                          count--;
                      }
  
                  }
  
              }
  
  
          //And here is my Program
  
              class Program
              {
  
                  static void Main(string[] args)
                  {
                      List listOne = new List();
  
                      List listTwo = new List();
  
                      listOne.Add(1);
                      listOne.Add(2);
  
                      listTwo.Add(3);
  
  
                      listTwo.Print();
  
                      Console.WriteLine("");
  
  
                      listOne.Merge(listOne, listTwo);
  
                      Console.WriteLine("");
  
                      listOne.Print();
  
                      //This line below shows that the data "3" from listTwo is being added to listOne in the list Merge Method
        //Console.WriteLine(listOne.firstNode.Next.Next.Data);
  
                      Console.ReadKey();
                  }
              }
     }
  </code></pre></li>
  </ul>