Ejemplo n.º 1
0
        // Given an int n, determines runtime for Appending/Removing 2000000 MobileObjects to a CircularLinkedList
        // runs 10 trial and returns 2 strings, the first contains the output of all of the runtimes for Appending as well as the average runtime, the second contains thee same information for removing all items
        static string[] TimeCircularList(int n)
        {
            string[] names = new string[10] {
                "Toast", "Toasty", "Toaster", "Cactus", "Notacactus", "Pancreas", "ToastMaster", "ToastApprentice", "NotToast", "Bread"
            };
            string insertOutput = "CircularList Append 2000000 Runtimes: ";
            string deleteOutput = "CircularList DeleteAll 2000000 Runtimes: ";
            long   insertTotal  = 0;
            long   deleteTotal  = 0;
            CircularLinkedList <MobileObject> circularList = new CircularLinkedList <MobileObject>();
            Random rnd = new Random();

            Console.Write("Timing CircularList Append/Delete");
            for (int i = 0; i < n; i++)
            {
                var watch = Stopwatch.StartNew();
                for (int j = 0; j < 2000000; j++)
                {
                    if (j % 2 == 0)
                    {
                        circularList.AddLast(new NPC(names[j % 10] + j / 10, j, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99));
                    }
                    else
                    {
                        circularList.AddLast(new Vehicle(names[j % 10] + j / 10, j, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99));
                    }
                }
                watch.Stop();
                insertTotal  += watch.ElapsedMilliseconds;
                insertOutput += watch.ElapsedMilliseconds + "MS ";

                watch = Stopwatch.StartNew();
                circularList.DeleteAll();
                watch.Stop();
                deleteTotal  += watch.ElapsedMilliseconds;
                deleteOutput += watch.ElapsedMilliseconds + "MS ";
                Console.Write("  .  ");
            }
            Console.WriteLine();
            insertTotal  /= n;
            deleteTotal  /= n;
            insertOutput += "\nAverage Runtime: " + insertTotal + "MS";
            deleteOutput += "\nAverage Runtime: " + deleteTotal + "MS";
            return(new string[] { insertOutput, deleteOutput });
        }
Ejemplo n.º 2
0
        // Tests and outputs results for various methods of CircularLinkedList
        public static void CircularLinkedListTests()
        {
            Console.WriteLine("\n\nCircularLinkedList Tests-------------------------------------------------------");
            CircularLinkedList <int> circularList;

            // Testing Constructor
            Console.WriteLine("\nAttempt to create CircularLinkedList:");
            try
            {
                circularList = new CircularLinkedList <int>();
                Console.WriteLine("Attempt Succeeded");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            // Testing PrintAllForward() and PrintAllReverse()
            circularList = new CircularLinkedList <int>();
            circularList.AddFirst(2);
            circularList.AddFirst(4);
            circularList.AddFirst(-7);
            circularList.AddFirst(22);
            Console.WriteLine("Attempt to print all elements:");
            Console.WriteLine("Number of elements in list: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            Console.WriteLine("\nAttempt to print all elements in reverse:");
            Console.WriteLine("Number of elements in list: " + circularList.Count);
            Console.Write(circularList.PrintAllReverse());

            // Testing AddFront()
            Console.WriteLine("\n\nAttempt to add to front of empty CircularLinkedList:");
            circularList = new CircularLinkedList <int>();
            Console.WriteLine("Number of elements in list before add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.AddFirst(2);
            Console.WriteLine("Number of elements in list after add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            Console.WriteLine("\nAttempt to add to front of CircularLinkedList:");
            Console.WriteLine("Number of elements in list before add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.AddFirst(5);
            Console.WriteLine("Number of elements in list after add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            // Testing AddLast()
            Console.WriteLine("\n\nAttempt to add to end of empty CircularLinkedList:");
            circularList = new CircularLinkedList <int>();
            Console.WriteLine("Number of elements in list before add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.AddLast(7);
            Console.WriteLine("Number of elements in list after add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            Console.WriteLine("\nAttempt to add to end of CircularLinkedList:");
            Console.WriteLine("Number of elements in list before add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.AddLast(-2);
            Console.WriteLine("Number of elements in list after add: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            // Testing DeleteFirst()
            Console.WriteLine("\n\nAttempt to delete first element of CircularLinkedList:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.DeleteFirst();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            Console.WriteLine("\nAttempt to delete first element of CircularLinkedList with one element:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.DeleteFirst();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            Console.WriteLine("\nAttempt to delete first element of CircularLinkedList with no elements:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.DeleteFirst();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            // Testing DeleteLast()
            circularList.AddFirst(4);
            circularList.AddFirst(2);
            Console.WriteLine("\n\nAttempt to delete last element of CircularLinkedList:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.DeleteLast();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            Console.WriteLine("\nAttempt to delete last element of CircularLinkedList with one element:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.DeleteLast();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            Console.WriteLine("\nAttempt to delete last element of CircularLinkedList with no elements:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());
            circularList.DeleteLast();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
            Console.Write(circularList.PrintAllForward());

            // Testing DeleteAll()
            circularList.AddFirst(2);
            circularList.AddFirst(4);
            circularList.AddFirst(-7);
            Console.WriteLine("\n\nAttempt to delete all elements of CircularLinkedList:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            circularList.DeleteAll();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
            Console.WriteLine("\nAttempt to delete all elements of empty CircularLinkedList:");
            Console.WriteLine("Number of elements in list before delete: " + circularList.Count);
            circularList.DeleteAll();
            Console.WriteLine("Number of elements in list after delete: " + circularList.Count);
        }