Exemplo n.º 1
0
        public static Singly intersection(Singly l1, Singly l2)
        {
            if (l1 == null || l2 == null)
            {
                return(null);
            }

            infoList res1 = getTailandSize(l1);
            infoList res2 = getTailandSize(l2);

            if (res1.lastNode != res2.lastNode)
            {
                return(null);
            }

            int diffLists = 0;;

            if (res1.size > res2.size)
            {
                diffLists = res1.size - res2.size;
                l1        = alignList(l1, diffLists);
            }
            else if (res1.size < res2.size)
            {
                diffLists = res2.size - res1.size;
                l2        = alignList(l2, diffLists);
            }

            return(findIntersectionNode(l1, l2));
        }
Exemplo n.º 2
0
        public static TheoryData <Singly, Singly> getLists2()
        {
            Singly l1 = buildLinkedListNodeFromArray <Singly, int>(new int[2] {
                9, 3
            });
            Singly l2 = buildLinkedListNodeFromArray <Singly, int>(new int[2] {
                1, 3
            });

            Singly l1_1 = buildLinkedListNodeFromArray <Singly, int>(null);
            Singly l2_2 = buildLinkedListNodeFromArray <Singly, int>(new int[2] {
                1, 3
            });

            Singly l1_3 = buildLinkedListNodeFromArray <Singly, int>(null);
            Singly l2_3 = buildLinkedListNodeFromArray <Singly, int>(null);

            Singly l1_4 = buildLinkedListNodeFromArray <Singly, int>(new int[0] {
            });
            Singly l2_4 = buildLinkedListNodeFromArray <Singly, int>(new int[0] {
            });

            return(new TheoryData <Singly, Singly>()
            {
                { l1, l2 }, { l1_1, l2_2 }, { l1_3, l2_3 }, { l1_4, l2_4 }
            });
        }
Exemplo n.º 3
0
        //k=1 last element
        //k=2 the second to last element
        public static Singly KLastElement(Singly head, int k)
        {
            if (k <= 0)
            {
                return(null);
            }

            Singly node = head;
            int    i    = 0;

            while (node != null)
            {
                i++;
                node = node.next;
            }

            int index = i - k;

            if (index < 0)
            {
                return(null);
            }

            node = head;
            i    = 0;

            while (i != index)
            {
                i++;
                node = node.next;
            }

            return(node);
        }
Exemplo n.º 4
0
        public static Singly addListsForward(Singly list1, Singly list2)
        {
            int len1 = lengthList(list1);
            int len2 = lengthList(list2);

            if (len1 > len2)
            {
                list2 = padList(list2, len1 - len2);
            }
            else if (len1 < len2)
            {
                list1 = padList(list1, len2 - len1);
            }


            PartialSum res = addLists(list1, list2);
            Singly     r   = null;

            if (res.carry > 0)
            {
                r      = new Singly(res.carry, null, res.sum);
                r.data = res.carry;
            }

            return(r ?? res.sum);
        }
Exemplo n.º 5
0
        public static Singly addListsForwardIte(Singly list1, Singly list2)
        {
            int n1  = getNumber(list1);
            int n2  = getNumber(list2);
            int sum = n1 + n2;

            return(buildList(sum));
        }
Exemplo n.º 6
0
        public static void KLastElementRecursiveTest2(Singly head, int k)
        {
            Singly node = KLastElementRec(head, k);

            Assert.True(node.data.Equals(11));
            node = KLastElementRec2(head, k);
            Assert.True(node.data.Equals(11));
        }
Exemplo n.º 7
0
        public static void KLastElementRecursiveTest3(Singly head, int k)
        {
            Singly node = KLastElementRec(head, k);

            Assert.True(node == null);
            node = KLastElementRec2(head, k);
            Assert.True(node == null);
        }
Exemplo n.º 8
0
 private static void compareValues(Singly list, int[] res)
 {
     for (int i = 0; i < res.Length; i++)
     {
         Assert.True(list?.data.Equals(res[i]));
         list = list.next;
     }
 }
Exemplo n.º 9
0
        public static void listLoopTest2(Singly list)
        {
            Singly startLoopNode = loopDetection(list);

            Assert.True(startLoopNode == null);

            startLoopNode = loopDetection2(list);
            Assert.True(startLoopNode == null);
        }
Exemplo n.º 10
0
        public static void KLastElementTest2(Singly head, int k)
        {
            Singly node = KLastElement(head, k);

            Assert.True(node.data.Equals(11));
            Singly node2 = KLastElementTwoPointers(head, k);

            Assert.True(node2.data.Equals(11));
        }
Exemplo n.º 11
0
        public static void KLastElementTest3(Singly head, int k)
        {
            Singly node = KLastElement(head, k);

            Assert.True(node == null);
            Singly node2 = KLastElementTwoPointers(head, k);

            Assert.True(node2 == null);
        }
Exemplo n.º 12
0
        private static Singly alignList(Singly l1, int diffLists)
        {
            for (int i = 0; i < diffLists; i++)
            {
                l1 = l1.next;
            }

            return(l1);
        }
Exemplo n.º 13
0
        public static void listLoopTest(Singly list)
        {
            Singly startLoopNode = loopDetection(list);

            Assert.True(startLoopNode.data.Equals(44));

            startLoopNode = loopDetection2(list);
            Assert.True(startLoopNode.data.Equals(44));
        }
Exemplo n.º 14
0
        public static TheoryData <Singly, Singly> getLists3()
        {
            Singly l1 = buildLinkedListNodeFromArray <Singly, string>(null);
            Singly l2 = buildLinkedListNodeFromArray <Singly, string>(null);

            return(new TheoryData <Singly, Singly>()
            {
                { l1, l2 }
            });
        }
