private void CheckStateWithSingleNode(SinglyLinkedList <int> list) { Assert.AreEqual(1, list.Count); Assert.IsFalse(list.IsEmpty); Assert.AreSame(list.Head, list.Tail); }
public void Init() { _list = new SinglyLinkedList <int>(); }
public static BigInteger Subtract(BigInteger n1, BigInteger n2) { BigInteger sub = new BigInteger(); sub.list.RemoveFirst(); if (n1.sign && !n2.sign) { n2.sign = true; sub = Add(n1, n2); n2.sign = false; return(sub); } if (!n1.sign && n2.sign) { n2.sign = false; sub = Add(n1, n2); n2.sign = true; return(sub); } if (n1.CompareAbs(n2) < 0) { sub = Subtract(n2, n1); sub.sign = !n1.sign; return(sub); } SinglyLinkedList <int> list1 = new SinglyLinkedList <int>(); SinglyLinkedList <int> list2 = new SinglyLinkedList <int>(); sub.sign = n1.sign; int x = 0; while (!n1.list.IsEmpty() && !n2.list.IsEmpty()) { x = n1.list.First() - n2.list.First() - x; if (x < 0) { sub.list.AddLast(x + 10); x = 1; } else { sub.list.AddLast(x); x = 0; } list1.AddLast(n1.list.RemoveFirst()); list2.AddLast(n2.list.RemoveFirst()); if (n2.list.IsEmpty()) { while (!n1.list.IsEmpty()) { if (n1.list.First() == 0 && x == 1) { sub.list.AddLast(9); } else { sub.list.AddLast(n1.list.First() - x); x = 0; } list1.AddLast(n1.list.RemoveFirst()); } } } while (sub.list.Last() == 0 && sub.list.Size() != 1) { sub.list.RemoveLast(); } sub.Trim(); n1.list = list1; n2.list = list2; return(sub); }
public BigInteger() { list = new SinglyLinkedList <int>(); list.AddLast(0); }
public static BigInteger Add(BigInteger n1, BigInteger n2) { BigInteger sum = new BigInteger(); sum.list.RemoveFirst(); if (n1.sign == n2.sign) { sum.sign = n1.sign; } else { if (!n1.sign) { n1.sign = true; sum = Subtract(n2, n1); n1.sign = false; } else { n2.sign = true; sum = Subtract(n1, n2); n2.sign = false; } return(sum); } SinglyLinkedList <int> list1 = new SinglyLinkedList <int>(); SinglyLinkedList <int> list2 = new SinglyLinkedList <int>(); int x = 0; while (!n1.list.IsEmpty() && !n2.list.IsEmpty()) { x = n1.list.First() + n2.list.First() + x; list1.AddLast(n1.list.RemoveFirst()); list2.AddLast(n2.list.RemoveFirst()); sum.list.AddLast(x % 10); x /= 10; if (n1.list.IsEmpty()) { while (!n2.list.IsEmpty()) { sum.list.AddLast((n2.list.First() + x) % 10); x = (n2.list.First() + x) / 10; list2.AddLast(n2.list.RemoveFirst()); } } else if (n2.list.IsEmpty()) { while (!n1.list.IsEmpty()) { sum.list.AddLast((n1.list.First() + x) % 10); x = (n1.list.First() + x) / 10; list1.AddLast(n1.list.RemoveFirst()); } } } if (x == 1) { sum.list.AddLast(x); } n1.list = list1; n2.list = list2; return(sum); }