コード例 #1
0
        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);
        }
コード例 #2
0
        /// <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;
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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.");
            }
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        public void LLCanAdd(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.Add(node4);

            Assert.Equal(list.Head.Value, value);
        }
コード例 #12
0
        public static void Merge()
        {
            Node      n1  = new Node(10);
            Node      n2  = new Node(20);
            Node      n3  = new Node(30);
            Node      n4  = new Node(40);
            LinkdList LL1 = new LinkdList(n4);

            LL1.Add(n3);
            LL1.Add(n2);
            LL1.Add(n1);
            LL1.Add(new Node(0));
            LL1.Add(new Node(100));

            Node      n1_2 = new Node(5);
            Node      n2_2 = new Node(15);
            Node      n3_2 = new Node(25);
            Node      n4_2 = new Node(35);
            LinkdList LL2  = new LinkdList(n4_2);

            LL2.Add(n3_2);
            LL2.Add(n2_2);
            LL2.Add(n1_2);

            LL1.Print();
            // 100 --> 0 --> 10 --> 20 --> 30 --> 40 --> NULL
            Console.WriteLine();
            LL2.Print();
            // 5 --> 15 --> 25 --> 35 --> NULL
            Console.WriteLine();
            Node head = LL1.Merge(LL1, LL2);

            LL1.Print();
            // 100 --> 5 --> 0 --> 15 --> 10 --> 25 --> 20 --> 35 --> 30 --> 40 --> NULL
            Console.WriteLine();
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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}");
        }