/// <summary> /// Merge two link lists by zippering them together. Every other node will be a node from the second given LL /// </summary> /// <param name="LL1">The LL that will be traversed and operated on</param> /// <param name="LL2">The LL that will be zippered into the first LL</param> /// <returns>returns the head node of the merged LL</returns> public static Node MergeLists(LinkdList LL1, LinkdList LL2) { Node temp = LL2.Head.Next; LL1.Current = LL1.Head; while (LL1.Current.Next.Next != null) { LL1.AddAfter(LL2.Head, LL1.Current); if (temp == null) { break; } LL2.Head = temp; temp = LL2.Head.Next; LL1.Current = LL1.Current.Next.Next; } LL1.AddAfter(LL2.Head, LL1.Current); LL2.Head = temp; LL1.Append(LL2.Head); LL2.Head = null; return(LL1.Head); }
public void CanTestIfListLengthIsOdd() { Node node1 = new Node("A"); Node node2 = new Node("I"); Node node3 = new Node("B"); Node node4 = new Node("O"); Node node5 = new Node("H"); Node node6 = new Node("P"); Node node7 = new Node("H"); Node node8 = new Node("O"); Node node9 = new Node("B"); Node node10 = new Node("I"); Node node11 = new Node("A"); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); list.Append(node5); list.Append(node6); list.Append(node7); list.Append(node8); list.Append(node9); list.Append(node10); list.Append(node11); Assert.True(PalindromeCheck(list)); }
public void CanMergeListsIfLL2IsLongerThanLL1() { Node node1 = new Node(10); Node node2 = new Node(20); Node node3 = new Node(30); Node node4 = new Node(40); Node node5 = new Node(50); LinkdList LL1 = new LinkdList(node1); LL1.Append(node2); LL1.Append(node3); LL1.Append(node4); LL1.Append(node5); Node node6 = new Node(60); Node node7 = new Node(70); Node node8 = new Node(80); Node node9 = new Node(90); Node node10 = new Node(100); Node node11 = new Node(110); Node node12 = new Node(120); LinkdList LL2 = new LinkdList(node6); LL2.Append(node7); LL2.Append(node8); LL2.Append(node9); LL2.Append(node10); LL2.Append(node11); LL2.Append(node12); Assert.Equal(LL1.Head, MergeLists(LL1, LL2)); }
public static void Main(string[] args) { Node node1 = new Node("A"); Node node2 = new Node("N"); Node node3 = new Node("N"); Node node4 = new Node("A"); LinkdList list1 = new LinkdList(node1); list1.Append(node2); list1.Append(node3); list1.Append(node4); Console.WriteLine("First Linked List Test:"); list1.Print(); Console.WriteLine($"\nIs Palindrome: {PalindromeCheck(list1)}"); Node node5 = new Node("A"); Node node6 = new Node("B"); Node node7 = new Node("C"); Node node8 = new Node("D"); LinkdList list2 = new LinkdList(node5); list2.Append(node6); list2.Append(node7); list2.Append(node8); Console.WriteLine("\nSecond Linked List Test:"); list2.Print(); Console.WriteLine($"\nIs Palindrome: {PalindromeCheck(list2)}"); }
public void MergeHeadNextIsTheSameAsLinkedList2Test() { // Arrange Node n6 = new Node(5); Node n5 = new Node(4); Node n4 = new Node(3); Node n3 = new Node(2); Node n2 = new Node(1); Node n1 = new Node(0); LinkdList LL1 = new LinkdList(n6); LL1.Add(n5); LL1.Add(n4); LL1.Add(n3); LL1.Add(n2); LL1.Add(n1); Node n3v2 = new Node(12); Node n2v2 = new Node(11); Node n1v2 = new Node(10); LinkdList LL2 = new LinkdList(n3v2); LL2.Add(n2v2); LL2.Add(n1v2); // Act Node head = LL1.Merge(LL1, LL2); // Assert Assert.Equal(head.Next.Value, LL2.Head.Value); }
/// <summary> /// This method is used to test the Linked Lists in the console. /// </summary> static void TestLL() { LinkdList ll = new LinkdList(new Node(10)); ll.Add(new Node(15)); ll.Add(new Node(20)); ll.Print(); // 20 -> 15 -> 10 -> NULL; Console.WriteLine(); Console.WriteLine("Let's find Node 10"); Node found = ll.Find(10); Console.WriteLine(found.Value); Node n3 = new Node(27); Node n6 = new Node(12); Node n7 = new Node(24); ll.AddBefore(n3, ll.Find(15)); ll.AddAfter(n6, ll.Find(27)); ll.AddLast(n7); ll.Print(); Console.WriteLine(); // 20 -> 27 -> 12 -> 15 -> 10 -> 24 -> NULL; }
public void MergeDoesNotReturnNullValueTest() { // Arrange Node n6 = new Node(5); Node n5 = new Node(4); Node n4 = new Node(3); Node n3 = new Node(2); Node n2 = new Node(1); Node n1 = new Node(0); LinkdList LL1 = new LinkdList(n6); LL1.Add(n5); LL1.Add(n4); LL1.Add(n3); LL1.Add(n2); LL1.Add(n1); Node n3v2 = new Node(12); Node n2v2 = new Node(11); Node n1v2 = new Node(10); LinkdList LL2 = new LinkdList(n3v2); LL2.Add(n2v2); LL2.Add(n1v2); // Act Node head = LL1.Merge(LL1, LL2); // Assert Assert.NotNull(head); }
public static void KthElement() { Node n1 = new Node(10); Node n2 = new Node(20); Node n3 = new Node(30); Node n4 = new Node(40); LinkdList ll = new LinkdList(n4); ll.Add(n3); ll.Add(n2); ll.Add(n1); int k = 3; try { Node result = ll.KthElement(k); Console.WriteLine(result.Value); } catch (IndexOutOfRangeException) { Console.WriteLine("The input needs to be positive."); } catch (ArgumentOutOfRangeException) { Console.WriteLine("The input is too large."); } }
public void LinkedListHeadPointerIsNullOnCreate() { //Arange //Act LinkdList testList = new LinkdList(); //Assert Assert.Null(testList.Head); }
public void CanAddNode(int value) { // Arrange LinkdList ll = new LinkdList(new Node(10)); Node node = new Node(value); // Act ll.Add(node); // Assert Assert.Equal(value, ll.Head.Value); }
public void CanAddNodeToHead(int input) { //Arange LinkdList testList = new LinkdList(); Node inputNode = new Node(input); //Act testList.AddHead(inputNode); //Assert Assert.Equal(input, testList.Head.Value); }
public void ReturnsFalseIfLLIsNotPalindrome() { Node node1 = new Node("A"); Node node2 = new Node("B"); Node node3 = new Node("C"); Node node4 = new Node("D"); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); Assert.False(PalindromeCheck(list)); }
public void CanTestIfListLengthIsEven() { Node node1 = new Node("A"); Node node2 = new Node("N"); Node node3 = new Node("N"); Node node4 = new Node("A"); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); Assert.True(PalindromeCheck(list)); }
public void CanAddNode() { // Arrange LinkdList ll = new LinkdList(new Node(4)); Node node = new Node(8); Node node2 = new Node(15); //Act ll.Add(node); ll.Add(node2); //Assert Assert.Equal(ll.Head.Value, node2.Value); }
public void CheckIfListIsReversed() { Node node1 = new Node("A"); Node node2 = new Node("B"); Node node3 = new Node("C"); Node node4 = new Node("D"); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); Assert.Equal(node4, ReverseLL(list).Head); }
public void LLCanFind(object value) { Node node1 = new Node(0, 7); Node node2 = new Node(0, "cat"); Node node3 = new Node(0, "dog"); Node node4 = new Node(0, value); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); Assert.Equal(value, list.Find(value).Value); }
/// <summary> /// Adds the specified key. /// </summary> /// <param name="key">The key</param> /// <param name="value">The value</param> public void Add(string key, object value) { int index = GetHash(key); Node node = new Node(key, value); if (Table[index] == null) { LinkdList list = new LinkdList(node); Table[index] = list; } else { Table[index].Append(node); } }
public void CanFindNthNumberFromEnd() { Node node1 = new Node(10); Node node2 = new Node(20); Node node3 = new Node(30); Node node4 = new Node(40); Node node5 = new Node(50); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); list.Append(node5); Assert.Equal(20, NthNodeFromEnd(3)); }
public void CannotFindNthNumberFromEnd(int value) { Node node1 = new Node(10); Node node2 = new Node(20); Node node3 = new Node(30); Node node4 = new Node(40); Node node5 = new Node(50); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); list.Append(node5); Assert.Equal("Your input is bad", NthNodeFromEnd(value)); }
static void Main(string[] args) { LinkdList lista = new LinkdList(); lista.AddOrdered(10); lista.AddOrdered(1); lista.AddOrdered(5); lista.AddOrdered(14); lista.AddOrdered(3); lista.AddOrdered(999999); lista.Print(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine($"Resultado -> [ {lista.Find(5)} ]"); }
static void Main(string[] args) { Node node1 = new Node("A"); Node node2 = new Node("B"); Node node3 = new Node("C"); Node node4 = new Node("D"); LinkdList list = new LinkdList(node1); list.Append(node2); list.Append(node3); list.Append(node4); Console.WriteLine("Original Linked List:"); list.Print(); Console.WriteLine("\nReversed Linked List:"); ReverseLL(list).Print(); }
public void ReturnsNullForNodeThatDoesNotExist(int value) { // Arrange LinkdList ll = new LinkdList(new Node(4)); Node node = new Node(8); Node node2 = new Node(15); Node node3 = new Node(16); Node node4 = new Node(23); // Act ll.Add(node); ll.Add(node2); ll.Add(node3); ll.Add(node4); Node found = ll.Find(value); // Assert Assert.Null(found); }
public void KthFromTheEndCorrectAnswerTest(int k, int expectedValue) { // Arrange Node n1 = new Node(10); Node n2 = new Node(20); Node n3 = new Node(30); Node n4 = new Node(40); LinkdList ll = new LinkdList(n4); ll.Add(n3); ll.Add(n2); ll.Add(n1); // Act Node result = ll.KthElement(k); // Assert Assert.Equal(result.Value, expectedValue); }
public void CanFindNodeThatExists(int value, int expected) { //Arrange LinkdList ll = new LinkdList(new Node(4)); Node node = new Node(8); Node node2 = new Node(15); Node node3 = new Node(16); Node node4 = new Node(23); ll.Add(node); ll.Add(node2); ll.Add(node3); ll.Add(node4); //Act Node found = ll.Find(value); //Assert Assert.Equal(expected, found.Value); }
public void ReturnsNullForNodeThatDoesNotExist(int value) { // Arrange LinkdList ll = new LinkdList(new Node(10)); // Act ll.Add(new Node(20)); ll.Add(new Node(30)); ll.Add(new Node(40)); ll.Add(new Node(50)); ll.Add(new Node(60)); ll.Add(new Node(70)); ll.Add(new Node(65)); ll.Add(new Node(100)); ll.Add(new Node(7)); Node found = ll.Find(value); // Assert Assert.Null(found); }
public void CanFindNodeThatExists(int value, int expected) { // Arrange LinkdList ll = new LinkdList(new Node(10)); ll.Add(new Node(20)); ll.Add(new Node(30)); ll.Add(new Node(40)); ll.Add(new Node(50)); ll.Add(new Node(60)); ll.Add(new Node(70)); ll.Add(new Node(65)); ll.Add(new Node(100)); ll.Add(new Node(7)); // Act Node result = ll.Find(value); // Assert Assert.Equal(expected, result.Value); }
public static void Main(string[] args) { Node node1 = new Node(10); Node node2 = new Node(20); Node node3 = new Node(30); Node node4 = new Node(40); Node node5 = new Node(50); LinkdList LL1 = new LinkdList(node1); LL1.Append(node2); LL1.Append(node3); LL1.Append(node4); LL1.Append(node5); Node node6 = new Node(60); Node node7 = new Node(70); Node node8 = new Node(80); Node node9 = new Node(90); Node node10 = new Node(100); Node node11 = new Node(110); Node node12 = new Node(120); LinkdList LL2 = new LinkdList(node6); LL2.Append(node7); LL2.Append(node8); LL2.Append(node9); LL2.Append(node10); LL2.Append(node11); LL2.Append(node12); Console.WriteLine("Linked List 1:"); LL1.Print(); Console.WriteLine("\nLinked List 2:"); LL2.Print(); Console.WriteLine("\nMerged Link Lists:"); MergeLists(LL1, LL2); LL1.Print(); }
static void Main(string[] args) { Console.WriteLine("Hello World!\n"); Node node1 = new Node(0, 7); Node node2 = new Node(0, "cat"); Node node3 = new Node(0, "dog"); Node node4 = new Node(0, 14); Node node5 = new Node(0, "bird"); LinkdList list = new LinkdList(node1); list.Add(node2); list.Append(node3); list.AddBefore(node4, node1); list.AddAfter(node5, node3); list.Print(); object searchTerm = 14; Node foundNode = list.Find(searchTerm); Console.WriteLine($"You searched for {searchTerm} and found {foundNode.Value}"); }
/// <summary> /// Checks to see if a linked list is a palindrome /// </summary> /// <param name="list">linked list to be checked</param> /// <returns>returns true or false whether or not linked list is palindrome</returns> public static bool PalindromeCheck(LinkdList list) { list.Current = list.Head; string test = ""; while (list.Current != null) { test = test + list.Current.Value; list.Current = list.Current.Next; } for (int i = 0; i < test.Length / 2; i++) { if (test[i] != test[test.Length - 1 - i]) { return(false); } } return(true); }
/// <summary> /// Reverses a linked list in place /// </summary> /// <param name="list">linked list to be reversed</param> /// <returns>returns the linked list after being reversed</returns> public static LinkdList ReverseLL(LinkdList list) { Node prev = null; Node curr = list.Head; Node next = curr.Next; if (curr == null) { return(list); } while (curr != null) { next = curr.Next; curr.Next = prev; prev = curr; curr = next; } list.Head = prev; return(list); }