static void Main(string[] args) { try { DoubleLinkedList <int> ItemList = new DoubleLinkedList <int>(); ItemList.AddFirst(0); ItemList.AddLast(6); ItemList.AddLast(7); ItemList.AddLast(8); ItemList.AddLast(9); ItemList.AddAfter(1, 0); ItemList.AddAfter(2, 1); ItemList.AddBefore(5, 6); ItemList.AddBefore(4, 5); ItemList.AddBefore(3, 4); foreach (var item in ItemList) { Console.WriteLine(item.ToString()); } Console.WriteLine(string.Format("Count = {0}", ItemList.Count)); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }
public void TestAddBeforeMethod() { var linkedList = new DoubleLinkedList <int>(); linkedList.AddFirst(4); linkedList.AddFirst(2); linkedList.AddBefore(linkedList.First, 0); linkedList.AddBefore(linkedList.Last, 3); linkedList.AddBefore(linkedList.Find(2), 1); CollectionAssert.AreEqual(new List <int> { 0, 1, 2, 3, 4 }, linkedList.ToList()); }
public void AddBeforeTest() { IDoubleLinkedList <int> list = new DoubleLinkedList <int>(); list.AddLast(1); list.AddLast(4); list.AddBefore(3, 4); Assert.Throws <ElementNotFoundException <int> >(() => list.AddAfter(2, -5)); list.AddBefore(2, 3); list.AddBefore(0, 1); Assert.Throws <ElementNotFoundException <int> >(() => list.AddAfter(-2, 5)); CheckListOrder(list); }
public void Test_AddBefore_NotExistingNode_Node() { DoubleLinkedList <int> dlList = new DoubleLinkedList <int>() { 1, 2, 3 }; dlList.AddBefore(dlList.First, new DNode <int>(5)); Assert.Equal(5, dlList.First.Data); Assert.Equal(1, dlList.First.Next.Data); Assert.Equal(2, dlList.First.Next.Next.Data); DNode <int> node = new DNode <int>(10); var checkGet = Assert.Throws <InvalidOperationException>(() => dlList.AddBefore(node, new DNode <int>(12))); Assert.Equal("Node does not exists!", checkGet.Message); }
void InvalidNode() { // Arrange var list = new DoubleLinkedList <int>(); var anotherList = new DoubleLinkedList <int>(new int[] { 1 }); var node = anotherList.Find(1); // Act Action action = () => list.AddBefore(node, 1); // Assert action.Should().ThrowExactly <InvalidOperationException>(); }
public void Test_NotEmpty_WhenAddingBefore() { DoubleLinkedList <Int32> list = new DoubleLinkedList <Int32> { 18, 88 }; list.AddBefore(88, 2); Assert.False(list.IsEmpty); Assert.Equal(3, list.Count); }
public void Test_AddRemoveOrder_Before() { DoubleLinkedList <Int32> list = new DoubleLinkedList <Int32> { 18, 88 }; list.AddBefore(18, 2); Assert.Equal(88, list.Remove()); Assert.Equal(2, list.Remove()); Assert.Equal(18, list.Remove()); }
void NullNode() { // Arrange var list = new DoubleLinkedList <int>(); // Act Action action = () => list.AddBefore(null, 1); // Assert action.Should() .ThrowExactly <ArgumentNullException>() .And .ParamName.Should() .Be("node"); }
public void TestClearMethod() { var linkedList = new DoubleLinkedList <int>(); linkedList.AddFirst(3); linkedList.AddLast(2); linkedList.AddAfter(linkedList.First, 1); linkedList.AddBefore(linkedList.Last, 3); linkedList.Clear(); Assert.AreEqual(0, linkedList.Count); Assert.IsNull(linkedList.First); Assert.IsNull(linkedList.Last); }
void AddItem(IReadOnlyList <int> collection, int after, int item, IReadOnlyList <int> expected) { // Arrange var list = new DoubleLinkedList <int>(collection); var version = list.Version; var node = list.Find(after); // Act list.AddBefore(node, item); // Assert list.Count.Should().Be(expected.Count); list.Version.Should().NotBe(version); list.EnumerateForward().Should().Equal(expected); list.EnumerateReversed().Should().Equal(expected.Reverse()); }
static void Main(string[] args) { Console.WriteLine("Custom List"); var list = new DoubleLinkedList <int>(); list.AddFirst(1); list.AddFirst(2); list.AddLast(3); list.AddBefore(list.Last, 10); list.AddAfter(list.First, 20); foreach (var element in list) { Console.WriteLine(element); } Console.WriteLine(" -------------- "); foreach (var element in list.Reverse()) { Console.WriteLine(element); } Console.WriteLine($"Count: {list.Count}"); Console.WriteLine(" -------------- "); Console.WriteLine("Removing element with value 10"); list.Remove(10); foreach (var element in list) { Console.WriteLine(element); } Console.WriteLine($"Count: {list.Count}"); Console.WriteLine(" -------------- "); Console.WriteLine("Removing first and last elements"); list.RemoveLast(); list.RemoveFirst(); foreach (var element in list) { Console.WriteLine(element); } Console.WriteLine($"Count: {list.Count}"); Console.WriteLine(" -------------- "); Console.WriteLine("Normal List"); var list2 = new LinkedList <int>(); list2.AddFirst(1); list2.AddFirst(2); list2.AddLast(3); list2.AddBefore(list2.Last, 10); list2.AddAfter(list2.First, 20); foreach (var element in list2) { Console.WriteLine(element); } Console.WriteLine(" -------------- "); foreach (var element in list2.Reverse()) { Console.WriteLine(element); } Console.WriteLine($"Count: {list2.Count}"); Console.WriteLine(" -------------- "); Console.WriteLine("Removing element with value 10"); list2.Remove(10); foreach (var element in list2) { Console.WriteLine(element); } Console.WriteLine($"Count: {list2.Count}"); Console.WriteLine("Removing first and last elements"); list2.RemoveLast(); list2.RemoveFirst(); foreach (var element in list2) { Console.WriteLine(element); } Console.WriteLine($"Count: {list2.Count}"); Console.ReadKey(); }
public void TestClearMethod() { var linkedList = new DoubleLinkedList<int>(); linkedList.AddFirst(3); linkedList.AddLast(2); linkedList.AddAfter(linkedList.First, 1); linkedList.AddBefore(linkedList.Last, 3); linkedList.Clear(); Assert.AreEqual(0, linkedList.Count); Assert.IsNull(linkedList.First); Assert.IsNull(linkedList.Last); }
public void TestAddBeforeMethod() { var linkedList = new DoubleLinkedList<int>(); linkedList.AddFirst(4); linkedList.AddFirst(2); linkedList.AddBefore(linkedList.First, 0); linkedList.AddBefore(linkedList.Last, 3); linkedList.AddBefore(linkedList.Find(2), 1); CollectionAssert.AreEqual(new List<int> { 0, 1, 2, 3, 4 }, linkedList.ToList()); }