static void Main(string[] args) { GenericStack <int> intStack = new GenericStack <int>(); intStack.Push(2); intStack.Push(4); intStack.Push(8); for (int i = 0; i < 3; i++) { Console.WriteLine("Pop value: {0}", intStack.Pop()); } GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Push("C#"); stringStack.Push("to"); stringStack.Push("Introduction"); for (int i = 0; i < 3; i++) { Console.WriteLine("Pop value: {0}", stringStack.Pop()); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Main(string[] args) { ObjectStack aObjectStack = new ObjectStack(); aObjectStack.Push("ABCD"); aObjectStack.Push(10); int firstItem = (int) aObjectStack.Pop(); int lastItem = (int) aObjectStack.Pop(); // Boom... IntegerStack aIntegerStack = new IntegerStack(); aIntegerStack.Push(12); //aIntegerStack.Push("ABCD"); //Compile error: good. // But for others data type you have to write seperate stacks ...;=(( GenericStack<int> intGenericStack = new GenericStack<int>(); intGenericStack.Push(1212); //intGenericStack.Push("ABCD"); //Compile error GenericStack<string> stringGenericStack = new GenericStack<string>(); //Wow. Can configure the type stringGenericStack.Push("ABCD"); //stringGenericStack.Push(2323); GenericStack<Student> studentStack = new GenericStack<Student>(); //..Great it can be used for user defined type Student student1 = new Student("001", "Pinki"); Student student2 = new Student("003", "Pavel"); studentStack.Push(student1); studentStack.Push(student2); }
// There is a trick applied with -ve numbers, make sure tree nodes do not have -ve number anytime. // when both left and right subtree traversal is complete then popped node will have -ve value, so time to print private static void PostOrderIterative(TreeNode <int> node) { var stack = new GenericStack <TreeNode <int> >(50); // taking a fail safe size while (node != null || !stack.IsEmpty()) { if (node != null) { stack.Push(node); node = node.Left; } else { node = stack.Pop(); if (node.Value > 0) { node.Value = node.Value * -1; stack.Push(node); node = node.Right; } else { Console.Write("({0}), ", node.Value * -1); node = null; } } } }
[Test] public void testPushPop() { GenericStack <string> stringStack = new GenericStack <string>(); GenericStack <List <string> > stringListStack = new GenericStack <List <string> >(); List <string> vect = new List <string>(); vect.Add("a string in a vector 1"); vect.Add("another string 2"); stringStack.Push("value 1"); stringListStack.Push(vect); stringStack.Push("value 2"); stringListStack.Push(null); Assert.AreEqual(2, stringStack.Count); Assert.AreEqual(2, stringListStack.Count); Assert.AreEqual("value 2", stringStack.Pop()); Assert.AreEqual(1, stringStack.Count); Assert.AreEqual("value 1", stringStack.Pop()); Assert.AreEqual(0, stringStack.Count); Assert.Null(stringListStack.Pop()); Assert.AreEqual(vect, stringListStack.Pop()); Assert.Null(stringStack.Pop()); Assert.AreEqual(0, stringStack.Count); }
public void TestGenericStackPeek() { GenericStack<string> stack = new GenericStack<string>(); stack.Push("1. John"); stack.Push("2. Nicholas"); stack.Push("3. Mary"); stack.Push("4. George"); Assert.AreEqual("4. George", stack.Peek()); }
static void Main(string[] args) { GenericStack<int> exampleIntStack = new GenericStack<int>(); Console.WriteLine(exampleIntStack.StackEmpty()); exampleIntStack.Push(25); exampleIntStack.Push(26); Console.WriteLine(exampleIntStack.StackEmpty()); Console.WriteLine(exampleIntStack.Pop()); Console.WriteLine(exampleIntStack.Pop()); Console.WriteLine(exampleIntStack.StackEmpty()); }
public void TestGenericStackPeek() { GenericStack <string> stack = new GenericStack <string>(); stack.Push("1. John"); stack.Push("2. Nicholas"); stack.Push("3. Mary"); stack.Push("4. George"); Assert.AreEqual("4. George", stack.Peek()); }
public void TestGenericStackContains() { GenericStack<string> stack = new GenericStack<string>(); stack.Push("1. John"); stack.Push("2. Nicholas"); stack.Push("3. Mary"); stack.Push("4. George"); stack.Push("5. Michael"); Assert.IsTrue(stack.Contains("5. Michael")); }
[Test] public void testPeek() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Push("value 1"); stringStack.Push("value 2"); Assert.AreEqual("value 2", stringStack.Peek(0)); // deepest stack Assert.AreEqual(2, stringStack.Count); Assert.AreEqual("value 2", stringStack.Top()); Assert.AreEqual("value 1", stringStack.Peek(1)); }
public void TestGenericStackContains() { GenericStack <string> stack = new GenericStack <string>(); stack.Push("1. John"); stack.Push("2. Nicholas"); stack.Push("3. Mary"); stack.Push("4. George"); stack.Push("5. Michael"); Assert.IsTrue(stack.Contains("5. Michael")); }
public void Push_WhenStackIsFull_ShouldReturnIndexOutOfRangeException() { //Act var stack = new GenericStack <int>(1); stack.Push(2); stack.Push(2); //Assert Assert.Throws <IndexOutOfRangeException>( delegate { throw new IndexOutOfRangeException(); }); }
static void Main(string[] args) { // Create a stack object GenericStack <int> stack = new GenericStack <int>(); // push in some data stack.Push(1); stack.Push(2); stack.Push(3); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
[Test] public void TestReverseIndex() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Push("1"); stringStack.Push("2"); stringStack.Push("3"); Assert.AreEqual(2, stringStack.ReverseIndex(0)); Assert.AreEqual(1, stringStack.ReverseIndex(1)); Assert.AreEqual(0, stringStack.ReverseIndex(2)); Assert.AreEqual(0, stringStack.ReverseIndex(5)); Assert.AreEqual(2, stringStack.ReverseIndex(-20)); }
public void TestGenericStackClear() { GenericStack<string> stack = new GenericStack<string>(); stack.Push("1. John"); stack.Push("2. Nicholas"); stack.Push("3. Mary"); stack.Push("4. George"); stack.Push("5. Michael"); Assert.AreEqual(5, stack.Count); stack.Clear(); Assert.AreEqual(0, stack.Count); }
public void TestGenericStackClear() { GenericStack <string> stack = new GenericStack <string>(); stack.Push("1. John"); stack.Push("2. Nicholas"); stack.Push("3. Mary"); stack.Push("4. George"); stack.Push("5. Michael"); Assert.AreEqual(5, stack.Count); stack.Clear(); Assert.AreEqual(0, stack.Count); }
[Test] public void testEquals() { GenericStack <string> stringStack = new GenericStack <string>(); GenericStack <string> stringStack2 = new GenericStack <string>(); GenericStack <List <string> > stringListStack = new GenericStack <List <string> >(); // System.out.println("stringStack type is " + stringStack.getClass()); // XXX What!? // Assert.True(stringStack == (stringListStack)); // see note in equals // Assert.True(stringStack == (stringStack2)); Assert.AreEqual(stringStack.GetHashCode(), stringStack2.GetHashCode()); // Assert.AreEqual(stringStack.GetHashCode(), stringListStack.GetHashCode()); // see note in equals // XXX C# doesn't do type erasure so this doesn't work anymore. Thankfully. Assert.AreNotEqual(stringStack.GetHashCode(), stringListStack.GetHashCode()); stringStack.Push("value 1"); Assert.False(stringStack == (stringStack2)); // Assert.False(stringStack == (stringListStack)); Assert.False(stringStack.GetHashCode() == stringStack2.GetHashCode()); stringStack2.Push("value 1"); Assert.True(stringStack.Equals(stringStack2)); Assert.AreEqual(stringStack.GetHashCode(), stringStack2.GetHashCode()); }
[Test] public void testPushAllReverse() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Push("value 1"); stringStack.Push("value 2"); GenericStack <string> stringStack2 = new GenericStack <string>(); stringStack.PushAllReverse(stringStack2); Assert.AreEqual(2, stringStack.Count); Assert.AreEqual(2, stringStack2.Count); Assert.AreEqual("value 1", stringStack2.Pop()); Assert.AreEqual("value 2", stringStack2.Pop()); }
public int[] CheckAddElementToQueue(int[] array, int element) { var stack = new GenericStack <int>(array); stack.Push(element); return(stack.ToArray()); }
[Test] public void testDup() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Dup(); Assert.AreEqual(0, stringStack.Count); stringStack.Push("value 1"); stringStack.Push("value 2"); stringStack.Dup(); Assert.AreEqual(3, stringStack.Count); Assert.AreEqual("value 2", stringStack.Peek(0)); Assert.AreEqual("value 2", stringStack.Peek(1)); Assert.AreEqual("value 1", stringStack.Peek(2)); }
[Test] public void testSwap() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Push("value 1"); stringStack.Swap(); Assert.AreEqual(1, stringStack.Count); Assert.AreEqual("value 1", stringStack.DeepPeek(0)); stringStack.Push("value 2"); stringStack.Swap(); Assert.AreEqual(2, stringStack.Count); Assert.AreEqual("value 1", stringStack.DeepPeek(1)); Assert.AreEqual("value 2", stringStack.DeepPeek(0)); }
static void GenericExplanation() { IntegerStack iStack = new IntegerStack(5); iStack.Push(0); iStack.Push(100); iStack.Push(1000); int element = iStack.Pop(); Console.WriteLine(element); element = iStack.Pop(); Console.WriteLine(element); element = iStack.Pop(); Console.WriteLine(element); GeneralPurposeStack gStack = new GeneralPurposeStack(5); gStack.Push("ABCD"); gStack.Push("jj"); gStack.Push("kk"); string element1 = (string) gStack.Pop(); GenericStack<int> genericIntStack = new GenericStack<int>(5); genericIntStack.Push(0); genericIntStack.Push(1); int element22 = genericIntStack.Pop(); Console.WriteLine(element); element = genericIntStack.Pop(); Console.WriteLine(element); GenericStack<Person> pStack = new GenericStack<Person>(5); pStack.Push(new Person { FirstName = "ABCD" }); pStack.Push(new Person { FirstName = "DEFG" }); Person element122 = pStack.Pop(); Console.WriteLine(element122.FirstName); element122 = pStack.Pop(); Console.WriteLine(element122.FirstName); }
private void button1_Click(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(textBox1.Text)) { stack.Push(textBox1.Text); textBox1.Text = ""; } }
static void Main(string[] args) { // Create a stack object GenericStack <string> stack = new GenericStack <string>(); // push in some data stack.Push("Introduction"); stack.Push("to"); stack.Push("C#"); // see if the stack contains a specific value Console.WriteLine("Stack contains 'C#'? {0}", stack.Contains("C#")); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Main(string[] args) { // create a new IntStack GenericStack <int> stack = new GenericStack <int>(); stack.Push(2); //stack.Push("apple"); stack.Push(8); for (int i = 0; i < 3; i++) { Console.WriteLine("Pop value: {0}", stack.Pop()); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
[Test] public void TestTop() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Push("Hi"); Assert.AreEqual("Hi", stringStack.Top()); stringStack.Pop(); Assert.AreEqual(null, stringStack.Top()); }
public static void Main() { Console.WriteLine("Hello World!"); GenericStack <string> numbers = new GenericStack <string>(); var hs = new HandelStack(); numbers.StackEvent += hs.StackChange; numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); numbers.Push("five"); numbers.Pop("one"); numbers.Display(); }
/// Test the generic stack public static void TestGenericStack() { // Create stack int size = 10; GenericStack <double> stack = new GenericStack <double>(size); GenericStack <string> stack2 = new GenericStack <string>(size); // Push elements on the stack try { for (int i = 0; i <= size; i++) { stack.Push(i); Console.WriteLine("Push: {0}", i); } } catch (ApplicationException ex) { Console.WriteLine("Error while pushing values on the stack: {0}", ex.Message); } // Pop elements from the stack double total = 0.0; try { for (int i = 0; i <= size + 5; i++) { // Note, no casting needed. double value = stack.Pop(); total += value; Console.WriteLine("Pop: {0}", value); } } catch (ApplicationException ex) { Console.WriteLine("Error while poping values from the stack: {0}", ex.Message); } Console.WriteLine("Total: {0}", total); // Using Generic methods int sz1 = 10; int sz2 = 6; GenericStack <double> stackA = new GenericStack <double>(sz1); GenericStack <double> stackB = new GenericStack <double>(sz2); GenericMethod.Swap <GenericStack <double> >(ref stackA, ref stackB); Console.WriteLine("Sizes of stacks: {0} {1}", stackA.Size(), stackB.Size()); // Swap 2 doubles double d1 = 1.2; double d2 = 3.0; GenericMethod.Swap <double>(ref d1, ref d2); Console.WriteLine("Sizes of stacks: {0} {1}", d1, d2); }
static void Main(string[] args) { // create a struct value GenericStack <int> structStack = new GenericStack <int>(10); // push some values in to the stack structStack.Push(2); structStack.Push(4); structStack.Push(8); // pop and write out the values for (int i = 0; i < 3; i++) { Console.WriteLine("Popped Value: {0}", structStack.Pop()); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
public void PushValueOfType(object val, ValueType t) { // Push a value of type or return error if (val == null) { throw new System.ArgumentNullException("val"); } switch (t) { case ValueType.INT: IntStack.Push(System.Convert.ToInt32(val)); break; case ValueType.ACTOR: ActorStack.Push(val as PMGActor); break; } }
static void Main(string[] args) { GenericStack <string> stringStack = new GenericStack <string>(); // push in an item of data stringStack.Push("Introduction to C#"); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
public void Push_WhenGivenOneInt_ShoudReturn1() { //Act var stack = new GenericStack <int>(1); stack.Push(5); var expected = 1; //Assert Assert.AreEqual(expected, stack.Size()); }
public void Top_WhenStackGetsOneValue_ShouldReturnValue() { //Act var stack = new GenericStack <int>(1); stack.Push(1); var expected = 1; //Assert Assert.AreEqual(expected, stack.Top()); }
[Test] public void testRot() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Push("value 1"); stringStack.Push("value 2"); stringStack.Rot(); Assert.AreEqual(2, stringStack.Count); Assert.AreEqual("value 2", stringStack.DeepPeek(1)); Assert.AreEqual("value 1", stringStack.DeepPeek(0)); stringStack.Push("value 3"); stringStack.Push("value 4"); stringStack.Rot(); Assert.AreEqual(4, stringStack.Count); Assert.AreEqual("value 2", stringStack.DeepPeek(3)); Assert.AreEqual("value 4", stringStack.DeepPeek(2)); Assert.AreEqual("value 3", stringStack.DeepPeek(1)); Assert.AreEqual("value 1", stringStack.DeepPeek(0)); }
public static void Main(string[] args) { GenericPair<int, string> pairche = new GenericPair<int, string>(5, "asd"); GenericPair<int, string> pairche2 = new GenericPair<int, string>(5, "asd"); Console.WriteLine("{0} equals {1} : {2}\n", pairche, pairche2, pairche.Equals(pairche2)); GenericStack<int> stackche = new GenericStack<int>(); stackche.Push(1); stackche.Push(2); stackche.Push(3); stackche.Push(4); Console.WriteLine(stackche.Peek()); // 4 Console.WriteLine(stackche.Pop()); // 4 Console.WriteLine(stackche.Peek()); // 3 Console.WriteLine("The stack contains 2 : {0}", stackche.Contains(2)); Console.WriteLine("The stack contains 7 : {0}", stackche.Contains(7)); stackche.Clear(); Console.WriteLine("Stack was cleared."); Console.WriteLine("The stack contains 2 : {0}\n", stackche.Contains(2)); //Console.WriteLine(stackche.Peek()); // throws InvalidOperationException; GenericDequeue<int> dequeueche = new GenericDequeue<int>(); dequeueche.AddToEnd(3); // {3} dequeueche.AddToEnd(2); // {3, 2} dequeueche.AddToFront(4); // {4, 3, 2} dequeueche.AddToEnd(1); // {4, 3, 2, 1} dequeueche.AddToFront(5); // {5, 4, 3, 2, 1} Console.WriteLine(dequeueche.PeekFromEnd()); // 1 Console.WriteLine(dequeueche.PeekFromFront()); // 5 Console.WriteLine("Dequeue contains 5: {0}", dequeueche.Contains(5)); dequeueche.RemoveFromEnd(); // {5, 4, 3, 2} dequeueche.RemoveFromFront(); // {4, 3, 2} Console.WriteLine("Dequeue contains 5: {0}", dequeueche.Contains(5)); Console.WriteLine(dequeueche.PeekFromEnd()); // 2 Console.WriteLine(dequeueche.PeekFromFront()); // 4 dequeueche.Clear(); //Console.WriteLine(dequeuche.PeekFromEnd()); // throws InvalidOperationException; }
static void Main(string[] args) { // create a GenericStack<T> using the derived type GenericStack <VolvoC30> volvoStack = new GenericStack <VolvoC30>(); // push a data item into the stack volvoStack.Push(new VolvoC30()); // print the details of the first item in the stack CarPrinter.PrintFirstCarDetails(volvoStack); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Main(string[] args) { // create a stack that will hold any value type GenericStack <ValueType> valueStack = new GenericStack <ValueType>(); // push some values into the stack valueStack.Push(10L); valueStack.Push(23.34f); valueStack.Push(-100); valueStack.Push(512); // filter the stack so that we only get int values GenericStack <int> intStack = valueStack.FilterStack <int>(); // print out the contents of the filtered stack do { Console.WriteLine("Filtered value: {0}", intStack.Pop()); } while (intStack.Count > 0); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Main(string[] args) { GenericStack<int> stack = new GenericStack<int>(); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Peek()); int[] array = new int[] { 1, 3, 5, 6, 7, 8, 9, 11, 13 }; for(int i=0;i<array.Length;i++) { stack.Push(array[i]); } Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Peek()); Console.WriteLine(stack.Contains(8)); stack.Clear(); Console.WriteLine(stack.Pop()); }
public GenericStack <U> FilterStack <U>() where U : T { // create a stack using the more derived type GenericStack <U> resultStack = new GenericStack <U>(); // run through the contents of this stack and // add those items which are of the derived type for (int i = 0; i < currentPos; i++) { if (dataArray[i] is U) { resultStack.Push((U)dataArray[i]); } } // return the result stack return(resultStack); }
public void TestGenericStackPop_ReferenceType() { GenericStack<int[]> stack = new GenericStack<int[]>(); stack.Push(new int[] { 1, 1, 1 }); stack.Push(new int[] { 2, 2, 2 }); stack.Push(new int[] { 3, 3, 3 }); stack.Push(new int[] { 4, 4, 4 }); stack.Push(new int[] { 5, 5, 5 }); stack.Push(new int[] { 6, 6, 6 }); Assert.AreEqual(6, stack.Count); CollectionAssert.AreEqual(new int[] { 6, 6, 6 }, stack.Pop()); }
public void SortbyModel() { GenericStack<ProtocolDroid> ProtocolStack = new GenericStack<ProtocolDroid>(); GenericStack<UtilityDroid> UtilityStack = new GenericStack<UtilityDroid>(); GenericStack<JanitorDroid> JanitorStack = new GenericStack<JanitorDroid>(); GenericStack<AstromechDroid> AstromechStack = new GenericStack<AstromechDroid>(); GenericQueue<Droid> DroidsQueue = new GenericQueue<Droid>(); foreach (IDroid droid in droidCollection) { if (droid is ProtocolDroid) { ProtocolStack.Push((ProtocolDroid)droid); } else if (droid is UtilityDroid) { UtilityStack.Push((UtilityDroid)droid); } else if (droid is JanitorDroid) { JanitorStack.Push((JanitorDroid)droid); } else if (droid is AstromechDroid) { AstromechStack.Push((AstromechDroid)droid); } } while (ProtocolStack != null) { DroidsQueue.Enqueue(ProtocolStack.Pop()); } while (UtilityStack != null) { DroidsQueue.Enqueue(UtilityStack.Pop()); } while (JanitorStack != null) { DroidsQueue.Enqueue(JanitorStack.Pop()); } while (AstromechStack != null) { DroidsQueue.Enqueue(AstromechStack.Pop()); } while (DroidsQueue != null) { int i =0; droidCollection[i] = DroidsQueue.Dequeue(); i++; } }
public void SortByModel() { //Create generic stacks for each model type GenericStack<Droid> protocolStack = new GenericStack<Droid>(); GenericStack<Droid> utilityStack = new GenericStack<Droid>(); GenericStack<Droid> janitorStack = new GenericStack<Droid>(); GenericStack<Droid> astromechStack = new GenericStack<Droid>(); //Create new generic queue GenericQueue<Droid> droidQueue = new GenericQueue<Droid>(); foreach (Droid droid in droidArray) { if (droid != null) { switch (droid.GetModel()) { case "Protocol": protocolStack.Push(droid); break; case "Utility": utilityStack.Push(droid); break; case "Janitor": janitorStack.Push(droid); break; case "Astromech": astromechStack.Push(droid); break; default: break; } } } //Get droids from each stack and enqueue them while (astromechStack.IsNotEmpty()) { droidQueue.Enqueue(astromechStack.Pop()); } while (janitorStack.IsNotEmpty()) { droidQueue.Enqueue(janitorStack.Pop()); } while (utilityStack.IsNotEmpty()) { droidQueue.Enqueue(utilityStack.Pop()); } while (protocolStack.IsNotEmpty()) { droidQueue.Enqueue(protocolStack.Pop()); } int i = 0; while (droidQueue.IsNotEmpty()) { droidArray[i] = droidQueue.Dequeue(); i++; } }
static void Main(string[] args) { Console.WriteLine("---Generic Stack Test---"); GenericStack<int> stack = new GenericStack<int>(0); stack.Push(1); stack.Push(2); stack.Push(3); while(!stack.IsEmpty()) { Console.WriteLine(stack.Contains(2)); Console.WriteLine(stack.Peek()); Console.WriteLine(stack.Pop()); } Console.WriteLine("---Generic Dequeue Test---"); GenericDequeue<int> dequeue = new GenericDequeue<int>(0); dequeue.AddToEnd(3); dequeue.AddToEnd(4); dequeue.AddToFront(2); dequeue.AddToFront(1); while (!dequeue.IsEmpty()) { Console.WriteLine(dequeue.Contains(3)); Console.WriteLine(dequeue.PeekFromEnd()); Console.WriteLine(dequeue.RemoveFromEnd()); Console.WriteLine(dequeue.Contains(3)); Console.WriteLine(dequeue.PeekFromFront()); Console.WriteLine(dequeue.RemoveFromFront()); } Console.WriteLine("---Lotto Game Test---"); LottoGame<int, string> lottoGame = new LottoGame<int, string>(new Combination<int, string>(1, 2, 3, "a", "b", "c")); var comb1 = new Combination<int, string>(5, 7, 9, "we", "asd", "rgd"); var comb2 = new Combination<int, string>(5, 7, 8, "we", "asd", "rgd"); var comb3 = new Combination<int, string>(5, 7, 9, "we", "asd", "rgd"); var comb4 = new Combination<int, string>(1, 7, 9, "we", "asd", "rgd"); var comb5 = new Combination<int, string>(5, 2, 9, "we", "b", "rgd"); if (lottoGame.AddUserCombination(comb1)) Console.WriteLine("Added combination {0}", comb1); else Console.WriteLine("Combination {0} already exists!", comb1); if (lottoGame.AddUserCombination(comb2)) Console.WriteLine("Added combination {0}", comb2); else Console.WriteLine("Combination {0} already exists!", comb2); if (lottoGame.AddUserCombination(comb3)) Console.WriteLine("Added combination {0}", comb3); else Console.WriteLine("Combination {0} already exists!", comb3); if (lottoGame.AddUserCombination(comb4)) Console.WriteLine("Added combination {0}", comb4); else Console.WriteLine("Combination {0} already exists!", comb4); if (lottoGame.AddUserCombination(comb5)) Console.WriteLine("Added combination {0}", comb5); else Console.WriteLine("Combination {0} already exists!", comb5); Console.WriteLine(); var lottoResult = lottoGame.Validate(); if (lottoResult.IsWinning) Console.WriteLine("This lotto game has a winner with {0} matching values!", lottoResult.MatchedNumbersCount); else Console.WriteLine("There is no winner in this lotto game!"); Console.ReadKey(); }
/// <summary> /// public method to Sort the droids into categories using a modified bucket sort /// </summary> public void SortIntoCategories() { //Create a generic stack for each type of droid, and pass in the droid type as the generic that will //come through on the stack class as T. GenericStack<ProtocolDroid> protocolStack = new GenericStack<ProtocolDroid>(); GenericStack<UtilityDroid> utilityStack = new GenericStack<UtilityDroid>(); GenericStack<JanitorDroid> janitorStack = new GenericStack<JanitorDroid>(); GenericStack<AstromechDroid> astromechStack = new GenericStack<AstromechDroid>(); //Create a queue to hold the droids as we pop them off the stack. GenericQueue<IDroid> categorizedDroidQueue = new GenericQueue<IDroid>(); //For each IDroid in the droidCollection foreach (IDroid droid in this.droidCollection) { //if the droid is not null we want to process it. If it is null we will go to the else if (droid != null) { //The testing of the droids must occur in this order. It must be done in the order of //most specific droid to least specific. //If we were to test a droid that IS of type Astromech against Utility BEFORE we test against //Astromech, it would pass and be put into the Utility stack and not the Astromech. That is why it //is important to test from most specific to least. //If the droid is an Astromech, push it on the astromech stack if (droid is AstromechDroid) { astromechStack.Push((AstromechDroid)droid); } //Else if it is a JanitorDroid, push it on the janitor stack else if (droid is JanitorDroid) { janitorStack.Push((JanitorDroid)droid); } //Do for Utility else if (droid is UtilityDroid) { utilityStack.Push((UtilityDroid)droid); } //Do for Protocol else if (droid is ProtocolDroid) { protocolStack.Push((ProtocolDroid)droid); } } //The droid we are trying to consider is null, break out of the loop. else { break; } } //Now that the droids are all in thier respective stacks we can do the work //of poping them off of the stacks and adding them to the queue. //It is required that they be popped off from each stack in this order so that they have //the correct order going into the queue. //This is a primer pop. It gets the first droid off the stack, which could be null if the stack is empty AstromechDroid currentAstromechDroid = astromechStack.Pop(); //While the droid that is popped off is not null while (currentAstromechDroid != null) { //Add the popped droid to the queue. categorizedDroidQueue.Enqueue(currentAstromechDroid); //Pop off the next droid for the loop test currentAstromechDroid = astromechStack.Pop(); } //See above method for Astromech. It is the same except for Janitor JanitorDroid currentJanitorDroid = janitorStack.Pop(); while (currentJanitorDroid != null) { categorizedDroidQueue.Enqueue(currentJanitorDroid); currentJanitorDroid = janitorStack.Pop(); } //See above method for Astromech. It is the same except for Utility UtilityDroid currentUtilityDroid = utilityStack.Pop(); while (currentUtilityDroid != null) { categorizedDroidQueue.Enqueue(currentUtilityDroid); currentUtilityDroid = utilityStack.Pop(); } //See above method for Astromech. It is the same except for Protocol ProtocolDroid currentProtocolDroid = protocolStack.Pop(); while (currentProtocolDroid != null) { categorizedDroidQueue.Enqueue(currentProtocolDroid); currentProtocolDroid = protocolStack.Pop(); } //Now that the droids have all been removed from the stacks and put into the queue //we need to dequeue them all and put them back into the original array. //Set a int counter to 0. int counter = 0; //This is a primer dequeue that will get the first droid out of the queue. IDroid iDroid = categorizedDroidQueue.Dequeue(); //While the dequeued droid is not null. while (iDroid != null) { //Add the droid to the droid collection using the int counter as the index this.droidCollection[counter] = iDroid; //increment the counter counter++; //dequeue the next droid off the queue so it can be used in the while condition iDroid = categorizedDroidQueue.Dequeue(); } //set the lenght of the collection to the value of the counter. It should be the same, but in case it changed. this.lengthOfCollection = counter; }