Exemplo n.º 15
0
        private static Singly findIntersectionNode(Singly l1, Singly l2)
        {
            while (l1 != l2)
            {
                l1 = l1.next;
                l2 = l2.next;
            }

            return(l1);
        }
Exemplo n.º 16
0
        public static TheoryData <Singly> getListWithoutLoop()
        {
            Singly list  = buildLinkedListNodeFromArray <Singly, int>(new int[] { 1, 2, 3, 4 });
            Singly list2 = buildLinkedListNodeFromArray <Singly, int>(new int[0] {
            });

            return(new TheoryData <Singly>()
            {
                list, null, list2
            });
        }
Exemplo n.º 17
0
        public static void deleteMiddleNodeTest(Singly middleNode)
        {
            Singly newNextNode = middleNode.next.next;

            Assert.True(middleNode.data.Equals(9));

            bool result = DeleteMiddleNode(middleNode);

            Assert.True(result);
            Assert.True(middleNode.next == newNextNode);
            Assert.True(middleNode.data.Equals(-4));
        }
Exemplo n.º 18
0
        //It is not possible to determine if the node passed to the function is the first node of a Singly Linked List
        public static bool DeleteMiddleNode(Singly middleNode)
        {
            if (middleNode == null || middleNode.next == null)
            {
                return(false);
            }

            Singly nextNode = middleNode.next;

            middleNode.data = nextNode.data;
            middleNode.next = nextNode.next;
            return(true);
        }
Exemplo n.º 19
0
        private static int getNumber(Singly list)
        {
            int number = 0;

            while (list != null)
            {
                number *= 10;
                number += list.data;
                list    = list.next;
            }

            return(number);
        }
Exemplo n.º 20
0
        public static TheoryData <Singly, Singly> getLists2()
        {
            Singly l1 = buildLinkedListNodeFromArray <Singly, int>(new int[2] {
                8, 1
            });
            Singly l2   = buildLinkedListNodeFromArray <Singly, int>(new int[0] {
            });
            Singly l2_1 = buildLinkedListNodeFromArray <Singly, int>(null);

            return(new TheoryData <Singly, Singly>()
            {
                { l1, l2 }, { l1, l2_1 }
            });
        }
Exemplo n.º 21
0
        public static TheoryData <Singly, Singly> getLists()
        {
            Singly l1 = buildLinkedListNodeFromArray <Singly, int>(new int[4] {
                9, 9, 1, 1
            });
            Singly l2 = buildLinkedListNodeFromArray <Singly, int>(new int[5] {
                9, 8, 8, 1, 1
            });

            return(new TheoryData <Singly, Singly>()
            {
                { l1, l2 }
            });
        }
Exemplo n.º 22
0
        private static Singly buildList(int num)
        {
            Singly result = null;
            Singly next   = null;

            while (num > 0)
            {
                result = new Singly(num % 10, null, next);
                num   /= 10;
                next   = result;
            }

            return(result);
        }
Exemplo n.º 23
0
        private static Singly padList(Singly l, int padding)
        {
            Singly precNode = l;
            Singly newNode  = null;

            for (int i = 0; i < padding; i++)
            {
                newNode      = new Singly();
                newNode.next = precNode;
                precNode     = newNode;
            }

            return(newNode);
        }
Exemplo n.º 24
0
        public static TheoryData <Singly> getMiddleNode()
        {
            int[]  values = { 11, 5, 15, 5, 7, 9, -4 };
            Singly node   = buildLinkedListNodeFromArray <Singly, int>(values);

            while (node.data != 9)
            {
                node = node.next;
            }

            return(new TheoryData <Singly>()
            {
                node
            });
        }
Exemplo n.º 25
0
        private static int lengthList(Singly l)
        {
            if (l == null)
            {
                return(0);
            }

            int count = 1;

            while (l.next != null)
            {
                count++;
                l = l.next;
            }
            return(count);
        }
Exemplo n.º 26
0
        private static Singly addListsCarry(Singly list1, Singly list2, int carry)
        {
            if (list1 == null && list2 == null && carry == 0)
            {
                return(null);
            }

            int value = ((list1 != null) ? list1.data : 0) +
                        ((list2 != null) ? list2.data : 0) +
                        carry;

            Singly res = new Singly(value % 10, null,
                                    addListsCarry(list1?.next, list2?.next, value > 9 ? 1 : 0));

            return(res);
        }
Exemplo n.º 27
0
        private static Result getStartLoopNodeandTail(Singly l, int n)
        {
            Result re = new Result();

            while (l.next != null)
            {
                if (l.data.Equals(n))
                {
                    re.startLoop = l;
                }
                l = l.next;
            }
            re.tail = l;

            return(re);
        }
Exemplo n.º 28
0
        public static TheoryData <Singly> getMiddleNode2()
        {
            int[]  values = { 11, 5, 15, 5, 7, 9, -4 };
            Singly node   = buildLinkedListNodeFromArray <Singly, int>(values);
            Singly tail   = node;

            while (tail.data != -4)
            {
                tail = tail.next;
            }

            return(new TheoryData <Singly>()
            {
                tail, null
            });
        }
Exemplo n.º 29
0
        private static infoList getTailandSize(Singly l)
        {
            if (l == null)
            {
                return(null);
            }

            int count = 1;

            while (l.next != null)
            {
                count++;
                l = l.next;
            }

            return(new infoList(count, l));
        }
Exemplo n.º 30
0
        public static Singly loopDetection2(Singly list)
        {
            Dictionary <Singly, bool> dic = new Dictionary <Singly, bool>();

            while (list != null)
            {
                if (dic.ContainsKey(list))
                {
                    return(list);
                }

                dic.Add(list, false);
                list = list.next;
            }

            return(null);
        }