Skip to content

Commit ae19f20

Browse files
Modified StackUtils.cs as per Codacy code analysis report
1 parent 610a77d commit ae19f20

File tree

1 file changed

+140
-114
lines changed

1 file changed

+140
-114
lines changed

DataStructures/Stack/StackUtils.cs

Lines changed: 140 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
/// <summary>
55
///This is a stack-based utility in C-Sharp that can be used by other developers in their solutions.
6-
///This utility provides commonly used operations related to stacks, which can be beneficial for solving various problems such as expression evaluations, balanced parentheses, or maintaining a history of operations.
6+
///This utility provides commonly used operations related to stacks, which can be beneficial for solving
7+
///various problems such as expression evaluations, balanced parentheses, or maintaining a history of operations.
78

89
///MinStack: This custom stack keeps track of the minimum element so that getMin() can return the minimum in O(1) time.
910
///Next Greater Element: For each element in an array, the utility finds the next greater element on the right side using a stack.
@@ -13,152 +14,177 @@
1314
///@author Mohit Singh
1415
///@author <a href="https://github.com/mohit-gogitter">mohit-gogitter<a>
1516
/// </summary>
16-
public class StackUtils
17+
18+
namespace DataStructures.Stack
1719
{
18-
// 1. MinStack - Stack supporting push, pop, and retrieving minimum in O(1) time
19-
public class MinStack
20+
public class StackUtils
2021
{
21-
private Stack<int> mainStack;
22-
private Stack<int> minStack;
23-
24-
public MinStack()
22+
private static readonly Dictionary<char, char> parenthesesMap = new Dictionary<char, char>()
2523
{
26-
mainStack = new Stack<int>();
27-
minStack = new Stack<int>();
28-
}
29-
/// <summary>
30-
/// Adds an item on top of the stack.
31-
/// </summary>
32-
/// <param name="value">Item to be added on top of stack.</param>
33-
public void Push(int value)
24+
{ '(', ')' },
25+
{ '{', '}' },
26+
{ '[', ']' }
27+
};
28+
29+
// 1. MinStack - Stack supporting push, pop, and retrieving minimum in O(1) time
30+
public class MinStack
3431
{
35-
mainStack.Push(value);
36-
if (minStack.Count == 0 || value <= minStack.Peek())
32+
private readonly Stack<int> mainStack;
33+
private readonly Stack<int> minStack;
34+
35+
36+
37+
public MinStack()
38+
{
39+
mainStack = new Stack<int>();
40+
minStack = new Stack<int>();
41+
}
42+
/// <summary>
43+
/// Adds an item on top of the stack.
44+
/// </summary>
45+
/// <param name="value">Item to be added on top of stack.</param>
46+
public void Push(int value)
47+
{
48+
mainStack.Push(value);
49+
if (minStack.Count == 0 || value <= minStack.Peek())
50+
{
51+
minStack.Push(value);
52+
}
53+
}
54+
55+
/// <summary>
56+
/// Removes an item from top of the stack.
57+
/// </summary>
58+
public void Pop()
3759
{
38-
minStack.Push(value);
60+
if (mainStack.Count > 0)
61+
{
62+
int poppedValue = mainStack.Pop();
63+
if (poppedValue == minStack.Peek())
64+
{
65+
minStack.Pop();
66+
}
67+
}
68+
}
69+
70+
/// <summary>
71+
/// Fetches the minimum item from the stack and returns it.
72+
/// </summary>
73+
/// <returns>minimum item from the stack</returns>
74+
public int GetMin()
75+
{
76+
return minStack.Count == 0 ? int.MaxValue : minStack.Peek();
77+
}
78+
/// <summary>
79+
/// Removes an item from top of the stack and returns it.
80+
/// </summary>
81+
/// <returns>item on top of stack.</returns>
82+
public int Top()
83+
{
84+
return mainStack.Count == 0 ? -1 : mainStack.Peek();
85+
}
86+
/// <summary>
87+
/// Checks whether the stack is empty. Returns True if found empty else False.
88+
/// </summary>
89+
/// <returns>True or False</returns>
90+
public bool IsEmpty()
91+
{
92+
return mainStack.Count == 0;
3993
}
4094
}
4195

96+
// 2. Next Greater Element for each element in an array
4297
/// <summary>
43-
/// Removes an item from top of the stack.
98+
/// For each element in an array, the utility finds the next greater element on the right side using a stack.
4499
/// </summary>
45-
public void Pop()
100+
/// <param name="nums">Integer Array for which NextGreaterElement needs to be computed</param>
101+
/// <returns>Interger Array</returns>
102+
public static int[] NextGreaterElement(int[] nums)
46103
{
47-
if (mainStack.Count > 0)
104+
Stack<int> stack = new Stack<int>();
105+
int[] result = new int[nums.Length];
106+
107+
for (int i = nums.Length - 1; i >= 0; i--)
48108
{
49-
int poppedValue = mainStack.Pop();
50-
if (poppedValue == minStack.Peek())
109+
while (stack.Count > 0 && stack.Peek() <= nums[i])
51110
{
52-
minStack.Pop();
111+
stack.Pop();
53112
}
113+
result[i] = stack.Count == 0 ? -1 : stack.Peek();
114+
stack.Push(nums[i]);
54115
}
116+
return result;
55117
}
56118

119+
// 3. Balanced Parentheses Checker
57120
/// <summary>
58-
/// Fetches the minimum item from the stack and returns it.
121+
/// It checks if an expression has matching and balanced parentheses.
59122
/// </summary>
60-
/// <returns>minimum item from the stack</returns>
61-
public int GetMin()
62-
{
63-
return minStack.Count == 0 ? int.MaxValue : minStack.Peek();
64-
}
65-
/// <summary>
66-
/// Removes an item from top of the stack and returns it.
67-
/// </summary>
68-
/// <returns>item on top of stack.</returns>
69-
public int Top()
70-
{
71-
return mainStack.Count == 0 ? -1 : mainStack.Peek();
72-
}
73-
/// <summary>
74-
/// Checks whether the stack is empty. Returns True if found empty else False.
75-
/// </summary>
123+
/// <param name="expression">string containing parenthesis</param>
76124
/// <returns>True or False</returns>
77-
public bool IsEmpty()
125+
public static bool IsBalanced(string expression)
78126
{
79-
return mainStack.Count == 0;
127+
Stack<char> stack = new Stack<char>();
128+
foreach (char c in expression)
129+
{
130+
if (c == '(' || c == '{' || c == '[')
131+
{
132+
stack.Push(c);
133+
}
134+
else if (c == ')' || c == '}' || c == ']')
135+
{
136+
if (stack.Count == 0)
137+
{
138+
return false;
139+
}
140+
char open = stack.Pop();
141+
142+
if (!IsMatchingPair(open, c))
143+
{
144+
return false;
145+
}
146+
}
147+
else
148+
{
149+
//since there are no other brackets, this is unreachable code
150+
}
151+
}
152+
return stack.Count == 0;
80153
}
81-
}
82154

