public void IsEmpty_AddingAnIntegerToTheStack_ReturnsFalse() { int numberToAdd = 6; _intStack.Push(numberToAdd); Assert.False(_intStack.IsEmpty()); }
// 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; } } } }
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()); }
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); }
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(); }
public void PushTest() { stack.Push(1); Assert.IsFalse(stack.IsEmpty()); }