Software & Finance





C# - Singly Linked Circular List





I have given here C# sample code for singly linked circular list, filling elements and traversal in forward direction and counting the number of elements in the list. 

 

Singly Linked List, Doubly Linked List, Singly Linked Circular List and Doubly Linked Circular List are an important data structure used in C# applications.


The main difference between singly linked list and singly linked circular list are the next node pointer of last node is NULL in case of singly linked list where as next node pointer of last node is first node in case of singly linked circular list.

If there is only one node in singly linked circular list, then this condition will be true (this == this->next)

Related Links:


 

Source Code


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CSTest

{

 

   class SLinkCircularList

   {

      private int data;

      private SLinkCircularList next;

 

 

      public SLinkCircularList()

      {

         data = 0;

         next = this;

      }

 

      public SLinkCircularList(int value)

      {

         data = value;

         next = this;

      }

 

      public SLinkCircularList InsertNext(int value)

      {

         SLinkCircularList node = new SLinkCircularList(value);

         if (this.next == this) // only one node in the circular list

         {

            // Easy to handle, after the two lines of executions,

            // there will be two nodes in the circular list

            node.next = this;

            this.next = node;

         }

         else

         {

            // Insert in the middle

 

            SLinkCircularList temp = this.next;

            node.next = temp;

            this.next = node;

         }

         return node;

 

      }

 

      public int DeleteNext()

      {

         if (this.next == this)

         {

            System.Console.WriteLine("\nThe node can not be deleted as there is only one node in the circular list");

            return 0;

         }

 

         SLinkCircularList node = this.next;

         this.next = this.next.next;

         node = null;

         return 1;

      }

 

      public void Traverse()

      {

         Traverse(this);

      }

 

      public void Traverse(SLinkCircularList node)

      {

         if (node == null)

            node = this;

         System.Console.WriteLine("\n\nTraversing in Forward Direction\n\n");

         SLinkCircularList startnode = node;

 

         do

         {

            System.Console.WriteLine(node.data);

            node = node.next;

         }

         while (node != startnode);

      }

 

      public int GetNumberOfNodes()

      {

         return GetNumberOfNodes(this);

      }

 

      public int GetNumberOfNodes(SLinkCircularList node)

      {

         if (node == null)

            node = this;

 

         int count = 0;

         SLinkCircularList startnode = node;

         do

         {

            count++;

            node = node.next;

         }

         while (node != startnode);

 

         System.Console.WriteLine("\nCurrent Node Value: " + node.data.ToString());

         System.Console.WriteLine("\nTotal nodes in Circular List: " + count.ToString());

 

         return count;

      }

 

      static void Main(string[] args)

      {

 

         SLinkCircularList node1 = new SLinkCircularList(1);

         node1.DeleteNext(); // Delete will fail in this case.

 

         SLinkCircularList node2 = node1.InsertNext(2);

         node1.DeleteNext(); // It will delete the node2.

 

         node2 = node1.InsertNext(2); // Insert it again

 

         SLinkCircularList node3 = node2.InsertNext(3);

         SLinkCircularList node4 = node3.InsertNext(4);

         SLinkCircularList node5 = node4.InsertNext(5);

 

         node1.GetNumberOfNodes();

         node3.GetNumberOfNodes();

         node5.GetNumberOfNodes();

 

         node1.Traverse();

         node3.DeleteNext(); // delete the node "4"

         node2.Traverse();

 

         node1.GetNumberOfNodes();

         node3.GetNumberOfNodes();

         node5.GetNumberOfNodes();

      }

   } 

}

 

Output


The node can not be deleted as there is only one node in the circular list

 

Current Node Value: 1

Total nodes in Circular List: 5

 

Current Node Value: 3

Total nodes in Circular List: 5

 

Current Node Value: 5

Total nodes in Circular List: 5

 

 

Traversing in Forward Direction

 

1

2

3

4

5

 

 

Traversing in Forward Direction

 

2

3

5

1

 

 

Current Node Value: 1

Total nodes in Circular List: 4

 

Current Node Value: 3

Total nodes in Circular List: 4

 

Current Node Value: 5

Total nodes in Circular List: 4