Пример #1
0
        public static void DetectCycle()
        {
            Playground.DataStructure.LinkedList <int> list = new Playground.DataStructure.LinkedList <int>();
            list.Add(1);
            var node2 = list.Add(2);

            list.Add(3);
            var node4 = list.Add(4);

            node4.Next = node2;

            LinkedListQ <int> listQ = new LinkedListQ <int>(list);

            Assert.That(listQ.DetectCycle() == node2);
        }
Пример #2
0
        public static void GetIntersectionNode()
        {
            Playground.DataStructure.LinkedList <int> list = new Playground.DataStructure.LinkedList <int>();
            list.Add(4);
            list.Add(5);

            Playground.DataStructure.LinkedList <int> list1 = new Playground.DataStructure.LinkedList <int>();
            list1.Add(1);
            list1.Add(2);
            list1.Add(3);
            list1.Add(4);
            list1.Add(5);

            var node = LinkedListQ <int> .GetIntersectionNode(list.Head, list1.Head);

            Assert.That(node.Value == 4);
        }
Пример #3
0
        public static void RotateRight()
        {
            Playground.DataStructure.LinkedList <int> list = new Playground.DataStructure.LinkedList <int>();
            list.Add(91);
            list.Add(34);
            list.Add(18);
            list.Add(83);
            list.Add(38);
            list.Add(82);
            list.Add(21);
            list.Add(69);

            LinkedListQ <int> listQ = new LinkedListQ <int>(list);
            var node = listQ.RotateRight(89);


            Assert.That(node.Value == 69);
        }
Пример #4
0
        public void MergeSortedLinkedList()
        {
            Playground.DataStructure.LinkedList <int> list = new Playground.DataStructure.LinkedList <int>();
            list.Add(1);
            list.Add(10);
            list.Add(20);

            Playground.DataStructure.LinkedList <int> list1 = new Playground.DataStructure.LinkedList <int>();
            list1.Add(4);
            list1.Add(11);
            list1.Add(13);

            Playground.DataStructure.LinkedList <int> list2 = new Playground.DataStructure.LinkedList <int>();
            list2.Add(3);
            list2.Add(8);
            list2.Add(9);

            List <Playground.DataStructure.LinkedList <int> .Node> heads = new List <Playground.DataStructure.LinkedList <int> .Node>()
            {
                list.Head, list1.Head, list2.Head
            };
            var node = LinkedListQ <int> .MergeSortedLinkedList(heads);

            Assert.That(node.Value == 1);

            int index = 1;

            while (index != 4)
            {
                node = node.Next;
                index++;
            }

            Assert.That(node.Value == 8);
            while (index != 7)
            {
                node = node.Next;
                index++;
            }
            Assert.That(node.Value == 11);
        }
Пример #5
0
        public static void SwapNodesInPairs()
        {
            Playground.DataStructure.LinkedList <int> list1 = new Playground.DataStructure.LinkedList <int>();
            list1.Add(1);
            list1.Add(2);
            list1.Add(3);
            list1.Add(4);
            var listQ = new LinkedListQ <int>(list1);

            listQ.SwapNodesInPairs();
            Assert.That(listQ.Head.Value == 2);


            list1 = new Playground.DataStructure.LinkedList <int>();
            list1.Add(1);
            list1.Add(2);
            list1.Add(3);
            listQ = new LinkedListQ <int>(list1);
            listQ.SwapNodesInPairs();
            Assert.That(listQ.Head.Value == 2);
        }
Пример #6
0
        public void InsertionSortLinkedList()
        {
            Playground.DataStructure.LinkedList <int> list = new Playground.DataStructure.LinkedList <int>();
            //3-> 5-> 66-> 68-> 42-> 73-> 25-> 84-> 63-> 72-> 20-> 77-> 38-> 8-> 99
            list.Add(3);
            list.Add(5);
            list.Add(66);
            list.Add(68);
            list.Add(42);
            list.Add(73);
            list.Add(25);
            list.Add(84);
            list.Add(63);
            list.Add(72);
            list.Add(20);
            list.Add(77);
            list.Add(38);
            list.Add(8);
            list.Add(99);

            LinkedListQ <int> listQ = new LinkedListQ <int>(list);
            var node = listQ.InsertionSortList();
        }
Пример #7
0
        public void ReverseBeetween()
        {
            Playground.DataStructure.LinkedList <int> list = new Playground.DataStructure.LinkedList <int>();
            //3-> 5-> 66-> 68-> 42-> 73-> 25-> 84-> 63-> 72-> 20-> 77-> 38-> 8-> 99
            list.Add(3);
            list.Add(5);
            list.Add(66);
            list.Add(68);
            list.Add(42);
            list.Add(73);
            list.Add(25);
            list.Add(84);
            list.Add(63);
            list.Add(72);
            list.Add(20);
            list.Add(77);
            list.Add(38);
            list.Add(8);
            list.Add(99);

            LinkedListQ <int> listQ = new LinkedListQ <int>(list);
            var node = listQ.ReverseBetween(4, 13);
            // 3-> 5-> 66-> 38-> 77-> 20-> 72-> 63-> 84-> 25-> 73-> 42-> 68-> 8-> 99
        }
Пример #8
0
        public static void ReverseListInKRange()
        {
            Playground.DataStructure.LinkedList <int> list = new Playground.DataStructure.LinkedList <int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);
            list.Add(6);

            LinkedListQ <int> listQ = new LinkedListQ <int>(list);

            Assert.That(listQ.ReverseListInGivenSize(2).Value == 2);

            Playground.DataStructure.LinkedList <int> list1 = new Playground.DataStructure.LinkedList <int>();
            list1.Add(1);
            list1.Add(2);
            list1.Add(3);
            list1.Add(4);
            list1.Add(5);
            list1.Add(6);
            listQ = new LinkedListQ <int>(list1);
            Assert.That(listQ.ReverseListInGivenSizeRecursive(listQ.Head, 2).Value == 2);
        }