Пример #1
0
        public void tLoopDetection()
        {
            MyLinkedList<char> list = new MyLinkedList<char>();
            MyLinkedList<char>.Node node;

            MyLinkedList<char>.Node repeatedNode = new MyLinkedList<char>.Node('C');

            node = new MyLinkedList<char>.Node('A');
            list.AddNode(node);
            node = new MyLinkedList<char>.Node('B');
            list.AddNode(node);

            list.AddNode(repeatedNode);

            node = new MyLinkedList<char>.Node('D');
            list.AddNode(node);
            node = new MyLinkedList<char>.Node('E');
            list.AddNode(node);

            list.AddNode(repeatedNode);
            Assert.IsTrue(ReferenceEquals(repeatedNode, LoopDetection.Detect(list)));

            list.RemoveNode(6);
            Assert.IsTrue(ReferenceEquals(null, LoopDetection.Detect(list)));
        }
Пример #2
0
        public void FirstTry_With_Longer_Loop()
        {
            Node <int> a = new Node <int>(1);
            Node <int> b = new Node <int>(2);

            a.Next = b;
            Node <int> c = new Node <int>(3);

            b.Next = c;
            Node <int> d = new Node <int>(4);

            c.Next = d;
            Node <int> e = new Node <int>(5);

            d.Next = e;
            Node <int> f = new Node <int>(6);

            e.Next = f;
            Node <int> g = new Node <int>(7);

            f.Next = g;
            Node <int> h = new Node <int>(8);

            g.Next = h;
            Node <int> i = new Node <int>(9);

            h.Next = i;
            i.Next = b;

            var result = new LoopDetection().FirstTry(a);

            Assert.True(result);
        }
Пример #3
0
        public void LoopDetectionAlgo_WhenLinkedListIsNonLoop_ReturnNull()
        {
            // Arrange
            var nonLoopLinkedList = new Node(1).AppendToTail(2).AppendToTail(3).AppendToTail(4).AppendToTail(5);

            // Act
            var actual = LoopDetection.LoopDetectionAlgo(nonLoopLinkedList);

            // Assert
            actual.Should().Be(null);
        }
Пример #4
0
        public void LoopDetectionAlgo_WhenLoopSizeEqualsToStepsBetweenHeadAndLoopStartingNode_ReturnCorrectLoopStartingNode()
        {
            // Arrange
            var loopStartingNode = new Node(9);
            var LoopSizeEqualsToStepsBetweenHeadAndLoopStartingNodeLinkedList = new Node(1).AppendToTail(2).AppendToTail(3).AppendToTail(4).AppendToTail(loopStartingNode)
                                                                                .AppendToTail(5).AppendToTail(6).AppendToTail(7).AppendToTail(8).AppendToTail(loopStartingNode);

            // Act
            var actual = LoopDetection.LoopDetectionAlgo(LoopSizeEqualsToStepsBetweenHeadAndLoopStartingNodeLinkedList);

            // Assert
            Object.ReferenceEquals(actual, loopStartingNode).Should().BeTrue();
        }
Пример #5
0
        public void FirstTry_Without_Loop()
        {
            Node <int> first  = new Node <int>(1);
            Node <int> second = new Node <int>(2);

            first.Next = second;
            Node <int> third = new Node <int>(3);

            second.Next = third;

            var result = new LoopDetection().FirstTry(first);

            Assert.False(result);
        }
Пример #6
0
        public void SecondTry_With_Loop()
        {
            Node <int> first  = new Node <int>(1);
            Node <int> second = new Node <int>(2);

            first.Next = second;
            Node <int> third = new Node <int>(3);

            second.Next = third;
            third.Next  = second;

            var result = new LoopDetection().SecondTry(first);

            Assert.True(result);
        }
Пример #7
0
        static void Main(string[] args)
        {
            var condition = true;

            //just keeping the current solving problem in if condition; nothing else
            if (condition)
            {
                KStacks.MainMethod();
            }
            else
            {
                #region LinkedLists
                LinkIntersection.MainMethod();
                SumList.MainMethod();
                RemoveDups <int> .MainMethod();

                ReturnKthToLast.MainMethod(1);
                DeleteMiddleNode.MainMethod();
                LoopDetection.MainMethod();
                #endregion

                #region Array and Strings
                StringRotation.IsStringRotation("waterbottle", "erbottlewat");
                ZeroMatrixImplementation();
                RotateMatrixImplementation(4);
                StringCompression.CompressedString("aabcccccaaa");
                OneAway.IsStringOneAway("pale", "paled");
                PalindromePermutation.IsPalindromePermutation("Mr. owl ate my Metal worm");
                URLify.URLifyString("Spaces in this string will be replaced by percent20");
                IsStringPermutation.VerifyStringPermutation("abdcdefgh", "aefgb2cdh");
                UniqueString.VerifyUniqueStringAsciiSet("!@#$%$^&*()EFgh");
                HashTableImplentation();
                SwapWithoutTemp.SwapWithoutTempVar(12, 24);
                #endregion
            }
        }
Пример #8
0
        public void LoopDetectionTest <T>(Node <T> input, Node <T> loopStartNodeExpected)
        {
            Node <T> loopStartNode = LoopDetection.DetectLoop(input);

            Assert.Equal(loopStartNodeExpected, loopStartNode);
        }
Пример #9
0
 public void Setup()
 {
     loopDetection = new LoopDetection();
 }