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(); }
// 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()); }