static void DeleteNode(LinkedLists list, int value) { var node = list.First(); while (node != null) { if (node.value == value) { node.value = node.next.value; node.next = node.next.next; break; } else { node = node.next; } } }
public void CannotFindValueInLinkedList() { // Arrange LinkedLists list = new LinkedLists(); list.Insert(4); list.Insert(8); list.Insert(15); list.Insert(16); list.Insert(23); list.Insert(42); // Act int searchForValue = 100; bool exists = list.Includes(searchForValue); // Assert Assert.False(exists); }
public void Can_Merge_Lists_Of_Different_Lengths_Shorter_First_Test() { //arrange DataStructures.LinkedLists listOne = new LinkedLists(); listOne.Insert(15); listOne.Insert(10); listOne.Insert(5); DataStructures.LinkedLists listTwo = new LinkedLists(); listTwo.Insert(12); listTwo.Insert(6); string expected = "{6} => {5} => {12} => {10} => {15} => NULL"; //act DataStructures.LinkedLists actualList = LLchallenges.mergeLists(listTwo, listOne); string actual = actualList.ToString(); //Assert Assert.Equal(expected, actual); }
public void CanSuccessfullyAddNoteToEndOfList() { // Arrange LinkedLists list = new LinkedLists(); list.Insert(4); list.Insert(8); list.Insert(15); list.Insert(16); list.Insert(23); list.Insert(42); list.AppendNewNode(5); // Act string expected = "42 -> 23 -> 16 -> 15 -> 8 -> 4 -> 5 -> NULL"; // Assert Assert.Equal(expected, list.ToString()); }
public void TestDeleteMiddleNode() { // Arrange SLLNode head = new SLLNode(1); SLLNode second = new SLLNode(2); SLLNode third = new SLLNode(3); SLLNode fourth = new SLLNode(4); head.Next = second; second.Next = third; third.Next = fourth; // Act LinkedLists.DeleteMiddleNode(second); // Assert Assert.AreEqual(head.Next.Data, 3); Assert.AreEqual(second.Next.Data, 4); Assert.IsNull(third.Next); }
public void CanSuccessfullyInsertANodeBeforeTheFirstNode() { // Arrange LinkedLists list = new LinkedLists(); list.Insert(4); list.Insert(8); list.Insert(15); list.Insert(16); list.Insert(23); list.Insert(42); list.InsertBefore(42, 5); // Act string expected = "5 -> 42 -> 23 -> 16 -> 15 -> 8 -> 4 -> NULL"; // Assert Assert.Equal(expected, list.ToString()); }
public void CanSuccessfullyInsertANodeAfterTheLast() { // Arrange LinkedLists list = new LinkedLists(); list.Insert(4); list.Insert(8); list.Insert(15); list.Insert(16); list.Insert(23); list.Insert(42); list.InsertAfter(4, 5); // Act string expected = "42 -> 23 -> 16 -> 15 -> 8 -> 4 -> 5 -> NULL"; // Assert Assert.Equal(expected, list.ToString()); }
public void CanReturnAllValuesInLinkedList() { // Arrange LinkedLists list = new LinkedLists(); list.Insert(4); list.Insert(8); list.Insert(15); list.Insert(16); list.Insert(23); list.Insert(42); // Act string value = list.ToString(); string expected = "42 -> 23 -> 16 -> 15 -> 8 -> 4 -> NULL"; // Assert Assert.Equal(expected, value); }
public void Can_Merge_Test() { //arrange DataStructures.LinkedLists listOne = new LinkedLists(); listOne.Insert(15); listOne.Insert(10); listOne.Insert(5); DataStructures.LinkedLists listTwo = new LinkedLists(); listTwo.Insert(18); listTwo.Insert(12); listTwo.Insert(6); string expected = "{5} => {6} => {10} => {12} => {15} => {18} => NULL"; //act DataStructures.LinkedLists actualList = LLchallenges.mergeLists(listOne, listTwo); string actual = actualList.ToString(); //Assert Assert.Equal(expected, actual); }
public void TestSumLists() { // Arrange // Both null SLLNode nullNode1 = null; SLLNode nullNode2 = null; // head1 null SLLNode nullHead1 = null; SLLNode nonNullHead2 = new SLLNode(1); // head2 null SLLNode nonNullHead1 = new SLLNode(2); SLLNode nullHead2 = null; // common case SLLNode commonCaseHead1 = new SLLNode(1); commonCaseHead1.AddToTail(0); commonCaseHead1.AddToTail(9); SLLNode commonCaseHead2 = new SLLNode(3); commonCaseHead2.AddToTail(2); // Act SLLNode bothNullResult = LinkedLists.SumLists(nullNode1, nullNode2); SLLNode nullHead1Result = LinkedLists.SumLists(nullHead1, nonNullHead2); SLLNode nullHead2Result = LinkedLists.SumLists(nonNullHead1, nullHead2); SLLNode commonCaseResult = LinkedLists.SumLists(commonCaseHead1, commonCaseHead2); // Assert Assert.IsNull(bothNullResult); Assert.AreEqual(nullHead1Result.Data, 1); Assert.AreEqual(nullHead2Result.Data, 2); Assert.AreEqual(commonCaseResult.Data, 4); Assert.AreEqual(commonCaseResult.Next.Data, 2); Assert.AreEqual(commonCaseResult.Next.Next.Data, 9); }
public void Partition(string list, int n) { var linkedList = parse(list); print(linkedList); LinkedLists.Partition(linkedList, n); print(linkedList); bool?lower = null; var node = linkedList.Head; while (node != null) { if (node.Value < n) { if (lower == null) { lower = true; } else if (lower == false) { Assert.Fail("found element lower than threshold after higher elements"); } } else { if (lower == null) { lower = false; } else if (lower == true) { lower = false; } } node = node.Next; } }
public void TestLoopDetection() { // Arrange // null head SLLNode nullHead = null; // single node SLLNode singleNode = new SLLNode(1); // common case SLLNode commonCaseHead = new SLLNode(1); SLLNode second = new SLLNode(2); commonCaseHead.Next = second; SLLNode third = new SLLNode(3); second.Next = third; SLLNode fourth = new SLLNode(4); third.Next = fourth; SLLNode fifth = new SLLNode(5); fourth.Next = fifth; SLLNode sixth = new SLLNode(6); fifth.Next = sixth; sixth.Next = third; // Act SLLNode nullHeadResult = LinkedLists.LoopDetection(nullHead); SLLNode singleNodeResult = LinkedLists.LoopDetection(singleNode); SLLNode commonCaseResult = LinkedLists.LoopDetection(commonCaseHead); // Assert Assert.IsNull(nullHeadResult); Assert.IsNull(singleNodeResult); Assert.AreEqual(third, commonCaseResult); }
static void Main(string[] args) { LinkedLists llist1 = new LinkedLists(); LinkedLists llist2 = new LinkedLists(); llist1.AddToEnd(new Node(10)); llist1.AddToEnd(new Node(20)); llist1.AddToEnd(new Node(30)); Console.WriteLine("the first List elements are :"); llist1.Print(); llist2.AddToEnd(new Node(15)); llist2.AddToEnd(new Node(17)); llist2.AddToEnd(new Node(40)); Console.WriteLine("the second list elements are :"); llist2.Print(); llist1.head = SortedMerge(llist1.head, llist2.head); Console.WriteLine("After sorting two lists The values are :"); llist1.Print(); Console.Read(); }
//Still getting infinite loop public static LinkedLists mergeLists(LinkedLists lOne, LinkedLists lTwo) { if (lOne.Head != null && lTwo.Head != null) { Node oneCurrent = lOne.Head.Next; Node twoCurrent = lTwo.Head; Node newCurrent = lOne.Head; while (oneCurrent != null || twoCurrent != null) { if (twoCurrent != null) { newCurrent.Next = twoCurrent; newCurrent = newCurrent.Next; twoCurrent = twoCurrent.Next; } if (oneCurrent != null) { newCurrent.Next = oneCurrent; newCurrent = newCurrent.Next; oneCurrent = oneCurrent.Next; } } return(lOne); } else if (lOne.Head != null && lTwo.Head == null) { return(lOne); } else if (lOne.Head == null && lTwo.Head != null) { return(lTwo); } else { return(lOne); } }
public void TestIsPalindrome() { // Arrange // null head SLLNode nullHead = null; // single node SLLNode singleNode = new SLLNode(1); // not a palindrome SLLNode notAPalindrome = new SLLNode(1); notAPalindrome.AddToTail(2); // is a palindrome SLLNode isAPalindrome = new SLLNode(1); isAPalindrome.AddToTail(1); isAPalindrome.AddToTail(1); isAPalindrome.AddToTail(2); isAPalindrome.AddToTail(2); isAPalindrome.AddToTail(1); isAPalindrome.AddToTail(1); isAPalindrome.AddToTail(1); // Act bool nullHeadResult = LinkedLists.IsPalindrome(nullHead); bool singleNodeResult = LinkedLists.IsPalindrome(singleNode); bool notAPalindromeResult = LinkedLists.IsPalindrome(notAPalindrome); bool isAPalindromeResult = LinkedLists.IsPalindrome(isAPalindrome); // Assert Assert.IsFalse(nullHeadResult); Assert.IsTrue(singleNodeResult); Assert.IsFalse(notAPalindromeResult); Assert.IsTrue(isAPalindromeResult); }
private static void PerformLinkedListOps() { var linkedList = new LinkedLists <int>(); linkedList.AddFirst(1); linkedList.AddFirst(0); linkedList.AddLast(-1); linkedList.AddLast(4); linkedList.Print(); Console.WriteLine(linkedList.getKthFromTheEnd(2)); Console.WriteLine("Reverse"); linkedList.Reverse(); linkedList.Print(); linkedList.DeleteFirst(); linkedList.DeleteLast(); Console.WriteLine(linkedList.Contains(1)); Console.WriteLine(linkedList.Contains(21)); Console.WriteLine(linkedList.IndexOf(1)); Console.WriteLine(linkedList.IndexOf(21)); Console.WriteLine(linkedList.ToArray()); linkedList.Print(); Console.ReadLine(); }
public void TestMixedInserts() { LinkedLists list = new LinkedLists(); list.InsertFirst(p3); // p3 list.InsertLast(p22); // p3,p22 list.InsertFirst(p9); // p9,p3,p22 list.InsertFirst(p1); // p1,p9,p3,p22 list.InsertLast(p24); // p1,p9,p3,p22,p24 list.InsertFirst(p5); // p5,p1,p9,p3,p22,p24 list.InsertLast(p16); // p5,p1,p9,p3,p22,p24,p16 Assert.AreEqual(p5, list.First); Assert.AreEqual(p16, list.Last); Assert.AreEqual(7, list.Count); Assert.AreEqual(p5, list.Items(0)); Assert.AreEqual(p1, list.Items(1)); Assert.AreEqual(p9, list.Items(2)); Assert.AreEqual(p3, list.Items(3)); Assert.AreEqual(p22, list.Items(4)); Assert.AreEqual(p24, list.Items(5)); Assert.AreEqual(p16, list.Items(6)); }
public LinkedListsTests() { linkedLists = new LinkedLists(); }
static void Main(string[] args) { ArrayAndStrings.DisplayChapter1Results(); LinkedLists.DisplayChapter2Results(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); // Sort Algorithms var sortedArray = Sorts <int> .BubbleSort(new[] { 5, 4, 3, 2, 1 }); var sortedArray2 = Sorts <int> .SelectionSort(new[] { 5, 4, 3, 2, 1 }); var sortedArray3 = Sorts <int> .InsertionSort(new[] { 5, 4, 3, 2, 1 }); var kthLargest = Sorts <int> .FindKthLargest(new[] { 3, 2, 1, 5, 6, 4 }, 2); // Recursions var triangularNumbers = Recursions.TriangularNumbers(7); var factoril = Recursions.Factorial(4); // Binary Search Tree var tree = new TreeNode(1) { Left = new TreeNode(2) { Left = new TreeNode(3) { Left = new TreeNode(5), Right = new TreeNode(6) }, Right = new TreeNode(4) { Left = new TreeNode(6), Right = new TreeNode(5) } }, Right = new TreeNode(2) { Left = new TreeNode(4) { Left = new TreeNode(5), Right = new TreeNode(6) }, Right = new TreeNode(3) { Left = new TreeNode(6), Right = new TreeNode(5) } } }; var bst = new TreeNode(5) { Left = new TreeNode(4), Right = new TreeNode(6) }; var isBst = Trees.ValidateBst(tree); var isBst2 = Trees.ValidateBstRecursive(tree); var isSymetricRec = Trees.IsSymmetricRecursive(tree); var isSymetric = Trees.IsSymmetric(tree); var closestVal = Trees.ClosestValue(bst, 4.5); var sumRoutes = Trees.SumNumbers(tree); var test = Trees.SumNumbers(bst); // Linked Lists ListNode node1 = new ListNode(8) { Next = new ListNode(7) { Next = new ListNode(9) } }; ListNode node2 = new ListNode(5) { Next = new ListNode(8) { Next = new ListNode(4) } }; var res = LinkedLists.AddTwoNumbers(node1, node2); var res2 = LinkedLists.AddTwoNumbers2(node1, node2); var res3 = LinkedLists.GetIntersectionNode(node1, node2); var res4 = LinkedLists.MergeKLists(new ListNode[] { node1 }); // Arrays var arrRes = Arrays.TwoSum(new[] { 3, 2, 4 }, 6); }
public void CanInstantiateEmptyLinkedList() { LinkedLists list = new LinkedLists(); Assert.Null(list.Head); }
public void Test1() { LinkedLists.LinkedLlistChallengeO1(); Assert.Equal(true, true); }
public void Test1() { LinkedLists.LinkedLlistChallengeO1(); Assert.True(true); }