private LlNode <int> Sum(LlNode <int> node1, LlNode <int> node2, int extra = 0) { var num = node1.Data + node2.Data + extra; var carier = (num - num % 10) / 10; var node = new LlNode <int>(num % 10); var nextNode1 = node1.Next; if (nextNode1 == null && node2.Next != null) { nextNode1 = new LlNode <int>(0); } var nextNode2 = node2.Next; if (nextNode2 == null && node1.Next != null) { nextNode2 = new LlNode <int>(0); } if (nextNode1 != null && nextNode2 != null) { node.Next = Sum(nextNode1, nextNode2, carier); } else if (carier > 0) { node.Next = new LlNode <int>(carier); } return(node); }
public void Simple() { var node = new LlNode <char>('a'); node.Chain('b').Chain('c').Chain('d'); Assert.That(Backward(node), Is.EqualTo("dcba")); }
public void LongCycle() { var node0 = new LlNode <int>(0); var node1 = new LlNode <int>(1); node0.Next = node1; node1.Next = new LlNode <int>(2).Next = new LlNode <int>(30).Next = node1; Assert.That(CycleFinder.Find(node0), Is.True); }
public void MovedToCarier() { var n1 = new LlNode <int>(5); var n2 = new LlNode <int>(5); var sum = Sum(n1, n2); Assert.That(sum.Data, Is.EqualTo(0)); Assert.That(sum.Next.Data, Is.EqualTo(1)); Assert.That(sum.Next.Next, Is.Null); }
public void NoCycle() { var node0 = new LlNode <int>(0); var node1 = new LlNode <int>(1); var node2 = new LlNode <int>(2); var node3 = new LlNode <int>(3); node0.Next = node1; node1.Next = node2; node2.Next = node3; Assert.That(CycleFinder.Find(node0), Is.False); }
public void Lists_Not_Equal2() { var n1 = new LlNode <int>(1); n1.Chain(8); var n2 = new LlNode <int>(0); var sum = Sum(n2, n1); Assert.That(sum.Data, Is.EqualTo(1)); Assert.That(sum.Next.Data, Is.EqualTo(8)); Assert.That(sum.Next.Next, Is.Null); }
public void Simple() { var node1 = new LlNode <int>(2); node1.Chain(4).Chain(3); var node2 = new LlNode <int>(5); node2.Chain(6).Chain(4); var sum = Sum(node1, node2); Assert.That(sum.Data, Is.EqualTo(7)); Assert.That(sum.Next.Data, Is.EqualTo(0)); Assert.That(sum.Next.Next.Data, Is.EqualTo(8)); Assert.That(sum.Next.Next.Next, Is.Null); }
public void LongerCycle() { var node0 = new LlNode <int>(0); var node1 = new LlNode <int>(1); var node2 = new LlNode <int>(2); var node3 = new LlNode <int>(3); var node4 = new LlNode <int>(4); var node5 = new LlNode <int>(5); node0.Next = node1; node1.Next = node2; node2.Next = node3; node3.Next = node4; node4.Next = node5; node5.Next = node1; Assert.That(CycleFinder.Find(node0), Is.True); }
private string Backward(LlNode <char> node) { return((node.Next == null ? "" : Backward(node.Next)) + node.Data); }