简体   繁体   English

尝试将项目添加到单链表末尾时出现 NullReferenceException

[英]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.但我不断收到 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.代码背后的想法是不断循环列表直到我们到达 null(标记链表的末尾),然后我们在末尾添加一个新节点。 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.您必须重写while循环并检查curr.Next属性是否为null值,否则在循环完成后您将获得null作为curr值。

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.这意味着,尽管有SortedLinkedList类名,但您的列表并未排序。

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.要对列表进行排序,您应该在while循环中逐个比较值以找到值的正确位置,或者如果找不到位置,则将其插入到末尾。 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 .currnull ,这里循环结束,因此下一行您将在curr.Next上获得 null 引用。 Try following:尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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