Faizan
Faizan

Reputation: 552

Adding a new Node after specific node in Linked List Implementation

I am following some tutorial go implementing linkedlist in C#. This is my code which i copied from that tutorial.

 internal class Node
    {
        internal int data;
        internal Node next;
        public Node(int d)
        {
            data = d;
            next = null;
        }
    }
    internal class SingleLinkedList
    {
        public void printAllNodes()
        {
            Node current = head;
            while (current != null)
            {
                Console.WriteLine(current.data);
                current = current.next;
            }
        }
        internal Node head;
        internal void InsertFront(SingleLinkedList singlyList, int new_data)
        {
            Node new_node = new Node(new_data);
            new_node.next = singlyList.head;
            singlyList.head = new_node;
        }
        internal void InsertLast(SingleLinkedList singlyList, int new_data)
        {
            Node new_node = new Node(new_data);
            if (singlyList.head == null)
            {
                singlyList.head = new_node;
                return;
            }
            Node lastNode = GetLastNode(singlyList);
            lastNode.next = new_node;
        }
        internal Node GetLastNode(SingleLinkedList singlyList)
        {
            Node temp = singlyList.head;
            while (temp.next != null)
            {
                temp = temp.next;
            }
            return temp;
        }
        internal void InsertAfter(Node prev_node, int new_data)
        {
            if (prev_node == null)
            {
                Console.WriteLine("The given previous node Cannot be null");
                return;
            }
            Node new_node = new Node(new_data);
            new_node.next = prev_node.next;
            prev_node.next = new_node;
        }
    }
}

Now i Added some nodes and displaying it which is working very fine.

SingleLinkedList obj = new SingleLinkedList();
            obj.InsertFront(obj, 7);
            obj.printAllNodes();
            obj.InsertFront(obj, 9);
            obj.printAllNodes();
            obj.InsertLast(obj, 345);
            obj.printAllNodes();

Now i want to use that "InsertAfter" function which i am using but it is not working correctly , it is still showing the old nodes.What's wrong with it?

obj.InsertAfter(new Node(7), 10);
            obj.printAllNodes();

Upvotes: 1

Views: 1306

Answers (1)

Harutyun Imirzyan
Harutyun Imirzyan

Reputation: 464

Maybe this is your requirement:

SingleLinkedList obj = new SingleLinkedList();
obj.InsertFront(obj, 7);
obj.InsertFront(obj, 9);
var d = obj.GetLastNode(obj);
obj.InsertLast(obj, 345);
obj.InsertAfter(d, 44);
obj.printAllNodes();

I think this is the best solution if you don't change the class SingleLinkedList. But if I were you, I would consider editing that class.

EDIT:

I would implement "insert" methods like this:

internal Node InsertFront(SingleLinkedList singlyList, int new_data)
{
    Node new_node = new Node(new_data);
    new_node.next = singlyList.head;
    singlyList.head = new_node;
    return new_node;
}

So that instead of calling obj.InsertFront(obj, 7); I would call Node node = obj.InsertFront(obj, 7); and it would then help me to insert node after any node: obj.InsertAfter(node, 44);

Upvotes: 1

Related Questions