public override string Print() { string temp = null; if (arrayA != null) { //SortCars [] test_obj = new SortCars[sizeA]; SortCars test_obj = arrayA[0] as SortCars; if (test_obj != null) { temp = "\n[\n"; for (int i = 0; i < sizeA; i++) { test_obj = arrayA[i] as SortCars; temp += test_obj.Print() + "\n"; } temp += "]\n"; } else { temp = "\n["; foreach (var item in arrayA) { temp += item.ToString() + " "; } temp += "]\n"; } } return(temp); }
public override string Print() { string temp = null; int i = 0; int count_print = count; if (count_print == 0) { temp += "[\n"; // beginning of the stack which is the same as 1st element in the array } SortCars test_obj = arrayA[0] as SortCars; if (test_obj != null) { while (count_print > 0) { if (i == 0) { test_obj = arrayA[i] as SortCars; temp += "[\n" + test_obj.Print() + "\n"; i++; } else { test_obj = arrayA[i] as SortCars; temp += test_obj.Print() + "\n"; i++; } count_print--; // reduce the size of the printed elements } } else { while (count_print > 0) { if (i == 0) { temp += "[ " + arrayA[i]; // print start of the stack i++; } else { temp += ", " + arrayA[i]; // print middle of the stack i++; } count_print--; // reduce the size of the printed elements } } temp += "]\n"; // print the last element of the stack return(temp); }
public override string Peek() { string temp = null; if (arrayA != null) { SortCars test_obj = arrayA[0] as SortCars; if (test_obj != null) { temp = "\n[\n "; test_obj = TValue as SortCars; temp = test_obj.Print() + "\n"; return(temp); } } return(TValue.ToString()); }
static void Main(string[] args) { Console.WriteLine("Please select one of the following option: 1=Buble Sort, 2=Insertion Sort, 3=Stack, 4=Circular Buffer"); Console.Write("Please enter your selection: "); string str = Console.ReadLine(); //SortItem [] Item1 = new SortItem[3] {3,"Vasya",2,"Petya",1,"Mykola" }; // SortItem[] arrayOfClass = new SortItem[3]; SortCars[] arrayOfCars = new SortCars[6] { new SortCars("Ford", 1992), new SortCars("Fiat", 1988), new SortCars("Buick", 1932), new SortCars("Ford", 1932), new SortCars("Dodge", 1999), new SortCars("Honda", 1977) }; SortCars[] arrayOfCars2 = new SortCars[3] { new SortCars("", 0), new SortCars("", 0), new SortCars("", 0), }; string result = null; switch (str) { case "1": case "Bubble Sort": //double[] arrayBub = new double[3] { 0.3, -0.33, 0 }; // init array BubbleSorter <SortCars> Bubble1 = new BubbleSorter <SortCars>(arrayOfCars); //BubbleSorter<double> Bubble1 = new BubbleSorter<double>(arrayBub); Console.Write("The array before sorting: "); result = Bubble1.Print(); Console.Write(result); Bubble1.Sort(); result = Bubble1.Print(); Console.Write("Your buble sorted array: "); //Print(); Console.Write(result); Console.WriteLine("Press ENTER to quit"); Console.Read(); break; case "2": case "Insertion Sort": //int[] arrayIns = new int[3] {3, 2, 1}; // init array //BubbleSorter<int> Sort1 = new BubbleSorter<int>(arrayIns); BubbleSorter <SortCars> Sort1 = new BubbleSorter <SortCars>(arrayOfCars); Console.Write("The array before sorting: "); result = Sort1.Print(); Console.Write(result); Sort1.Sort(); Console.Write("Insertion sorted array: "); result = Sort1.Print(); Console.Write(result); Console.WriteLine("Press ENTER to quit"); Console.Read(); break; case "3": case "Stack": //char[] arrayStack = new char[3] {' ', ' ', ' '}; // MyStack<char> StackInst = new MyStack<char>(arrayStack); MyStack <SortCars> StackInst = new MyStack <SortCars>(arrayOfCars2); //char[] inp1 = new char[3] { 'c', 'b', 'a' }; SortCars[] arrayOfCars3 = new SortCars[3] { new SortCars("Dodge", 1999), new SortCars("Ford", 1981), new SortCars("Buick", 1932), }; int j = 0; while (str != "q") { if (j > 2) { j = 0; } Console.WriteLine("Please select one of the following option: 1=push, 2=pop, q=quit"); //Console.WriteLine("Please enter your selection: "); str = Console.ReadLine(); switch (str) { case "1": case "push": // return true if stack is still not full, otherwise return false if (!StackInst.IsFull()) { StackInst.Push(arrayOfCars3[j]); j++; Console.WriteLine("Your stack is: "); result = StackInst.Print(); // print the stack current status Console.Write(result); } else { Console.WriteLine("The stack is full"); } break; case "2": case "pull": // return true if stack is still not empty, otherwise return false if (!StackInst.IsEmpty()) { StackInst.Pop(); Console.WriteLine("The value pulled from top is: " + StackInst.Peek()); Console.WriteLine("Your stack is: "); result = StackInst.Print(); // print the stack current status Console.Write(result); } else { Console.WriteLine("The stack is empty "); } break; case "q": case "quite": break; default: Console.Write("Invalid selection. "); break; } } break; case "4": case "circular buffer": SortCars[] arrayOfCars4 = new SortCars[3] { new SortCars("Honda", 1977), new SortCars("Fiat", 1988), new SortCars("Buick", 1932), }; j = 0; MyQueue <SortCars> QueueInst = new MyQueue <SortCars>(arrayOfCars2); // initialize array with 0-s while (str != "q") { Console.WriteLine("Please select one of the following option: 1=enqueue, 2=dequeue q=quit"); str = Console.ReadLine(); // read user input from the console if (j > 2) { j = 0; } switch (str) { case "1": case "enqueue": // write new element to the buffer from the head if (!QueueInst.IsFull()) { QueueInst.Enqueue(arrayOfCars4[j]); j++; Console.WriteLine("Your queue is: "); result = QueueInst.Print(); // print the stack current status Console.Write(result); } else { Console.WriteLine("The queue is full"); } break; case "2": case "dequeue": // delete first element from the buffer from the tail if (!QueueInst.IsEmpty()) { QueueInst.Dequeue(); Console.WriteLine("The value pulled from top is: " + QueueInst.Peek()); Console.WriteLine("Your queue is:"); result = QueueInst.Print(); // print the stack current status Console.Write(result); } else { Console.WriteLine("The queue is empty "); } break; case "q": case "quit": break; default: Console.Write("invalid selection. "); break; } } break; default: Console.WriteLine("Invalid selection. Please select 1, 2, or 3, or 4."); break; } Console.WriteLine("Press ENTER to quite"); Console.Read(); }
public override string Print() { string temp = null; int i = tail; int count_print = count; if (count_print == 0) // if buffer is empty print [ ] { temp = "[ ]\n"; } SortCars test_obj = arrayA[0] as SortCars; while (count_print > 0 && arrayA != null) { if (test_obj != null) { if (i < sizeA) // if printed element is less than the maximum of the array { if (tail == i) { test_obj = arrayA[i] as SortCars; temp += "[\n " + test_obj.Print() + "\n"; i++; } else if (tail != i) { test_obj = arrayA[i] as SortCars; temp += test_obj.Print() + "\n"; i++; } } else if (i >= sizeA) // if index exceed the size of the array { i = 0; if (tail == sizeA) // print start position of the buffer { test_obj = arrayA[i] as SortCars; temp += "[\n" + test_obj.Print() + "\n"; i++; } else // print the middle elements of the buffer { test_obj = arrayA[i] as SortCars; temp += test_obj.Print() + "\n"; i++; } } } else { if (tail == i) { temp += "[ " + arrayA[i]; // print the first element sof the buffer i++; } else if (tail != i) { temp += ", " + arrayA[i]; // middle element i++; } else if (i >= sizeA) // if index exceed the size of the array { i = 0; if (tail == sizeA) // print start position of the buffer { temp += "[ " + arrayA[i]; i++; } else // print the middle elements of the buffer { temp += ", " + arrayA[i]; i++; } } } count_print--; if (count_print == 0) { temp += "]\n"; } } return(temp); }