public void Q008ReverseLinkedListRecursiveTest() { var newHead = Q008ReverseLinkedList.ReverseRecursive <int>(null); Assert.IsNull(newHead); var head = new LinkedNode <int>(1); newHead = Q008ReverseLinkedList.ReverseRecursive(head); Assert.AreEqual(head, newHead); head = new LinkedNode <int>(10, new LinkedNode <int>(11)); newHead = Q008ReverseLinkedList.ReverseRecursive(head); Assert.AreEqual(head, newHead.Next); head = new LinkedNode <int>(1); var tail = head .Append(new LinkedNode <int>(2)) .Append(new LinkedNode <int>(3)) .Append(new LinkedNode <int>(4)) .Append(new LinkedNode <int>(5)) .Append(new LinkedNode <int>(6)) .Append(new LinkedNode <int>(7)) .Append(new LinkedNode <int>(8)) .Append(new LinkedNode <int>(9)); newHead = Q008ReverseLinkedList.ReverseRecursive(head); Assert.AreEqual(tail, newHead); Assert.AreEqual(null, head.Next); }
public void Q007CheckLinkedListsAreJoinHasCylicTest1() { // Create a cylic link; var cStart = new LinkedNode <int>(1); var l = cStart .Append(new LinkedNode <int>(2)) .Append(new LinkedNode <int>(3)) .Append(new LinkedNode <int>(4)) .Append(new LinkedNode <int>(5)) .Append(new LinkedNode <int>(6)) .Append(new LinkedNode <int>(7)) .Append(new LinkedNode <int>(8)) .Append(new LinkedNode <int>(9)); l.Next = cStart; // Create h1 var h1 = new LinkedNode <int>(100, new LinkedNode <int>(101)); h1.Next.Next = cStart; // Create h2 var h2 = new LinkedNode <int>(200, new LinkedNode <int>(201, new LinkedNode <int>(202, new LinkedNode <int>(203)))); h2.Next.Next.Next.Next = cStart; var actual = Q007CheckLinkedListsAreJoin.AreJoinWithCylic(h1, h2); Assert.IsTrue(actual); }
public void Q007CheckLinkedListsAreJoinHasCylicTest2() { // Create a cylic link; var cStart1 = new LinkedNode <int>(1); var l1 = cStart1 .Append(new LinkedNode <int>(2)) .Append(new LinkedNode <int>(3)) .Append(new LinkedNode <int>(4)) .Append(new LinkedNode <int>(5)) .Append(new LinkedNode <int>(6)) .Append(new LinkedNode <int>(7)) .Append(new LinkedNode <int>(8)) .Append(new LinkedNode <int>(9)); l1.Next = cStart1; // Create a cylic link2 var cStart2 = new LinkedNode <int>(11); var l2 = cStart2 .Append(new LinkedNode <int>(12)) .Append(new LinkedNode <int>(13)) .Append(new LinkedNode <int>(14)) .Append(new LinkedNode <int>(15)) .Append(new LinkedNode <int>(16)) .Append(new LinkedNode <int>(17)) .Append(new LinkedNode <int>(18)) .Append(new LinkedNode <int>(19)); l2.Next = cStart2; var h1 = new LinkedNode <int>(100, new LinkedNode <int>(101)); h1.Next.Next = cStart1; var h2 = new LinkedNode <int>(200, new LinkedNode <int>(201, new LinkedNode <int>(202, new LinkedNode <int>(203)))); h2.Next.Next.Next.Next = cStart2; var actual = Q007CheckLinkedListsAreJoin.AreJoinWithCylic(h1, h2); Assert.IsFalse(actual); }
private void RunTest(Func <LinkedNode <int>, LinkedNode <int> > func) { var result = func(null); Assert.IsNull(result); var h = new LinkedNode <int>(0); result = func(h); Assert.AreEqual(h, result); h = new LinkedNode <int>(1); h.Append(new LinkedNode <int>(2)) .Append(new LinkedNode <int>(3)); result = func(h); var expected = new[] { 2, 1, 3 }; CollectionAssert.AreEqual(expected, result.GetValueArray()); h = new LinkedNode <int>(1); h.Append(new LinkedNode <int>(2)) .Append(new LinkedNode <int>(3)) .Append(new LinkedNode <int>(4)) .Append(new LinkedNode <int>(5)) .Append(new LinkedNode <int>(6)) .Append(new LinkedNode <int>(7)) .Append(new LinkedNode <int>(8)); result = func(h); expected = new[] { 2, 1, 4, 3, 6, 5, 8, 7 }; CollectionAssert.AreEqual(expected, result.GetValueArray()); h = new LinkedNode <int>(1); h.Append(new LinkedNode <int>(2)) .Append(new LinkedNode <int>(3)) .Append(new LinkedNode <int>(4)) .Append(new LinkedNode <int>(5)) .Append(new LinkedNode <int>(6)) .Append(new LinkedNode <int>(7)); result = func(h); expected = new[] { 2, 1, 4, 3, 6, 5, 7 }; CollectionAssert.AreEqual(expected, result.GetValueArray()); }