Salman Karim
Salman Karim

Reputation: 11

Insertion At last Node Single Linked List

i am working on linked list.. i successfully inserted and deleted a node at first node.. but when i try to insert node at last .. it gives an error "Object reference not set to an instance of an object"

My logic is correct but visual studio is generating an exception dont know why please help me out..

Full code is given below

class MyList
{
    private Node first;
    private Node current;
    private Node previous;

    public MyList()
    {
        first = null;
        current = null;
        previous = null;
    }

    public void InsertLast(int data)
    {
        Node newNode = new Node(data);

        current = first;

        while (current != null)
        {
            previous = current;
            current = current.next;
        }

        previous.next = newNode;
        newNode.next = null;
    }

    public void displayList()
    {
        Console.WriteLine("List (First --> Last): ");
        Node current = first;
        while (current != null)
        {
            current.DisplayNode();
            current = current.next;
        }
        Console.WriteLine(" ");
    }
}



class Node
{
    public int info;
    public Node next;

    public Node(int a)
    {
        info = a;
    }

    public void DisplayNode()
    {
        Console.WriteLine(info);
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyList newList = new MyList();

        newList.InsertLast(10);
        newList.InsertLast(20);
        newList.InsertLast(30);
        newList.InsertLast(40);

        newList.displayList();

        Console.ReadLine();
    }
}

Upvotes: 0

Views: 1763

Answers (2)

Justin
Justin

Reputation: 21

Basically you will have to deal with the case of an empty list. In your current code when the list is empty you have previous, current, and first all equal to null. So you're getting an error because you're attempting to assign previous.next() a value when previous equals null.

Upvotes: 2

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

If the list is empty, first will be equal to null and your code will raise exception when you try to do this previous.next = newNode; because the previous is also null. when adding first node, you will have to add the new element as first, so rewrite the code like this:

public void InsertLast(int data)
{
      Node newNode = new Node(data);
      if (first == null)
      {
           first = newNode;
      }
      else
      {
           current = first;

           while (current != null)
           {
                previous = current;
                current = current.next;
           }
           previous.next = newNode;
      }
      newNode.next = null;
}

EDIT: You can implement it something like this but take care if the fist is null and the case when there is only first node. Check the method:

public void RemoveLast()
{            
      if (first != null)//there is no point in removing since the list is empty
      {
           if (first.next == null) //situation where list contains only one node
                first = null;
           else //all other situations
           {
                current = first;

                while (current.next != null)
                {
                    previous = current;
                    current = current.next;
                }
                previous.next = null;
           }
      }
}

Upvotes: 0

Related Questions