3
3
4
4
/// <summary>
5
5
///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.
7
8
8
9
///MinStack: This custom stack keeps track of the minimum element so that getMin() can return the minimum in O(1) time.
9
10
///Next Greater Element: For each element in an array, the utility finds the next greater element on the right side using a stack.
13
14
///@author Mohit Singh
14
15
///@author <a href="https://github.com/mohit-gogitter">mohit-gogitter<a>
15
16
/// </summary>
16
- public class StackUtils
17
+
18
+ namespace DataStructures . Stack
17
19
{
18
- // 1. MinStack - Stack supporting push, pop, and retrieving minimum in O(1) time
19
- public class MinStack
20
+ public class StackUtils
20
21
{
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 > ( )
25
23
{
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
34
31
{
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 ( )
37
59
{
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 ;
39
93
}
40
94
}
41
95
96
+ // 2. Next Greater Element for each element in an array
42
97
/// <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.
44
99
/// </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 )
46
103
{
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 -- )
48
108
{
49
- int poppedValue = mainStack . Pop ( ) ;
50
- if ( poppedValue == minStack . Peek ( ) )
109
+ while ( stack . Count > 0 && stack . Peek ( ) <= nums [ i ] )
51
110
{
52
- minStack . Pop ( ) ;
111
+ stack . Pop ( ) ;
53
112
}
113
+ result [ i ] = stack . Count == 0 ? - 1 : stack . Peek ( ) ;
114
+ stack . Push ( nums [ i ] ) ;
54
115
}
116
+ return result ;
55
117
}
56
118
119
+ // 3. Balanced Parentheses Checker
57
120
/// <summary>
58
- /// Fetches the minimum item from the stack and returns it .
121
+ /// It checks if an expression has matching and balanced parentheses .
59
122
/// </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>
76
124
/// <returns>True or False</returns>
77
- public bool IsEmpty ( )
125
+ public static bool IsBalanced ( string expression )
78
126
{
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 ;
80
153
}
81
- }
82
154
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
+ }
93
159
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 )
95
166
{
96
- while ( stack . Count > 0 && stack . Peek ( ) <= nums [ i ] )
167
+ if ( stack . Count == 0 )
97
168
{
98
- stack . Pop ( ) ;
169
+ return ;
99
170
}
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 ) ;
102
174
}
103
- return result ;
104
- }
105
175
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 )
116
177
{
117
- if ( c == '(' || c == '{' || c == '[' )
178
+ if ( stack . Count == 0 )
118
179
{
119
- stack . Push ( c ) ;
180
+ stack . Push ( value ) ;
120
181
}
121
- else if ( c == ')' || c == '}' || c == ']' )
182
+ else
122
183
{
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 ) ;
126
187
}
127
188
}
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
- }
163
189
}
164
- }
190
+ }
0 commit comments