Beispiel #1
0
 public void Given_an_cycle_list_when_check_hasCycle_then_should_return_has_cycle()
 {
     CycleNodes();
     var linkedListCycle = new LinkedListCycle();
     var result = linkedListCycle.HasCycle(_node1);
     Assert.True(result);
 }
        public void TestMethod1(int[] head, int pos, bool expected)
        {
            // Arrange
            LinkedListCycle question = new LinkedListCycle();
            ListNode        headNode = null;

            if (head != null && head.Length > 0)
            {
                headNode = new ListNode(head[0]);
                ListNode node  = headNode;
                ListNode cycle = null;
                if (pos == 0)
                {
                    cycle = headNode;
                }

                for (int i = 1; i < head.Length; i++)
                {
                    node.next = new ListNode(head[i]);
                    node      = node.next;
                    if (pos == i)
                    {
                        cycle = node;
                    }
                }

                node.next = cycle;
            }

            // Act
            bool actual = question.HasCycle(headNode);

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void HasCycleTest(int[] values, int pos, bool expected)
        {
            var head = InitializeLinkedList(values);
            var end  = head;

            if (end != null)
            {
                while (end.next != null)
                {
                    end = end.next;
                }
            }

            if (pos >= 0)
            {
                var node = head;
                var i    = 0;

                while (i++ < pos)
                {
                    node = node.next;
                }

                end.next = node;
            }

            var actual = new LinkedListCycle().HasCycle(head);

            Assert.Equal(expected, actual);
        }
Beispiel #4
0
        public void HasCycleTestCase1()
        {
            var values     = new[] { 3, 2, 0, -4 };
            var linkedList = CommonHelpers.GetCycledLinkedList(values, 2);

            LinkedListCycle.HasCycle(linkedList).Should().BeTrue();
        }
Beispiel #5
0
        public void Given_an_acyclic_list_when_check_has_cycle_then_should_return_not_has_cycle()
        {
            CycleNodes();
            _node4.Next = null;

            var linkedListCycle = new LinkedListCycle();
            var result = linkedListCycle.HasCycle(_node1);
            Assert.False(result);
        }
Beispiel #6
0
        public static void Run()
        {
            var result = LinkedListUtillities.GenerateSinglyLinkedListFromArray(new int[] { 1, 1, 2, 3, 5, 2, 4 });

            //LinkedListUtillities.PrintSLL(result);
            Console.WriteLine();
            //LinkedListUtillities.PrintSLL(RemoveDups.RemoveDuplicates(result));
            Console.WriteLine(LinkedListCycle.IsCyclePresent(result));
            Console.WriteLine(LinkedListCycle.IsCyclePresent2(result));
        }
        public void HasCycle1()
        {
            var n1 = new ListNode(1);

            n1.Next = n1;

            var sut    = new LinkedListCycle();
            var actual = sut.HasCycle(n1);

            Assert.True(actual);
        }
Beispiel #8
0
        public void OneElem()
        {
            var sol = new LinkedListCycle();

            var head = new ListNode(1);

            Assert.IsFalse(sol.HasCycle(head));

            head.next = head;

            Assert.IsTrue(sol.HasCycle(head));
        }
        public void HasCycle2()
        {
            var n1 = new ListNode(1);
            var n2 = new ListNode(2);
            var n3 = new ListNode(3);

            n1.Next = n2;
            n2.Next = n3;

            var sut    = new LinkedListCycle();
            var actual = sut.HasCycle(n1);

            Assert.False(actual);
        }
        public void HasCycle3()
        {
            var n1 = new ListNode(1);
            var n2 = new ListNode(2);
            var n3 = new ListNode(3);
            var n4 = new ListNode(4);
            var n5 = new ListNode(5);

            n1.Next = n2;
            n2.Next = n3;
            n3.Next = n4;
            n4.Next = n5;
            n5.Next = n2;

            var sut    = new LinkedListCycle();
            var actual = sut.HasCycle(n1);

            Assert.True(actual);
        }
Beispiel #11
0
        public void ThreeElem()
        {
            var sol = new LinkedListCycle();

            var head = new ListNode(1, new ListNode(2, new ListNode(3)));

            Assert.IsFalse(sol.HasCycle(head));

            head.next.next.next = head;

            Assert.IsTrue(sol.HasCycle(head));

            head.next.next.next = head.next;

            Assert.IsTrue(sol.HasCycle(head));

            head.next.next.next = head.next.next;

            Assert.IsTrue(sol.HasCycle(head));
        }
Beispiel #12
0
 public void Given_an_empty_node_when_check_hasCycle_then_should_return_not_has_cycle()
 {
     var linkedListCycle = new LinkedListCycle();
     var result = linkedListCycle.HasCycle(null);
     Assert.False(result);
 }
 public void BeforeEach()
 {
     LinkedListCycle = new LinkedListCycle();
 }
Beispiel #14
0
        public void HasCycleTestCase2()
        {
            var linkedList = new MySinglyLinkedList(1); // No cycle

            LinkedListCycle.HasCycle(linkedList).Should().BeFalse();
        }