/// <summary> /// Deeply compares two <see cref="ListRandom"/> /// </summary> /// <param name="first">First list.</param> /// <param name="second">Second list.</param> /// <param name="print">Print node data to console.</param> /// <returns>True if lists are deeply equal, false otherwise.</returns> static bool DeepCompareLists(ListRandom first, ListRandom second, bool print) { ListNode firstNode = first.Head; ListNode secondNode = second.Head; while (firstNode != null) { if (print) { Console.WriteLine($"{firstNode.Data} | {secondNode.Data}"); } if (secondNode == null) { return(false); } if (firstNode.Data != secondNode.Data && firstNode.Random.Data != secondNode.Random.Data) { return(false); } firstNode = firstNode.Next; secondNode = secondNode.Next; } return(true); }
static void Main(string[] args) { int length = 12; ListNode head = new ListNode(); ListNode tail; ListNode currentNode; head.Data = Guid.NewGuid().ToString(); tail = head; for (int i = 0; i < length - 1; i++) { tail = AddNode(tail); } currentNode = head; for (int i = 0; i < length; i++) { currentNode.Random = GetRandomNode(head, length); currentNode = currentNode.Next; } ListRandom randomList = new ListRandom { Head = head, Tail = tail, Count = length }; //Will use in-memory stream Stream stream = new MemoryStream(); randomList.Serialize(stream); ListRandom resultList = new ListRandom(); resultList.Deserialize(stream); if (DeepCompareLists(randomList, resultList, true)) { Console.WriteLine("Lists are equal."); } else { Console.WriteLine("Lists are not equal."); } Console.Read(); }