public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SingleLinkedList head = new SingleLinkedList();
head.Next = null;
head.Value = 1;
SingleLinkedList tail = new SingleLinkedList();
head.Next = tail;
tail.Next = null;
tail.Value = 2;
tail = new SingleLinkedList();
head.Next.Next = tail;
tail.Next = null;
tail.Value = 3;
// The tail point to nothing and therefore list is NOT circular
if (tail.Next == null)
{
Console.WriteLine("List is NOT circular");
}
// Add a pointer to the head from tail.Next. This now makes the list circular.
tail.Next = head;
if (tail.Next == head)
{
Console.WriteLine("List is circular");
}
}
public class SingleLinkedList
{
public SingleLinkedList Next { get; set; }
public int Value { get; set; }
}
}