Ejemplo n.º 1
0
    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();
    }
Ejemplo n.º 2
0
        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());
        }
Ejemplo n.º 3
0
    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);
    }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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>();
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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());
        }
Ejemplo n.º 8
0
        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");
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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());
        }
Ejemplo n.º 11
0
        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());
        }