public static bool AreParenthesisBalanced(String str) { MyStack <char> myStack = new SllStack <char>(); List <Char> startingParenthsis = new List <char>() { '[', '{', '(' }; List <Char> endingParenthsis = new List <char>() { ']', '}', ')' }; Dictionary <char, char> dictionary = new Dictionary <char, char>(); dictionary.Add(']', '['); dictionary.Add('}', '{'); dictionary.Add(')', '('); foreach (var ch in str) { if (startingParenthsis.Contains(ch)) { myStack.Push(ch); } else if (endingParenthsis.Contains(ch)) { if (!myStack.IsEmpty() && dictionary[ch].CompareTo(myStack.Peek()) == 0) { myStack.Pop(); } else { return(false); } } else { //discard other characters } } //at the end nothing should be left out on stack return(myStack.Count == 0); }
/// <summary> /// This prints greater element distance on left side. If no grater found, then Ith index is its span /// </summary> public static void PrintStockSpan(int[] arr) { if (arr.Length == 0) { return; } MyStack <int> myStack = new SllStack <int>(); myStack.Push(arr.Length - 1); // Pushing index, not the element for (int i = arr.Length - 2; i >= 0; i--) { int currentItem = arr[i]; if (!myStack.IsEmpty()) { int peekedItem = arr[myStack.Peek()]; while (currentItem > peekedItem) //we found greter on left for some item { Console.WriteLine("For item " + peekedItem + ", stock span is: " + (myStack.Peek() - i)); myStack.Pop(); //Remove that processed item if (!myStack.IsEmpty()) { peekedItem = arr[myStack.Peek()]; //there can be many small than current item in stack } else { break; } } } //Add current item index to stack myStack.Push(i); } //Once we are done with this, all items in stack doesn't have greater on right. while (!myStack.IsEmpty()) { Console.WriteLine("For item " + arr[myStack.Peek()] + ", stock span last is: " + (myStack.Peek() + 1)); myStack.Pop(); } }
public void Run() { ArrayStack <int> arrayStack = new ArrayStack <int>(10); arrayStack.Push(2); arrayStack.Push(5); arrayStack.Push(1); arrayStack.Push(6); foreach (var item in arrayStack) { Console.WriteLine("Item:" + item); } Console.WriteLine("---------"); SllStack <int> sllStack = new SllStack <int>(); //no need of any capacity sllStack.Push(2); sllStack.Push(5); sllStack.Push(1); sllStack.Push(6); foreach (var item in sllStack) { Console.WriteLine("Item:" + item); } Console.WriteLine("---------"); DllStack <int> dllStack = new DllStack <int>(); //no need of any capacity dllStack.Push(1); Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData()); dllStack.Push(2); Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData()); dllStack.Push(3); Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData()); dllStack.Push(4); Console.WriteLine("Middle:" + dllStack.GetMiddleNodeData()); foreach (var item in dllStack) { Console.WriteLine("Item:" + item); } Console.WriteLine("---------"); SpecialStack <int> specialStack = new SpecialStack <int>(); //no need of any capacity specialStack.Push(2); specialStack.Push(5); specialStack.Push(1); specialStack.Push(6); while (!specialStack.IsEmpty()) { Console.WriteLine("Item:" + specialStack.Peek() + ", min:" + specialStack.GetMin() + ", max:" + specialStack.GetMax()); specialStack.Pop(); } Console.WriteLine("---------"); Console.WriteLine("Balanced: " + StackAlgos.AreParenthesisBalanced("[(1*2)+{5+6}")); Console.WriteLine("---------"); StackAlgos.ReverseStackUsingStackOperationsOnly(arrayStack); Console.WriteLine("After reverse"); foreach (var item in arrayStack) { Console.WriteLine("Item:" + item); } Console.WriteLine("---------"); StackAlgos.PrintNextGreaterElementInRightSideForAll(new int[] { 10, 4, 5, 90, 120, 80 }); Console.WriteLine("---------"); StackAlgos.PrintStockSpan(new int[] { 10, 4, 5, 90, 120, 80 }); Console.WriteLine("---------"); StackAlgos.MaxAreaRectangleFromHistogram(new int[] { 6, 2, 5, 4, 5, 1, 6 }); Console.WriteLine("---------"); int count = StackAlgos.MinNumberOfReversalNeededToMakeExpressionValid("}{{}}{{{"); Console.WriteLine("Number of reversal nedded:" + count); Console.WriteLine("---------"); int disk = 4; StackAlgos.TowerOfHanoi(disk); Console.WriteLine("---------"); Console.ReadKey(); }