public void AddFirst_and_AddLast_OneItem_CorrectState() { _list.AddFirst(1); CheckStateWithSingleNode(_list); _list.RemoveFirst(); _list.AddLast(1); CheckStateWithSingleNode(_list); }
public BigInteger(long value) { if (value < 0) { this.sign = false; value = -value; } list = new SinglyLinkedList <int>(); if (value == 0) { list.AddLast(0); } else { while (value != 0) { list.AddLast((int)(value % 10)); value = value / 10; } } }
public static void Main() { // Declare new linked list of type string, using Generics<T>. // Big Oh notated below in Main. SinglyLinkedList <string> ll = new SinglyLinkedList <string>(); Console.WriteLine("Populate new linked list using AddFirst, AddLast and Insert methods."); // O(1) ll.AddFirst("Three"); ll.Print(); // Create new node to insert before, O(1). Node <string> n = new Node <string>("Seven"); // AddLast by node, O(n). ll.AddLast(n); ll.Print(); // Insert 5 before the 7 node, O(n). ll.InsertBefore(n, "Five"); ll.Print(); // AddLast by value, O(n). ll.AddLast("Nine"); ll.Print(); Console.WriteLine("Remove nodes using RemoveFirst, RemoveLast, Remove(value) methods."); ll.Remove("Seven"); // O(n) ll.Print(); // O(n) ll.RemoveFirst(); // O(1) ll.Print(); ll.RemoveLast(); // O(n) ll.Print(); ll.Clear(); // O(1) Console.WriteLine("Clear linked list."); Console.WriteLine($"Linked list is Empty: {ll.IsEmpty()}"); // O(1) }
public BigInteger(string value) { list = new SinglyLinkedList <int>(); if (value == null || value == "") { list.AddLast(0); } if (value[0] == '-') { this.sign = false; value = value.Substring(1); } foreach (char i in value) { list.AddFirst(i - '0'); } }
public static BigInteger Multiply(BigInteger n1, BigInteger n2) { bool sign1 = true; bool sign2 = true; if (n1.CompareAbs(n2) < 0) { return(Multiply(n2, n1)); } BigInteger mult = new BigInteger(); SinglyLinkedList <int> list2 = new SinglyLinkedList <int>(); if (!n1.sign) { n1.sign = true; sign1 = false; } if (!n2.sign) { n2.sign = true; sign2 = false; } while (!n2.list.IsEmpty()) { int a = list2.Size(); BigInteger simple = MultSimple(n1, n2.list.First()); while (a > 0) { simple.list.AddFirst(0); a--; } mult = Add(simple, mult); list2.AddLast(n2.list.RemoveFirst()); } mult.Trim(); mult.sign = !(sign1 ^ sign2); n1.sign = sign1; n2.sign = sign2; return(mult); }
public static void Main() { // Create a new singly linked list of type string. SinglyLinkedList <string> sll = new SinglyLinkedList <string>(); Console.WriteLine("Populate new singly linked list of strings with duplicates..."); sll.AddFirst("Dog"); sll.AddLast("Cat"); sll.AddLast("Pig"); sll.AddLast("Cat"); sll.AddLast("Dog"); sll.Print(); // Remove Duplicates with no memory runs in O(n^2) time, with O(1) additional space. Console.WriteLine(); Console.WriteLine("Remove Dupes using no additional memory:"); sll.RemoveDupesNoMemory(sll.Head); sll.Print(); Console.WriteLine(); Console.WriteLine("Clear linked list."); sll.Clear(); Console.WriteLine($"Linked list is Empty: {sll.IsEmpty()}"); Console.WriteLine(); Console.WriteLine("Populate new singly linked list of strings with duplicates..."); sll.AddFirst("Jack"); sll.AddLast("Jill"); sll.AddLast("John"); sll.AddLast("Jack"); sll.AddLast("Jill"); sll.Print(); // Remove dupes using HashSet runs in O(n) time, with O(n) added memory. Console.WriteLine(); Console.WriteLine("Remove Dupes using additional memory (HashSet):"); sll.RemoveDupesWithMemory(sll.Head); sll.Print(); }
public static BigInteger MultSimple(BigInteger n1, int n2) { int x = 0; BigInteger multSimple = new BigInteger(); multSimple.list.RemoveFirst(); SinglyLinkedList <int> list1 = new SinglyLinkedList <int>(); while (!n1.list.IsEmpty()) { multSimple.list.AddLast((n1.list.First() * n2 + x) % 10); x = (n1.list.First() * n2 + x) / 10; list1.AddLast(n1.list.RemoveFirst()); } while (x != 0) { multSimple.list.AddLast(x % 10); x /= 10; } n1.list = list1; return(multSimple); }
public static void Main() { // Create a new singly linked list of type string. SinglyLinkedList <string> sll = new SinglyLinkedList <string>(); Console.WriteLine("Populate new singly linked list that is NOT a palindrome..."); sll.AddFirst("Dog"); sll.AddLast("Cat"); sll.AddLast("Pig"); sll.AddLast("Farm"); sll.Print(); // The IsPalindrome contains 3 methods: // IsPalindrome: Call two helper methods: O(2n) (including helper method execution) // ReverseAndCopy: O(n), with O(n) space. // IsEqual: O(n) // Runtime for IsPalindrome method: O(2n) -> O(n) // with O(n) extra memory. // Same can be done using a Stack...instead of copying a new linked list. Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}"); Console.WriteLine(); Console.WriteLine("Clear linked list."); sll.Clear(); Console.WriteLine($"Linked list is Empty: {sll.IsEmpty()}"); Console.WriteLine(); Console.WriteLine("Populate new singly linked list that IS a palindrome..."); sll.AddFirst("Dog"); sll.AddLast("Cat"); sll.AddLast("Pig"); sll.AddLast("Cat"); sll.AddLast("Dog"); sll.Print(); Console.WriteLine($"Is linked list a palindrome: {sll.IsPalindrome(sll.Head)}"); }
public void Push(object value) { data.AddLast(value); }
public void EnQueue(object value) { data.AddLast(value); }
public BigInteger() { list = new SinglyLinkedList <int>(); list.AddLast(0); }
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 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); }