83-
// 2. Next Greater Element for each element in an array
84-
/// <summary>
85-
/// For each element in an array, the utility finds the next greater element on the right side using a stack.
86-
/// </summary>
87-
/// <param name="nums">Integer Array for which NextGreaterElement needs to be computed</param>
88-
/// <returns>Interger Array</returns>
89-
public static int[] NextGreaterElement(int[] nums)
90-
{
91-
Stack<int> stack = new Stack<int>();
92-
int[] result = new int[nums.Length];
155+
private static bool IsMatchingPair(char open, char close)
156+
{
157+
return parenthesesMap.ContainsKey(open) && parenthesesMap[open] == close;
158+
}
93159

94-
for (int i = nums.Length - 1; i >= 0; i--)
160+
// 4. Reverse a Stack
161+
/// <summary>
162+
/// This utility reverses the elements in a stack using recursion.
163+
/// </summary>
164+
/// <param name="stack">A Stack of Generic Type</param>
165+
public static void ReverseStack<T>(Stack<T> stack)
95166
{
96-
while (stack.Count > 0 && stack.Peek() <= nums[i])
167+
if (stack.Count == 0)
97168
{
98-
stack.Pop();
169+
return;
99170
}
100-
result[i] = stack.Count == 0 ? -1 : stack.Peek();
101-
stack.Push(nums[i]);
171+
T temp = stack.Pop();
172+
ReverseStack(stack);
173+
InsertAtBottom(stack, temp);
102174
}
103-
return result;
104-
}
105175

106-
// 3. Balanced Parentheses Checker
107-
/// <summary>
108-
/// It checks if an expression has matching and balanced parentheses.
109-
/// </summary>
110-
/// <param name="expression">string containing parenthesis</param>
111-
/// <returns>True or False</returns>
112-
public static bool IsBalanced(string expression)
113-
{
114-
Stack<char> stack = new Stack<char>();
115-
foreach (char c in expression)
176+
private static void InsertAtBottom<T>(Stack<T> stack, T value)
116177
{
117-
if (c == '(' || c == '{' || c == '[')
178+
if (stack.Count == 0)
118179
{
119-
stack.Push(c);
180+
stack.Push(value);
120181
}
121-
else if (c == ')' || c == '}' || c == ']')
182+
else
122183
{
123-
if (stack.Count == 0) return false;
124-
char open = stack.Pop();
125-
if (!IsMatchingPair(open, c)) return false;
184+
T temp = stack.Pop();
185+
InsertAtBottom(stack, value);
186+
stack.Push(temp);
126187
}
127188
}
128-
return stack.Count == 0;
129-
}
130-
131-
private static bool IsMatchingPair(char open, char close)
132-
{
133-
return (open == '(' && close == ')') ||
134-
(open == '{' && close == '}') ||
135-
(open == '[' && close == ']');
136-
}
137-
138-
// 4. Reverse a Stack
139-
/// <summary>
140-
/// This utility reverses the elements in a stack using recursion.
141-
/// </summary>
142-
/// <param name="stack">A Stack of Generic Type</param>
143-
public static void ReverseStack<T>(Stack<T> stack)
144-
{
145-
if (stack.Count == 0) return;
146-
T temp = stack.Pop();
147-
ReverseStack(stack);
148-
InsertAtBottom(stack, temp);
149-
}
150-
151-
private static void InsertAtBottom<T>(Stack<T> stack, T value)
152-
{
153-
if (stack.Count == 0)
154-
{
155-
stack.Push(value);
156-
}
157-
else
158-
{
159-
T temp = stack.Pop();
160-
InsertAtBottom(stack, value);
161-
stack.Push(temp);
162-
}
163189
}
164-
}
190+
}

0 commit comments

Comments
 (0)