public int Calculate(string str) { char[] ch = str.ToCharArray(); int value = 0; LBStack <int> st = new LBStack <int>(); foreach (char c in ch) { if (c == '+' || c == '-' || c == '*' || c == '/') { int c1 = st.Pop(); int c2 = st.Pop(); switch (c) { case '+': value = c2 + c1; break; case '-': value = c2 - c1; break; case '*': value = c2 * c1; break; case '/': value = c2 / c1; break; default: break; } st.Push(value); } else { st.Push(Convert.ToInt32(c.ToString())); } } return(value); }
public IDictionary <int, int> GetNextGreaterElement() { LBStack <int> st = new LBStack <int>(); foreach (int i in items) { st.Push(i); } while (!st.IsEmpty()) { int item = st.Peek(); if (st.top == 0 || st.top == items.Length - 1) { st.Pop(); data.Add(item, -1); } else { st.Pop(); data.Add(item, max); } max = item < max ? max : item; } return(data); }
public void Enqueue(int ele) { while (!st1.IsEmpty()) { st2.Push(st1.Pop()); } st1.Push(ele); while (!st2.IsEmpty()) { st1.Push(st2.Pop()); } }
public static bool Validate(string str) { LBStack <char> st = new LBStack <char>(); map.Add('}', '{'); map.Add(')', '('); map.Add(']', '['); char[] ch = str.ToCharArray(); foreach (char c in ch) { string s = c.ToString(); switch (s) { case "{": case "(": case "[": st.Push(c); break; case "}": case ")": case "]": string popped2 = st.Pop().ToString(); if (map[c].ToString() != popped2) { return(false); } break; default: break; } } return(st.IsEmpty()); }
public int GetMaxWaterTrapped() { int i = 0; int total = 0; while (i < items.Length) { if (st.IsEmpty() || items[i] < items[st.Peek()]) { st.Push(i); } else { while (!st.IsEmpty() && items[i] >= items[st.Peek()]) { int top = st.Pop(); int height = Math.Min(items[i], items[st.Peek()]) - items[top]; int width = st.IsEmpty() ? 0 : i - 1 - st.Peek(); total = total + (height * width); } } i++; } return(total); }
private void calculateArea(int[] items, LBStack <int> st, int index) { int pop_index = st.Pop(); int lower_bound = st.IsEmpty() ? -1 : st.Peek(); int area = items[pop_index] * ((index - 1) - lower_bound); UpdateArea(area); }