C# Program to Traverse the Singly Linked List
Posted by Superadmin on August 15 2022 07:25:57

C# Program to Traverse the Singly Linked List

 

 

This is a C# Program to implement traversal in singly linked list.

Problem Description

This C# Program Implements Traversal in Singly Linked List.

Problem Solution

Here Elements are added in the singly linked list and the traversal is done in the forward direction.

Program/Source Code

Here is source code of the C# Program to Implement Traversal in Singly Linked List. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement Traversal in Singly Linked List
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
    class singlelist
    {
        private int data;
        private singlelist next;
        public singlelist()
        {
            data = 0;
            next = null;
        }
        public singlelist(int value)
        {
            data = value;
            next = null;
        }
        public singlelist InsertNext(int value)
        {
 
            singlelist node = new singlelist(value);
            if (this.next == null)
            {
                node.next = null; 
                this.next = node;
            }
           else
            {
                singlelist temp = this.next;
                node.next = temp;
                this.next = node;
            }
            return node;
        }
        public int DeleteNext()
        {
            if (next == null)
                return 0;
            singlelist node = this.next;
            this.next = this.next.next;  
            node = null;
            return 1;
        }
        public void Traverse(singlelist node)
        {
            if (node == null)
                node = this;
            System.Console.WriteLine("Traversing :");
            while (node != null)
            {
                System.Console.WriteLine(node.data);
                node = node.next;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            singlelist node1 = new singlelist(11);
            singlelist node2 = node1.InsertNext(12);
            singlelist node3 = node2.InsertNext(13);
            singlelist node4 = node3.InsertNext(14);
            singlelist node5 = node4.InsertNext(15);
            node1.Traverse(null);
            Console.WriteLine("Deleting !!");
            node3.DeleteNext();
            node2.Traverse(null);
            System.Console.ReadLine();
        }
    }
}
Program Explanation

This C# program is used to implement traversal in singly linked list. Here elements are added in the singly linked list and the traversal in done in the forward direction. Traversal is the very basic operation, which presents as a part in almost every operation on a singly-linked list.

 

For instance, an algorithm may traverse a singly-linked list to find a value, find a position for insertion, etc. For a singly-linked list, only forward direction traversal is possible. Using InsertNext() insert the elements into the list.

In traverse() function, if condition statement is used to check that the value of ‘node’ variable is equal to null. If the condition is true then execute the statement. While loop is used to check that the value of ‘node’ variable is not equal to null if the condition is true then execute the iteration of the loop.

The DeleteNext() function is used to check the condition that the value of ‘next’ variable is equal to null. If the condition is true then execute the statement. Check the list hasn’t been reached the end of a list yet, does some actions with the current node, which is specific for a particular algorithm, the current node becomes previous and next node becomes current.

 
Runtime Test Cases
 
Traversing :
11
12
13
14
15
Deleting!!!
Traversing :
12
13
14