Circular Singly Linked List Program in C#
Posted by Superadmin on August 15 2022 07:27:40
Circular Singly Linked List Program in C#
This is a C# Program to create a singly linked circular list.
Problem Description
This C# Program Creates a Singly Linked Circular List.
Problem Solution
Here singly linked circular list, filling elements and traversal in forward direction and counting the number of elements in the list is done.
Program/Source Code
Here is source code of the C# Program to Create a Singly Linked Circular List. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/*
* C# Program to Create a Singly Linked Circular List
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSTest
{
class Circlist
{
private int currentdata;
private Circlist nextdata;
public Circlist()
{
currentdata = 0;
nextdata = this;
}
public Circlist(int value)
{
currentdata = value;
nextdata = this;
}
public Circlist Insdata(int value)
{
Circlist node = new Circlist(value);
if (this.nextdata == this)
{
node.nextdata = this;
this.nextdata = node;
}
else
{
Circlist temp = this.nextdata;
node.nextdata = temp;
this.nextdata = node;
}
return node;
}
public int Deldata()
{
if (this.nextdata == this)
{
System.Console.WriteLine("\nOnly one node!!!!");
return 0;
}
Circlist node = this.nextdata;
this.nextdata = this.nextdata.nextdata;
node = null;
return 1;
}
public void Traverse()
{
Traverse(this);
}
public void Traverse(Circlist node)
{
if (node == null)
node = this;
System.Console.WriteLine("Forward Direction!!!!");
Circlist snode = node;
do
{
System.Console.WriteLine(node.currentdata);
node = node.nextdata;
}
while (node != snode);
}
public int Gnodes()
{
return Gnodes(this);
}
public int Gnodes(Circlist node)
{
if (node == null)
node = this;
int count = 0;
Circlist snode = node;
do
{
count++;
node = node.nextdata;
}
while (node != snode);
System.Console.WriteLine("\nCurrent Node Value : " +
node.currentdata.ToString());
System.Console.WriteLine("\nTotal nodes :" + count.ToString());
return count;
}
static void Main(string[] args)
{
Circlist node1 = new Circlist(1);
node1.Deldata();
Circlist node2 = node1.Insdata(2);
node1.Deldata();
node2 = node1.Insdata(2);
Circlist node3 = node2.Insdata(3);
Circlist node4 = node3.Insdata(4);
Circlist node5 = node4.Insdata(5);
node1.Gnodes();
node3.Gnodes();
node5.Gnodes();
node1.Traverse();
node3.Deldata();
node2.Traverse();
node1.Gnodes();
node3.Gnodes();
node5.Gnodes();
Console.Read();
}
}
}
Program Explanation
This C# program is used to create a singly linked circular list. A linked list is a collection of nodes that together form a linear ordering. It takes many different forms and implementation. In its simplest form, a singly linked list is a linked list where each node is an object that stores a reference to an element and a reference, called next, to another node. Note that a node is defined in terms of itself, which is called self-referential structure.
Using Circlist() procedure insert the current data value. If else condition statement is used to check the inserted data is the value of next data. If the condition is true, then execute the statement. Otherwise, if the condition is false then execute the else statement and assign the value of ‘temp’ variable to ‘nextdata’ variable.
The Deldata() procedure is used to delete the nodes in the list. If condition statement is used to check the value of ‘nextdata’ and the ‘current data’ values are equal. If the condition is true then execute the statement.
Inside the Traverse() procedure if condition statement is used to check the value of ‘node’ variable is equal to null. If the condition is true then execute the statement. Do while loop is used to check the value of node is not equal to the value of ‘snode’ variable. If the condition is true then execute the iteration of the loop.
Runtime Test Cases
Only one Node!!!!!
Current Node Value : 1
Total nodes : 5
Current Node Value : 3
Total nodes : 5
Current Node Value : 5
Total nodes : 5
Forward Direction!!!!
1
2
3
4
5
Forward Direction!!!!
2
3
5
1
Current Node Value : 1
Total nodes : 4
Current Node Value : 3
Total nodes : 4
Current Node Value: 5
Total nodes : 4