/// <summary> /// Prints the contents of a linked list /// </summary> /// <param name="head"></param> public static void RunIteratively(LinkListNode head) { LinkListNode node; if (head == null) { throw new System.ArgumentNullException("PrintList error, head arguement null."); } node = head; // check for loop if (FindLoopInList.Run(head) == true) { Console.WriteLine("PrintList Warning, list contains loop, unable to print."); return; } while (node != null) { node.PrintNode(false, false); if (node.prev != null && node.next != null) { Console.Write("<->"); } else { Console.Write("->"); } node = node.next; if (node == null) { Console.Write("(null)\n"); } } }
/// <summary> /// Prints the contents of a linked list in reverse /// </summary> /// <param name="node"></param> public static void RunRecursivelyReverse(LinkListNode node) { if (node == null) { Console.Write("(null)"); // <- Note the order, very important!!! return; } else { RunRecursivelyReverse(node.next); // <- Note the order, very important!!! if (node.next != null) // we check for "next" instead of "prev" { Console.Write("<->"); } else { Console.Write("<-"); // we write arrow before writting number } node.PrintNode(false, false); } }
/// <summary> /// Prints the contents of a linked list /// </summary> /// <remarks> /// Recursively traverses the linked list and prints out node values /// </remarks> /// <param name="node"></param> public static void RunRecursively(LinkListNode node) { if (node == null) { Console.Write("(null)\n"); return; } else { node.PrintNode(false, false); if (node.prev != null && node.next != null) { Console.Write("<->"); } else { Console.Write("->"); } RunRecursively(node.next); } }