/// <summary>
 /// Example - Creates a loop in a linked list
 /// </summary>
 private static void Example_CreateLoopInLinkedList()
 {
     if (head == null)
     {
         Example_CreateLinkedList();
     }
     FindLoopInLinkedList.CreateLoop(head);
 }
        /// <summary>
        /// Example - Finds a loop in a linked list
        /// </summary>
        private static void Example_FindLoopInLinkedList()
        {
            if (head == null)
            {
                Example_CreateLinkedList();
            }

            if (FindLoopInLinkedList.Run(head) == true)
            {
                Console.WriteLine("Loop found in Linked List");
            }
            else
            {
                Console.WriteLine("No Loop found in Linked List");
            }
        }
示例#3
0
        /// <summary>
        /// Prints the contents of a linked list
        /// </summary>
        /// <param name="head">Head.</param>
        public static void RunIteratively(LinkedListNode head)
        {
            LinkedListNode node;

            if (head == null)
            {
                throw new System.ArgumentNullException(nameof(head), "PrintLinkedList error, head arguement null.");
            }

            node = head;

            // check for loop
            if (FindLoopInLinkedList.Run(head) == true)
            {
                Console.WriteLine("PrintLinkedList Warning, list contains loops, unable to print.");
                return;
            }

            while (node != null)
            {
                node.PrintNode(false, false);

                if (node.next != null && node.next.prev != null)
                {
                    if (node.next.prev != node)
                    {
                        Console.Write("* ERROR *");
                    }
                    else
                    {
                        Console.Write("<->");
                    }
                }
                else
                {
                    Console.Write("->");
                }

                node = node.next;
                if (node == null)
                {
                    Console.Write("(null)\n");
                }
            }
        }