Example #1
0
        static void Main(string[] args)
        {
            string[]              words   = { "the", "fox", "jumps", "over", "the", "dog" };
            LinkedList <string>   list    = new LinkedList <string>(words);
            MyLinkedList <string> my_list = new MyLinkedList <string>(words);

            Test(list);
            Test(my_list);

            int[] BigArrOfInt = new int[10000];
            for (int i = 0; i < 10000; i++)
            {
                BigArrOfInt[i] = i;
            }
            Console.WriteLine("Производительность LinkedList: ");
            Stopwatch performance = new Stopwatch();

            performance.Start();
            LinkedList <int> new_list = new LinkedList <int>(BigArrOfInt);

            for (int i = 0; i < 10000; i++)
            {
                if (i % 3 == 0)
                {
                    new_list.Remove(i);
                }
            }
            for (int i = 0; i < 1000; i++)
            {
                new_list.AddLast(i * i * i);
            }
            performance.Stop();
            Console.WriteLine("{0} in ms", performance.ElapsedMilliseconds);
            performance.Reset();

            Console.WriteLine("Производительность MyLinkedList: ");
            performance.Start();
            LinkedList <int> new_my_list = new LinkedList <int>(BigArrOfInt);

            for (int i = 0; i < 10000; i++)
            {
                if (i % 3 == 0)
                {
                    new_my_list.Remove(i);
                }
            }
            for (int i = 0; i < 1000; i++)
            {
                new_my_list.AddLast(i * i * i);
            }
            performance.Stop();
            Console.WriteLine("{0} in ms", performance.ElapsedMilliseconds);
            Console.WriteLine();
            Console.WriteLine("Test collection LinkedList: ");
            TestCollection(new LinkedList <string>());
            Console.WriteLine();
            Console.WriteLine("Test collection MyLinkedList: ");
            TestCollection(new MyLinkedList <string>());
            Console.WriteLine();
            Console.WriteLine("test IComporable in MyLinkedList");
            int[] arr0 = { 1, 3, 5 };
            int[] arr1 = { 1 };
            int[] arr2 = { 4, 8 };
            MyLinkedList <int> list0 = new MyLinkedList <int>(arr0);
            MyLinkedList <int> list1 = new MyLinkedList <int>(arr1);
            MyLinkedList <int> list2 = new MyLinkedList <int>(arr2);

            MyLinkedList <int>[] arr = { list0, list1, list2 };
            Console.WriteLine("Current list: ");
            for (int i = 0; i < 3; i++)
            {
                Console.Write("{0}) ", i + 1);
                MyLinkedList <int> tmp = new MyLinkedList <int>(arr[i]);

                foreach (var item in tmp)
                {
                    Console.Write("{0} ", item);
                }
                Console.WriteLine();
            }
            Console.WriteLine("Sorted List (По числу элементов в списке) : ");
            Array.Sort(arr);
            for (int i = 0; i < 3; i++)
            {
                Console.Write("{0}) ", i + 1);
                MyLinkedList <int> tmp = new MyLinkedList <int>(arr[i]);

                foreach (var item in tmp)
                {
                    Console.Write("{0} ", item);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine("Test IClonable and Sort() in MyLinkedList");
            int[] nums = { 7, 5, 9, 15, 11, 4, 24, 1, 8 };
            MyLinkedList <int> my_link_list          = new MyLinkedList <int>(nums);
            MyLinkedList <int> clone_of_my_link_list = (MyLinkedList <int>)my_link_list.Clone();

            Console.Write("List: ");
            foreach (var i in my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.Write("Cloned List: ");
            foreach (var i in clone_of_my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Change Cloned List (Remove 5 and 11) :");
            clone_of_my_link_list.Remove(5);
            clone_of_my_link_list.Remove(11);
            Console.Write("List: ");
            foreach (var i in my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.Write("Cloned List: ");
            foreach (var i in clone_of_my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Now we will sort() our original list : ");
            my_link_list.sort(new IntComparer());
            Console.Write("List: ");
            foreach (var i in my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.Write("Cloned List: ");
            foreach (var i in clone_of_my_link_list)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();
            Console.ReadLine();
        }