static void Main(string[] args) { LinkedNode <string> node1 = new LinkedNode <string>("Luna"); LinkedNode <string> node2 = new LinkedNode <string>("Star"); LinkedNode <string> node3 = new LinkedNode <string>("Dev"); LinkedNode <string> node4 = new LinkedNode <string>("Luna"); node1.Next = node2; node3.Next = node4; node3.Previous = node2; System.Console.WriteLine(node3.DetectCirculation()); node4.Next = node2; System.Console.WriteLine(node3.DetectCirculation()); }
public bool DetectCirculation() { List <LinkedNode <T> > vVisitedNodes = new List <LinkedNode <T> >(); vVisitedNodes.Add(this); LinkedNode <T> traveling = Previous; while (traveling != null) { foreach (LinkedNode <T> visited in vVisitedNodes) { if (visited == traveling) { return(true); } } vVisitedNodes.Add(traveling); traveling = traveling.Previous; } traveling = Next; while (traveling != null) { foreach (LinkedNode <T> Visited in vVisitedNodes) { if (Visited == traveling) { return(true); } } vVisitedNodes.Add(traveling); traveling = traveling.Next; } return(false); }