Skip to main content

Convert singly linked list to circular list in c#

Csharp program for Convert singly linked list to circular list. Here more information.

// Include namespace system
using System;
// Csharp program for
// Convert singly linked list into circular list

// Define class of linked list Node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class LinkedList
{
	public LinkNode head;
	// Class constructor
	public LinkedList()
	{
		this.head = null;
	}
	// Check circular linked list or not
	// Note that this function is not capable to detect loop
	public bool isCircular()
	{
		if (this.head == null)
		{
			// Case when linked list is empty
			return false;
		}
		else
		{
			var temp = this.head;
			while (temp != null)
			{
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// When detecting circular node
					return true;
				}
			}
			// When not circular linked list
			return false;
		}
	}
	// Display node element of linked list
	public void display()
	{
		if (this.head == null)
		{
			Console.WriteLine("Empty Linked List");
		}
		else
		{
			Console.Write("Linked List Element :");
			var temp = this.head;
			// Iterate linked list
			while (temp != null)
			{
				// Display node
				Console.Write("  " + temp.data.ToString());
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// Stop iteration
					return;
				}
			}
			Console.WriteLine();
		}
	}
	// Coverted circular Linked list
	public void makeCircular()
	{
		if (this.head == null)
		{
			Console.WriteLine("Empty Linked List");
		}
		else
		{
			var temp = this.head;
			// Find last node
			while (temp.next != null)
			{
				temp = temp.next;
				if (temp == this.head)
				{
					// Already circular Linked list
					return;
				}
			}
			// Connect last node to first node
			temp.next = this.head;
		}
	}
	public static void Main(String[] args)
	{
		var ll = new LinkedList();
		// Insert element of linked list
		ll.head = new LinkNode(1);
		ll.head.next = new LinkNode(2);
		ll.head.next.next = new LinkNode(3);
		ll.head.next.next.next = new LinkNode(4);
		ll.head.next.next.next.next = new LinkNode(5);
		ll.head.next.next.next.next.next = new LinkNode(6);
		ll.head.next.next.next.next.next.next = new LinkNode(7);
		ll.display();
		if (ll.isCircular())
		{
			Console.WriteLine("Circular Yes");
		}
		else
		{
			Console.WriteLine("Circular No");
		}
		Console.WriteLine("After Convert");
		ll.makeCircular();
		if (ll.isCircular())
		{
			Console.WriteLine("Circular Yes");
		}
		else
		{
			Console.WriteLine("Circular No");
		}
	}
}

Output

Linked List Element :  1  2  3  4  5  6  7
Circular No
After Convert
Circular Yes




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment