Exemplo n.º 1
0
        public DLinkedList <T> NewList(int j)
        {
            DLinkedList <T> NewList = new DLinkedList <T>();

            NewList.Add(0);
            Node <T> NewCur  = NewList.head;
            Node <T> current = head;
            int      counter = 0;

            while (counter != j)
            {
                counter++;
                current = current.Next;
            }
            j       = current.Data;
            current = head;
            while (counter != count)
            {
                NewCur.Data = j * (count - current.Data);
                counter++;
                NewList.Add(0);
                NewCur = NewCur.Next;
            }
            return(NewList);
        }
Exemplo n.º 2
0
        public void Merge(DLinkedList <T> list2)
        {
            var elem1 = head;

            while (list2.head != null)
            {
                var current = list2.head;
                if (current.CompareTo(elem1) < 0)
                {
                    list2.head    = current.Next;
                    current.Next  = head;
                    head.Previous = current;
                    head          = current;
                }
                else if (current.CompareTo(elem1) > 0)
                {
                    var elem2 = head.Next;
                    while (current.CompareTo(elem2) > 0)
                    {
                        if (elem2.Next == null)
                        {
                            break;
                        }
                        elem2 = elem2.Next;
                    }
                    list2.head = current.Next;

                    elem2.Previous.Next = current;
                    current.Previous    = elem2.Previous;
                    current.Next        = elem2;
                    elem2.Previous      = current;
                }
                else if (current.CompareTo(elem1) == 0)
                {
                    list2.head       = current.Next;
                    current.Next     = head.Next;
                    current.Previous = head;
                    head.Next        = current;
                }
            }
        }
Exemplo n.º 3
0
        public Node <T>[] Divide()
        {
            Node <T>        current       = head;
            DLinkedList <T> multipleThree = new DLinkedList <T>();
            DLinkedList <T> normalDigits  = new DLinkedList <T>();

            while (current != null)
            {
                if (current.Data % 3 == 0)
                {
                    multipleThree.Add(current.Data);
                }
                else
                {
                    normalDigits.Add(current.Data);
                }
                current = current.Next;
            }

            return(new Node <T>[] { multipleThree.head, normalDigits.head });
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            DLinkedList <int> linkedList  = new DLinkedList <int>();
            DLinkedList <int> linkedList2 = new DLinkedList <int>();
            int k; // k element
            int n; //array size
            int j; // j for the last task

            int[] array;



            Console.WriteLine("Input size of array..");
            n     = int.Parse(Console.ReadLine());
            array = new int[n];
            Console.WriteLine("Input array elements..");
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = int.Parse(Console.ReadLine());
            }
            array = Merge_Sort(array);



            //#1
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Write("sTask #1\n");
                Console.ForegroundColor = ConsoleColor.White;
            }
            linkedList.CreateFromIntArr(array);

            /*
             * //#2
             * {
             *  Console.ForegroundColor = ConsoleColor.DarkRed;
             *  Console.Write("\n\nTask #2\n");
             *  Console.ForegroundColor = ConsoleColor.White;
             * }
             * linkedList.Decode(array);
             *
             * //#3
             * {
             *  Console.ForegroundColor = ConsoleColor.DarkRed;
             *  Console.Write("\n\nTask #3\n");
             *  Console.ForegroundColor = ConsoleColor.White;
             * }
             * Console.Write("Input 'k'...");
             * k = int.Parse(Console.ReadLine());
             * linkedList.Add(k);
             * linkedList.ShowList();
             * Console.ForegroundColor = ConsoleColor.DarkGreen;
             * Console.WriteLine($"Added {k}");
             * Console.ForegroundColor = ConsoleColor.White;
             *
             * //#4
             * {
             *  Console.ForegroundColor = ConsoleColor.DarkRed;
             *  Console.Write("\n\nTask #4\n");
             *  Console.ForegroundColor = ConsoleColor.White;
             * }
             * linkedList.Delete(k);
             * linkedList.ShowList();
             *
             * //#5 Merge
             *
             *
             */
            //#6 Max n. of same elements in DLinkedList
            linkedList.ShowList();
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Write("\n\nTask #7\n");
                Console.ForegroundColor = ConsoleColor.White;
            }
            Console.Write($"Max same numbers in list is {linkedList.MaxNum()}");

            /*
             *
             * //#7 divide DLinkedList into 2: multiples of 3 and everyone else. return array of 2 links with begining of list
             * {
             *  Console.ForegroundColor = ConsoleColor.DarkRed;
             *  Console.Write("\n\nTask #7\n");
             *  Console.ForegroundColor = ConsoleColor.White;
             * }
             * linkedList.Divide();
             *
             *
             */
            //#8 orderd list with one rule: every element is j*(n-j)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Write("\n\nTask #8\n");
                Console.ForegroundColor = ConsoleColor.White;
            }
            Console.Write("Input 'j'...\n");
            j           = int.Parse(Console.ReadLine());
            linkedList2 = linkedList.NewList(j);
            linkedList2.ShowList();
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine($"Every element is element {j}*(n- element {j})");
            Console.ForegroundColor = ConsoleColor.White;

            /*
             */
            Console.ReadKey();
        }