public void TestDeleteLast() { var lst = SingleLinkedList <string> .CreateList("the"); lst.InsertLast("quick"); lst.InsertLast("brown"); lst.InsertLast("fox"); lst.InsertLast("jumped"); lst.InsertLast("over"); lst.InsertLast("the"); lst.InsertLast("lazy"); lst.InsertLast("dog"); lst.Print(); while (lst.Head != null) { lst.DeleteLast(); lst.Print(); } Assert.IsTrue(lst.IsEmpty, "List should be empty now"); try { var node = lst.DeleteLast(); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message.Equals("List is empty."), "Error in delete first for empty list"); } }
public void ReArrageListOddLenghtTest() { var list = SingleLinkedList <string> .CreateList("a1"); list.InsertLast("a2"); list.InsertLast("a3"); list.InsertLast("a4"); list.InsertLast("b1"); list.InsertLast("b2"); list.InsertLast("b3"); list.Print(); try { LinkedListProblems.ReArrageList(list); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message.Equals("list length should be even."), "Odd length list should throw error"); } list.InsertLast("b4"); list.InsertLast("b5"); try { LinkedListProblems.ReArrageList(list); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message.Equals("list length should be even."), "Odd length list should throw error"); } list.Print(); }
public void FindNthToLastTest() { var list = SingleLinkedList <int> .CreateList(5); list.InsertLast(2); list.InsertLast(8); list.InsertLast(9); list.InsertLast(6); list.InsertLast(3); list.InsertLast(7); list.Print(); var node = LinkedListProblems.GetNthFromLast <int>(list, 3); Assert.IsTrue(node != null && node.Data == 6, string.Format("Found = {0}. Expected = 6", node.Data.ToString())); node = LinkedListProblems.GetNthFromLast <int>(list, 1); Assert.IsTrue(node != null && node == list.Tail, string.Format("Found = {0}. Expected = {1}", node.Data, list.Tail.Data)); node = LinkedListProblems.GetNthFromLast <int>(list, 7); Assert.IsTrue(node != null && node == list.Head, string.Format("Found = {0}. Expected = {1}", node.Data, list.Head.Data)); node = LinkedListProblems.GetNthFromLast <int>(list, 0); Assert.IsTrue(node == null, "0th to last is not a valid position."); try { node = LinkedListProblems.GetNthFromLast <int>(list, 10); Assert.IsTrue(false, "Should have thrown argument exception"); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message.Equals("N too big."), string.Format("N={0} should be out of range.", 10)); } }
public void DeleteDuplicateSingleNodeTest() { var list = SingleLinkedList <int> .CreateList(5); LinkedListProblems.DeleteDuplicates(list); list.Print(); }
public void ReArrageListNullTest() { var list = SingleLinkedList <string> .CreateList("a1"); list.DeleteFirst(); list.Print(); LinkedListProblems.ReArrageList(list); list.Print(); }
public void ReArrageListTwoNodeTest() { var list = SingleLinkedList <string> .CreateList("a1"); list.InsertLast("b1"); list.Print(); LinkedListProblems.ReArrageList(list); list.Print(); }
public void DeleteDuplicateWhereAllUniqueTest() { var list = SingleLinkedList <int> .CreateList(5); list.InsertLast(2); list.InsertLast(8); list.InsertLast(9); list.InsertLast(7); list.InsertLast(3); list.Print(); LinkedListProblems.DeleteDuplicates(list); list.Print(); }
public void ReArrageListBigListTest() { var list = SingleLinkedList <string> .CreateList("a1"); list.InsertLast("a2"); list.InsertLast("a3"); list.InsertLast("a4"); list.InsertLast("b1"); list.InsertLast("b2"); list.InsertLast("b3"); list.InsertLast("b4"); list.Print(); LinkedListProblems.ReArrageList(list); list.Print(); }
public void TestInsertFirst() { var ilist = SingleLinkedList <int> .CreateList(10); ilist.InsertFirst(5); ilist.Print(); Assert.IsTrue(ilist.Head.Data == 5, "5 should be head"); Assert.IsTrue(ilist.Tail.Data == 10, "10 should be tail"); // list with null Data object var nlist = SingleLinkedList <Student> .CreateList(null); nlist.InsertFirst(new Student { Id = 1, Name = "Bewda" }); nlist.Print(); Assert.IsTrue(nlist.Head.Data.Id == 1, "Head id should be 1"); Assert.IsTrue(nlist.Tail.Data == null, "Tail data should be null"); }
public void TestInsertAfter() { var ilist = SingleLinkedList <int> .CreateList(1); ilist.InsertLast(4); ilist.Print(); ilist.InsertAfter(4, 6); ilist.Print(); ilist.InsertAfter(4, 5); ilist.Print(); ilist.InsertAfter(1, 2); ilist.Print(); ilist.InsertAfter(2, 3); ilist.Print(); try { ilist.InsertAfter(0, -1); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message == "Failed to find node to insert after."); } try { var lst = SingleLinkedList <string> .CreateList("the"); var node = lst.DeleteFirst(); Assert.IsTrue(node.Data == "the", "Deleted '{0}'. Expected = '{1}'", node.Data, "the"); lst.InsertAfter("quick", "brown"); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message == "List is empty", "some other error encountered"); } }
public void TestCreateList() { // For different Types - int, bool, string, object var ilist = SingleLinkedList <int> .CreateList(10); Assert.IsTrue(ilist.Head == ilist.Tail, "Head is not equal tail"); var slist = SingleLinkedList <string> .CreateList("the"); Assert.IsTrue(slist.Head == slist.Tail, "Head is not equal tail"); var olist = SingleLinkedList <Student> .CreateList(new Student { Id = 1, Name = "John" }); Assert.IsTrue(olist.Head == olist.Tail, "Head is not equal tail"); Assert.IsTrue(olist.Head.Data.Id == 1, "Head is not poiting to the first node"); // Create with null data var nlist = SingleLinkedList <Student> .CreateList(null); Assert.IsFalse(nlist.IsEmpty, "List should not be empty"); Assert.IsTrue(nlist.Head.Data == null, "Head data should be null"); }
public void TestDelete() { // Delete empty var lst = SingleLinkedList <string> .CreateList("the"); lst.Print(); lst.Delete("the"); lst.Print(); try { lst.Delete("the"); } catch (ArgumentException ex) { Assert.IsTrue(ex.Message == "List is empty", "some other error encountered"); } if (lst.IsEmpty) { lst = SingleLinkedList <string> .CreateList("the"); } else { lst.InsertLast("the"); } lst.InsertLast("quick"); lst.InsertLast("brown"); lst.InsertLast("fox"); lst.InsertLast("jumped"); lst.InsertLast("over"); lst.InsertLast("the"); lst.InsertLast("lazy"); lst.InsertLast("dog"); lst.Print(); // delete head var node = lst.Delete("the"); lst.Print(); Assert.IsTrue( node != null && node.Data.Equals("the") && node.Next.Data.Equals("quick"), "Wrong node deleted"); // delete last node = lst.Delete("dog"); lst.Print(); Assert.IsTrue( node != null && node.Data.Equals("dog") && node.Next == null, "Wrong node deleted"); // delete a specific node in mid node = lst.Delete("jumped"); lst.Print(); Assert.IsTrue( node != null && node.Data.Equals("jumped") && node.Next.Data.Equals("over"), "Wrong node deleted"); //delete non-existing node node = lst.Delete("cat"); lst.Print(); Assert.IsTrue(node == null, "Wrong node deleted"); }