title: How to add nodes to a single linked list in c#
body: <p>I have started learning about algorithms in C#. I have been trying to create a linked list algorithm by myself (just for fun). I am having one issue however. I am wondering, how can I add nodes to the list? Right now I have three methods that interact with the nodes, but the list is empty so nothing happens. </p>
  
  <p><strong>Node Class</strong></p>
  
  <pre><code>class Node {
      public int info;
      public Node link;
  
      public Node (int i)
      {
          info = i;
          link = null;
      }
  }
  </code></pre>
  
  <p><strong>SingleLinkedList Class</strong></p>
  
  <pre><code>class SingleLinkedList {
      private Node start;
  
      public SingleLinkedList ()
      {
          start = null;
      }
  
      public void CreateList() {
          //TODO: Create the linked list here
      }
  
      public void DisplayList() {
          Node p;
          if (start == null) {
              Console.WriteLine ("Your list is empty, idiot");
              return;
          }
          Console.WriteLine ("List is :  ");
          p = start;
          while (p != null) {
              Console.WriteLine (p.info + " ");
              p = p.link;
          }
          Console.WriteLine ();
      }
  
      public void CountNodes() {
          int n = 0;
          Node p = start;
          while (p != null) {
              n++;
              p = p.link;
          }
          Console.WriteLine ("Number of nodes in list is = " + n);
      }
  
      public bool Search (int x) {
          int position = 1;
          Node p = start;
          while (p != null) {
              if (p.info == x)
                  break;
              position++;
              p = p.link;
          }
          if (p == null) {
              Console.WriteLine (x + "not found in list because use an idiot");
          } else {
              Console.WriteLine (x + "is at position " + position);
              return true;
          }
          return false;
      }
  }
  </code></pre>
  
  <p><strong>Main</strong></p>
  
  <pre><code>int choice, data;
  
  SingleLinkedList list = new SingleLinkedList ();
  
  Console.WriteLine ("1.Display List");
  Console.WriteLine ("2.Count Nodes");
  Console.WriteLine ("search for an integer");
  
  Console.WriteLine ("Enter your choice master: ");
  choice = Convert.ToInt32 (Console.ReadLine ());
  
  switch (choice) {
  
  case 1:
      list.DisplayList ();
      break;
  
  case 2:
      list.CountNodes ();
      break;
  
  case 3:
      Console.WriteLine ("Enter the element to be searched");
      data = Convert.ToInt32 (Console.ReadLine ());
      list.Search (data);
      break;
  
  default:
      break;
  }
  </code></pre>
  
  <p>How do I implement the CreateList method in the SingleLinkedList class to add nodes to the list?</p>