Please tell me what the error is. I am trying to use the recursive method on a self-made single-linked list, but I get the error System.NullReferenceException: The object reference does not indicate an object instance.

class Node { public Node() { } public Node(Node PreviousNode) { next = PreviousNode.Next; Element = PreviousNode.Element; } public void SetNextNode(Node _nextNode) { next = _nextNode; } public int Element { get { return element; } set { element = value; } } public Node Next { get { return next; } } private Node next; private int element; } class List { public List() { // создание пустого списка headNode = null; tailNode = headNode; Length = 0; } public void Push(int _element) { if (headNode == null) { // создать узел, сделать его головным headNode = new Node(); headNode.Element = _element; // этот же узел и является хвостовым tailNode = headNode; // следующего узла нет headNode.SetNextNode(null); } else { // создать временный узел Node newNode = new Node(); // следующий за предыдущим хвостовым узлом - это наш временный новый узел tailNode.SetNextNode(newNode); // сделать его же новым хвостовым tailNode = newNode; tailNode.Element = _element; // следующего узла пока нет tailNode.SetNextNode(null); } ++Length; } public int this[int _position] { get { Node tempNode = headNode; for (int i = 1; i <= _position; ++i) // переходим к следующему узлу списка tempNode = tempNode.Next; return tempNode.Element; } } public int Length { get; private set; } private Node headNode; private Node tailNode; } 

This code does not work:

 public void Insert(int element, int place) { if (place == 0) { PushFirst(element); } else if (place == 1) { Node newNode = new Node(); newNode.Element = element; newNode.SetNextNode(headNode.Next); headNode.SetNextNode(newNode); ++Length; } else if(place == 2) { Node newNode = new Node(); newNode.Element = element; newNode.SetNextNode(headNode.Next.Next); headNode.Next.SetNextNode(newNode); ++Length; } else { Node tempNode = headNode.Next.Next; Node preNode = headNode.Next; headNode.SetNextNode(preNode); preNode.SetNextNode(tempNode); RecursionInsert(tempNode, preNode, element, place); } } private void RecursionInsert(Node tempNode,Node prenode, int element, int place) { if (place == 0) { Node newNode = new Node(); newNode.Element = element; newNode.SetNextNode(tempNode); prenode.SetNextNode(newNode); } else { prenode.SetNextNode(tempNode); tempNode = tempNode.Next; RecursionInsert(tempNode,prenode, element, place--); } } 

But while this method works great:

  public void Remove(int element) { Node preNode = new Node(); Node tempNode = headNode; while ( true ) { if(tempNode.Element == element) { preNode.SetNextNode(tempNode.Next); break; } preNode = tempNode; tempNode = tempNode.Next; } Length--; } 
  • one
    error on which line appears? - codename0082016
  • the error appears in the lines: RecursionInsert (tempNode, preNode, element, place); tempNode = tempNode.Next; RecursionInsert (tempNode, prenode, element, place--); - Julius Focal
  • Try before using tempNode Add Node tempNode = new Node(); - codename0082016
  • The error remained ... - Julius Wittal
  • @ YuliVedkal take a look [here] ( ru.stackoverflow.com/questions/413041/… ) - Ethernets

1 answer 1

That's how it should be ...

  private void RecursionInsert(Node temp, Node prenode, int element, int place) { if (place != 0) { prenode = temp; temp = temp.Next; prenode.SetNextNode(temp); place--; RecursionInsert(temp,prenode, element, place); } else { Node newNode = new Node(); newNode.Element = element; newNode.SetNextNode(temp); prenode.SetNextNode(newNode); } 
  • So what was the mistake? How does this piece of code differ from what was in question? - Grundy
  • @Grundy nothing, he just decided that the author of the topic is lame logic and instead of (place == 0) put (place != 0) brilliant. - Ethernets
  • one
    @Ethernets, note that the answer was given, the author of the question is Grundy
  • @Grundy inattention, this also applies to me)) happens ... - Ethernets