public void Pop_Adding6ToStack_Returns6() { int numberToAdd = 6; _intStack.Push(6); Assert.AreEqual(numberToAdd, _intStack.Pop()); }
[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); }
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()); }
[Test] public void testShove() { GenericStack <string> stringStack = new GenericStack <string>(); stringStack.Shove("value 1", 0); Assert.AreEqual(1, stringStack.Count); Assert.AreEqual("value 1", stringStack.DeepPeek(0)); Assert.AreEqual("value 1", stringStack.Peek(0)); stringStack.Shove("value 4", 0); Assert.AreEqual(2, stringStack.Count); Assert.AreEqual("value 1", stringStack.DeepPeek(0)); Assert.AreEqual("value 4", stringStack.Peek(0)); stringStack.Pop(); stringStack.Shove("value 2", 1); Assert.AreEqual(2, stringStack.Count); // Assert.AreEqual("value 2", stringStack.DeepPeek(0)); // Assert.AreEqual("value 1", stringStack.DeepPeek(1)); // Assert.AreEqual("[value 1 value 2]", stringStack.ToString()); Assert.AreEqual("value 2", stringStack.DeepPeek(1)); Assert.AreEqual("value 1", stringStack.DeepPeek(0)); Assert.AreEqual(2, stringStack.Count); stringStack.Shove("value 3", 1); Assert.AreEqual(3, stringStack.Count); // Assert.AreEqual("[value 1 value 3 value 2]", stringStack.ToString()); Assert.AreEqual("value 2", stringStack.DeepPeek(2)); Assert.AreEqual("value 3", stringStack.DeepPeek(1)); Assert.AreEqual("value 1", stringStack.DeepPeek(0)); Assert.AreEqual("value 2", stringStack.Peek(0)); Assert.AreEqual("value 3", stringStack.Peek(1)); Assert.AreEqual("value 1", stringStack.Peek(2)); }
[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()); }
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(); }
// 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; } } } }
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()); }
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); }
public T Dequeue() { if (dequeueStack.IsEmpty()) { if (enqueueStack.IsEmpty()) { return(default(T)); } while (!enqueueStack.IsEmpty()) { dequeueStack.Push(enqueueStack.Pop()); } return(dequeueStack.Pop()); } return(dequeueStack.Pop()); }
[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()); }
/// 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); }
public void Pop_WhenStackIsEmpty_ShouldReturnIndexOutOfRangeException() { //Act var stack = new GenericStack <int>(1); stack.Pop(); //Assert Assert.Throws <IndexOutOfRangeException>( delegate { throw new IndexOutOfRangeException(); }); }
public void TestGenericStackPop() { 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.Pop()); Assert.AreEqual(3, stack.Count); }
public void TestGenericStackPop() { 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.Pop()); Assert.AreEqual(3, stack.Count); }
public object PopValueOfType(ValueType t) { // Return value of the specified type, or throw error switch (t) { case ValueType.INT: return(IntStack.Pop() as object); case ValueType.ACTOR: return(ActorStack.Pop() as object); default: throw new System.ArgumentException("Non-existant valuetype", "ValueType t"); } }
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()); }
static void Main(string[] args) { int size = 5; GenericStack <int> EnterThePile = new GenericStack <int>(size); EnterThePile.IsStackEmpty(); Console.WriteLine("\n"); EnterThePile.Push(5); EnterThePile.Push(2); EnterThePile.Push(3); EnterThePile.Push(8); EnterThePile.Push(1); EnterThePile.IsStackEmpty(); Console.WriteLine("\n"); EnterThePile.PrintStack(); Console.WriteLine("\n"); EnterThePile.PrintStack(); Console.WriteLine("\n"); EnterThePile.Push(10); Console.WriteLine("\n"); EnterThePile.Size(); EnterThePile.Top(); EnterThePile.Pop(); EnterThePile.ReverseStack(); EnterThePile.PrintStack(); Console.WriteLine("\n"); 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(); }
private static TreeNode GenerateTreeFromPostorder(int[] input) { TreeNode temp = null; TreeNode p = null; int i = input.Length - 1; TreeNode root = new TreeNode(); root.Value = input[i--]; if (input.Length == 1) { return(root); } var stack = new GenericStack <TreeNode>(input.Length); p = root; while (i >= 0) { if (input[i] < p.Value) { temp = new TreeNode(); temp.Value = input[i--]; p.Left = temp; stack.Push(p); p = temp; } else { if (input[i] > p.Value && (stack.IsEmpty() || input[i] < stack.Peek().Value)) { temp = new TreeNode(); temp.Value = input[i--]; p.Right = temp; p = temp; } else { p = stack.Pop(); } } } return(root); }
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(); }
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(); }
static void Main(string[] args) { // create a GenericStack<T> using the derived type GenericStack <VolvoC30> volvoStack = new GenericStack <VolvoC30>(); //// upcast the paramterized type //GenericStack<Car> carStack = volvoStack; // this won't compile //// push in a data item via the upcast instance //carStack.Push(new FordFiesta()); // pop the contents via the original instance // CAUTION - this would cause an exception because // although we are expecting a VolvoC30 object, we // are actually going to get a FordFiesta instead VolvoC30 dataItem = volvoStack.Pop(); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
/// <summary> /// Test the generic stack. /// </summary> public static void TestGenericStack() { // Create stack int size = 10; GenericStack <double> stack = new GenericStack <double>(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 { while (true) { // 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); }
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 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(); }
public void CheckPop(int[] array) { var stack = new GenericStack <int>(array); var element = stack.Pop(); Assert.That(element, Is.EqualTo(array[^ 1]));
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 PopTest() { stack.Push(1); stack.Pop(); Assert.IsTrue(stack.IsEmpty()); }
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++; } }
/// <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; }
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(); }
//method to organize droids by type public bool Organize() { GenericStack<AstromechDroid> astromechStack = new GenericStack<AstromechDroid>(); GenericStack<JanitorDroid> janitorStack = new GenericStack<JanitorDroid>(); GenericStack<UtilityDroid> utilityStack = new GenericStack<UtilityDroid>(); GenericStack<ProtocolDroid> protocolStack = new GenericStack<ProtocolDroid>(); GenericQueue<IDroid> droidQueue = new GenericQueue<IDroid>(); //Add droids to appropriate stack types for (int i = 0; i < lengthOfCollection; i++) { try { astromechStack.Add((AstromechDroid)droidCollection[i]); } catch { try { janitorStack.Add((JanitorDroid)droidCollection[i]); } catch { try { utilityStack.Add((UtilityDroid)droidCollection[i]); } catch { try { protocolStack.Add((ProtocolDroid)droidCollection[i]); } catch { return false; } } } } } //Add droids in order to the queue while (astromechStack.Head != null) { droidQueue.Add((IDroid)astromechStack.Pop()); } while (janitorStack.Head != null) { droidQueue.Add((IDroid)janitorStack.Pop()); } while (utilityStack.Head != null) { droidQueue.Add((IDroid)utilityStack.Pop()); } while (protocolStack.Head != null) { droidQueue.Add((IDroid)protocolStack.Pop()); } //Dequeue droids back into the array for (int i = 0; droidQueue.Tail != null; i++) { droidCollection[i] = (IDroid)droidQueue.Dequeue(); } return true; }
//public static void PrintFirstCarDetails(GenericStack<Car> carStack) { // Car myCar = carStack.Pop(); // Console.WriteLine("Car value popped: {0}", myCar); //} public static void PrintFirstCarDetails <T>(GenericStack <T> carStack) where T : Car { Car myCar = carStack.Pop(); Console.WriteLine("Car value popped: {0}", myCar); }
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()); }
private void button2_Click(object sender, EventArgs e) { stack.Pop(); }