ADVERTISEMENT
ADVERTISEMENT

C# program to delete a given node from the singly Linked-List

Here, we are going to learn how to delete a given node from the singly Linked-List in C#?
Submitted by Nidhi, on November 05, 2020

Here, we will delete the specified node from the linked list.

Program:

The source code to delete a given node from the single linked list is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to delete a node from the singly Linked-List. 

using System;

class ListNode
{
    private int item;
    private ListNode next;

    public ListNode(int value)
    {
        item = value;
        next = null;
    }
    public ListNode AddItem(int value)
    {
        ListNode node = new ListNode(value);
        if (this.next == null)
        {
            node.next = null;
            this.next = node;
        }
        else
        {
            ListNode temp = this.next;
            node.next = temp;
            this.next = node;
        }
        return node;
    }
    public void ListTraverse()
    {
        ListNode node = this;
            
        while (node != null)
        {
            Console.WriteLine("-->" + node.item);
            node = node.next;
        }
    }

    public void DeleteNode(ListNode given)
    {
        ListNode node = this;

        while (node != null)
        {
            if (node.next == given)
            {
                node.next = given.next;
                break;
            }
            node = node.next;
        }
    }
}

class Demo
{
    static void Main(string[] args)
    {
        ListNode StartNode = new ListNode(201);

        ListNode n1;
        ListNode n2;
        ListNode n3;
        ListNode n4; 
           
        n1 = StartNode.AddItem(202);
        n2 = n1.AddItem(203);
        n3 = n2.AddItem(204);
        n4 = n3.AddItem(205);
           
        Console.WriteLine("Linked list before deletion:");
        StartNode.ListTraverse();

       StartNode.DeleteNode(n2);
       Console.WriteLine("Linked list after deletion:");
       StartNode.ListTraverse();
    }
}

Output:

Linked list before deletion:
-->201
-->202
-->203
-->204
-->205
Linked list after deletion:
-->201
-->202
-->204
-->205
Press any key to continue . . .

Explanation:

In the above program, we created a ListNode class that contains data member's item and next. As we know that the node of a linked list contains item and pointer to the next node.

The ListNode class contains two constructors, AddItem(), ListTraverse(), and DeleteNode() methods. The AddItem method is used to add items into the node that it will return the address of the node.

The ListTraverse() method is used to traverse the list from the start node to the end of the node in the linked list.

The DeleteNode() method is used to delete a given specified node from the linked list.

Now look to the Demo class that contains the Main() method. Here, we created the nodes using ListNode class and then add items and link the node to make a Linked List. After that we finally traverse the list from start to end and printed the items on the console screen after that we deleted the node n2 from the list and printed the updated list on the console screen.

C# Data Structure Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.