Пример #1
0
        static void Main()
        {
            var list = new DoublyLinkedList <int>();

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");

            list.AddLast(5);
            list.AddFirst(3);
            list.AddFirst(2);
            list.AddLast(10);
            Console.WriteLine("Count = {0}", list.Count);

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");

            list.RemoveFirst();
            list.RemoveLast();
            list.RemoveFirst();

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");

            list.RemoveLast();

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");
        }
Пример #2
0
        static void Main(string[] args)
        {
            var node = new DoublyLinkedList <Cat>();

            node.AddFirst(new Cat("Maca", 7, "Maria"));
            node.AddFirst(new Cat("Myro", 2, "Qvor"));
            node.AddFirst(new Cat("Liva", 3, "Katq"));
            node.AddFirst(new Cat("Maq", 2, "Stevi"));
            node.AddFirst(new Cat("Klara", 3, "Qna"));
            node.ForEach(x => Console.WriteLine(x));

            var array = node.ToArray();

            Console.WriteLine(node.Count);

            Console.WriteLine(node.RemoveFirst());
            Console.WriteLine(node.RemoveLast());
            Console.WriteLine("After removing:");

            node.ForEach(x => Console.WriteLine(x.Name));

            Console.WriteLine("Cats names: ");
            foreach (var cat in node)
            {
                Console.WriteLine(cat.Name);
            }
            Console.WriteLine(new string('-', 20));
            //foreach (var cat in array)
            //{
            //    Console.WriteLine(cat);
            //}

            //  Console.WriteLine(string.Join(", ",node.ToArray()));
        }
        public void ForEach_EmptyList_ShouldEnumerateElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            // Act
            var items = new List <int>();

            list.ForEach(items.Add);

            // Assert
            Assert.Equal(items, new List <int> {
            });
        }
        public void AddLast_EmptyList_ShouldAddElement()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            // Act
            list.AddLast(5);

            // Assert
            Assert.Equal(1, list.Count);

            var items = new List <int>();

            list.ForEach(items.Add);
            Assert.Equal(items, new List <int> {
                5
            });
        }
        public void ForEach_MultipleElements_ShouldEnumerateElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList <string>();

            list.AddLast("Five");
            list.AddLast("Six");
            list.AddLast("Seven");

            // Act
            var items = new List <string>();

            list.ForEach(items.Add);

            // Assert
            Assert.Equal(items, new List <string> {
                "Five", "Six", "Seven"
            });
        }
        public void RemoveFirst_OneElement_ShouldMakeListEmpty()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            list.AddLast(5);

            // Act
            var element = list.RemoveFirst();

            // Assert
            Assert.Equal(5, element);
            Assert.Equal(0, list.Count);

            var items = new List <int>();

            list.ForEach(items.Add);
            Assert.Equal(items, new List <int> {
            });
        }
        public void AddLast_SeveralElements_ShouldAddElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            // Act
            list.AddLast(5);
            list.AddLast(10);
            list.AddLast(15);

            // Assert
            Assert.Equal(3, list.Count);

            var items = new List <int>();

            list.ForEach(items.Add);
            Assert.Equal(items, new List <int> {
                5, 10, 15
            });
        }
        public void RemoveLast_SeveralElements_ShouldRemoveElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            list.AddFirst(10);
            list.AddFirst(9);
            list.AddFirst(8);

            // Act
            var element = list.RemoveLast();

            // Assert
            Assert.Equal(10, element);
            Assert.Equal(2, list.Count);

            var items = new List <int>();

            list.ForEach(items.Add);
            Assert.Equal(items, new List <int> {
                8, 9
            });
        }