public string ConvertInfixToPostfix(string infix) { StackUsingLL <int> staging = new StackUsingLL <int>(); StringBuilder result = new StringBuilder(); for (int i = 0; i < infix.Length; i++) { //If scanned character is an operand if (IsOperand(infix[i])) { result.Append(infix[i]); } //If scanned character is '(', push it to stack else if (infix[i] == '(') { staging.Push('('); } //If scanned character is ')', pop all operators from stack until '(' is found else if (infix[i] == ')') { while (!staging.IsEmpty() && staging.PeekTop() != '(') { result.Append(Convert.ToChar(staging.Pop())); } //Invalid string check if (!staging.IsEmpty() && staging.PeekTop() != '(') { return(null); } else { staging.Pop(); } } //If operator else { while (!staging.IsEmpty() && (CheckPrecedence(infix[i]) <= CheckPrecedence(Convert.ToChar(staging.PeekTop())))) { result.Append(Convert.ToChar(staging.Pop())); } staging.Push(infix[i]); } } while (!staging.IsEmpty()) { result.Append(Convert.ToChar(staging.Pop())); } return(result.ToString()); }
/// <summary> /// Method to convert an Infix expr to Prefix (Polish Notation). This method uses 2 stacks to hold the values. /// </summary> /// <param name="input"></param> /// <returns></returns> public string ConvertInfixToPrefix(string input) { StackUsingLL <int> staging = new StackUsingLL <int>(); StackUsingLL <int> result = new StackUsingLL <int>(); StringBuilder resultString = new StringBuilder(); for (int i = input.Length - 1; i >= 0; i--) { if (input[i] == ')') { staging.Push(input[i]); } else if (IsOperand(input[i])) { result.Push(input[i]); } else if (input[i] == '(') { while (staging.PeekTop() != ')') { result.Push(staging.Pop()); } staging.Pop(); } else { if (!staging.IsEmpty() && CheckPrecedence(input[i]) >= CheckPrecedence(staging.PeekTop())) { staging.Push(input[i]); } else if (!staging.IsEmpty() && CheckPrecedence(input[i]) < CheckPrecedence(staging.PeekTop())) { result.Push(staging.Pop()); staging.Push(input[i]); } else if (staging.IsEmpty()) { staging.Push(input[i]); } } } while (!staging.IsEmpty()) { result.Push(staging.Pop()); } while (!result.IsEmpty()) { resultString.Append(Convert.ToChar(result.Pop())); } return(resultString.ToString()); }
static int TwoStacks(int x, int[] a, int[] b) { var obj1 = new StackUsingLL <int>(); var sumStack = new StackUsingLL <int>(); for (int i = a.Length - 1; i >= 0; i--) { obj1.Push(a[i]); } var obj2 = new StackUsingLL <int>(); for (int i = b.Length - 1; i >= 0; i--) { obj2.Push(b[i]); } while (sumStack.SumStack() + obj1.PeekTop() <= x) { sumStack.Push(obj1.Pop()); } int countStack1 = sumStack.StackLength(); int sum = sumStack.SumStack(); int max = countStack1; int countStack2 = 0; while (obj2.StackLength() != 0 && sumStack.StackLength() > 0) { sum = sum + obj2.Pop(); countStack2++; if (sum > x) { sum = sum - sumStack.Pop(); countStack1--; } if (sum <= x && countStack1 + countStack2 > max) { max = countStack1 + countStack2; } } return(max); }
public string IsBalanced(string s) { StackUsingLL <int> obj = new StackUsingLL <int>(); for (int i = 0; i < s.Length; i++) { if (s[i] == '{' || s[i] == '[' || s[i] == '(') { obj.Push(s[i]); } else { if (obj.StackLength() > 0) { switch (s[i]) { case '}': if (obj.PeekTop() == '{') { obj.Pop(); } else { return("NO"); } break; case ']': if (obj.PeekTop() == '[') { obj.Pop(); } else { return("NO"); } break; case ')': if (obj.PeekTop() == '(') { obj.Pop(); } else { return("NO"); } break; } } else { return("NO"); } } } if (obj.StackLength() == 0) { return("YES"); } else { return("NO"); } }