// sorted array by inorder DFS public T[] ToSortedArray() { T[] arr = new T[Count]; int i = 0; var stack = new Stack.Stack <TreeNode <T> >(); var node = Root; while (node != null || stack.Count != 0) { while (node != null && !IsEdgeSentinel(node)) { stack.Push(node); node = node.Left; } node = stack.Pop(); arr[i] = node.Data; ++i; node = node.Right; } return(arr); }
/*ToArray method cause some problem in Pex producing less tests*/ /* public override bool Equals(Stack<int> s1, Stack<int> s2) * { * int[] s1Array = s1.ToArray(); * int[] s2Array = s2.ToArray(); * * if (s1Array.Length != s2Array.Length) * return false; * * for (int i = 0; i < s1Array.Length; i++) * { * if (s1Array[i] != s2Array[i]) * return false; * } * * return true; * }*/ public override bool Equals(Stack.Stack <int> s1, Stack.Stack <int> s2) { if (s1 == null || s2 == null) { return(false); } return(s1.ToStringForInts().Equals(s2.ToStringForInts())); }
public override int GetHashCode(Stack.Stack <int> s) { int hash = 0; int[] sArray = s.ToArray(); for (int i = 0; i < sArray.Length; i++) { hash += i * sArray[i]; } return(hash); }
public static void Main() { Stack.Stack <int> stack = new Stack.Stack <int>(); while (true) { string cmd = Console.ReadLine(); if (cmd == "END") { break; } string[] cmdArgs = cmd.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); switch (cmdArgs[0]) { case "Push": foreach (var numStr in cmdArgs.Skip(1)) { stack.Push(int.Parse(numStr)); } break; case "Pop": try { stack.Pop(); } catch (InvalidOperationException) { Console.WriteLine("No elements"); } break; } } for (int i = 0; i < 2; i++) { foreach (var num in stack) { Console.WriteLine(num); } } }
/// <summary> /// Complexity of this code is O(N) /// </summary> /// <param name="expression"></param> /// <returns></returns> public static bool HasMatchingParans(string expression) { try { Stack.Stack <char> openPanransFound = new Stack.Stack <char>(); foreach (char expressionChar in expression) { if (openParans.Contains(expressionChar)) { openPanransFound.PUSH(expressionChar); } if (matchingOpens.ContainsKey(expressionChar)) { if (openPanransFound.GetSize() == 0) { return(false); } if (!matchingOpens.TryGetValue(expressionChar, out char openForTheCloseFound) || !openPanransFound.POP().Equals(openForTheCloseFound)) { return(false); } } } return(openPanransFound.IsEmpty()); } catch (Stack.StackOverflowException ex) { Console.WriteLine(ex); } catch (Stack.StackUnderflowException ex) { Console.WriteLine(ex); } return(false); }
public MinimumStack() { this.dataStack = new Stack.Stack <int>(); this.minimumStack = new Stack.Stack <int>(); }