Esempio n. 1
0
        // generates an LinkedList<MobileObject> of size n given an array of n strings
        public static DoublyLinkedList <MobileObject> GenerateMobileObjectList(string[] names)
        {
            DoublyLinkedList <MobileObject> linkedList = new DoublyLinkedList <MobileObject>();
            Random rnd = new Random();

            // Iterates through the list populating it with MobileObjects (NPCs on odd indices, Vehicles on even)
            // uses iterator of for loop to assign ID and to pull names from names array in reverse
            // uses Random object to generate dimensions in range from 1-100
            for (int i = 0; i < names.Length; i++)
            {
                if (i % 2 == 1)
                {
                    linkedList.AddLast(new NPC(names[names.Length - 1 - i], i, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99));
                }
                else
                {
                    linkedList.AddLast(new Vehicle(names[names.Length - 1 - i], i, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99));
                }
            }
            return(linkedList);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // calls methods for testing arraylist/linkedlist
            ArrayListTests();
            DoublyLinkedListTests();
            // list of names to populate arraylist/linkedlist
            string[] names = new string[10] {
                "Toast", "Toasty", "Toaster", "Cactus", "Notacactus", "Pancreas", "ToastMaster", "ToastApprentice", "NotToast", "Bread"
            };

            // generates arraylist/linkedlist of mobileobjects using methods
            ArrayList <MobileObject>        arrayList  = GenerateMobileObjectArray(names);
            DoublyLinkedList <MobileObject> linkedList = GenerateMobileObjectList(names);

            //outputs contents of arraylist/linkedlist
            Console.WriteLine("\n\nMobileObject ArrayList:------------------------------------------------------------------------------\n\n"
                              + arrayList.PrintAllForward());
            Console.WriteLine("\n\nMobileObject LinkedList:------------------------------------------------------------------------------\n\n"
                              + linkedList.PrintAllForward());

            Console.ReadKey();
        }
Esempio n. 3
0
        // Tests and outputs results for various methods of DoublyLinkedList
        public static void DoublyLinkedListTests()
        {
            Console.WriteLine("\n\nDoublyLinkedList Tests-------------------------------------------------------");
            DoublyLinkedList <int> linkedList;

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

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

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

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

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

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

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

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

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

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

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

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

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

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