简体   繁体   中英

NullReferenceException when trying to add item to end of singly linked list

I am trying to add an node to the end of singly linked list. But I keep getting NullReferenceException. I have tried everything but I can't get it to work. The idea behind the code is to keep looping through the list until we reach null(which marks the end of the linked list) then we add a new node to the end. So the question is why am I getting this and how can I get it to work?

Below you will find the code.

using System;
using System.Diagnostics;
using System.Threading;

namespace LinkedList
{
  public class Node<T> where T : IComparable
  {
    public T Value;
    public Node<T> Next { get; set; }

    public Node(T value, Node<T> next)
    {
      this.Value = value;
      this.Next = next;
    }
  }

  public class SortedLinkedList<T> where T : IComparable
  {
    public Node<T> start;

    public SortedLinkedList()
    {
      start = null;
    }

    public SortedLinkedList(Node<T> node)
    {
      start = node;
    }

    public void Insert(T value)
    {
      if ( this.start == null )
      {
        this.start = new Node<T>(value, this.start);
      }

      Node<T> curr = this.start;
      while ( curr != null )
      { curr = curr.Next;}
     curr.Next = new Node<T>(value,curr.Next);
    }
  }

  public class Program
  {
    public static void Main(string[] args)
    {
      var list =
        new SortedLinkedList<int>(
          new Node<int>(
            5, new Node<int>(
              7, new Node<int>(
                21, new Node<int>(
                  30, null)
                )
              )
            )
        );
      list.Insert(12);
      var list2 = new SortedLinkedList<string>();
      list2.Insert("hello");
    }
  }
}

You have to rewrite your while loop and check curr.Next property for null value, otherwise you'll get a null as curr value after loop finishes.

Node<T> curr = start;
while (curr.Next != null)
{ 
    curr = curr.Next; 
}
curr.Next = new Node<T>(value, curr.Next);

Also, you insert the node at the end of list without comparison with existing nodes. It means, that your list isn't sorted despite SortedLinkedList class name.

To have a list sorted you should compare the values one by one in while loop to find a correct place for value, or insert it to the end, if place is not found. Something like that

var comparer = Comparer<T>.Default;
Node<T> curr = start;
var inserted = false;
while (curr.Next != null)
{
    if (comparer.Compare(curr.Value, value) < 0 && 
        comparer.Compare(curr.Next.Value, value) >= 0)
    {
        var previous = curr.Next;
        curr.Next = new Node<T>(value, previous);
        inserted = true;
        break;
    }
    curr = curr.Next;
}

if (!inserted)
    curr.Next = new Node<T>(value, curr.Next);

Look at this fragment of code:

  while ( curr != null )
  { curr = curr.Next;}

Here loop ends when curr is null , so next line you get null reference on curr.Next . Try following:

while (curr.Next != null)
{
    curr = curr.Next;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM