File tree Expand file tree Collapse file tree 5 files changed +132
-190
lines changed Expand file tree Collapse file tree 5 files changed +132
-190
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ namespace DataStructures . Stack
3
+ {
4
+ /// <summary>
5
+ /// It checks if an expression has matching and balanced parentheses.
6
+ /// @author Mohit Singh
7
+ /// @author <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
8
+ /// </summary>
9
+ public class BalancedParenthesesChecker
10
+ {
11
+ private static readonly Dictionary < char , char > ParenthesesMap = new Dictionary < char , char > ( )
12
+ {
13
+ { '(' , ')' } ,
14
+ { '{' , '}' } ,
15
+ { '[' , ']' } ,
16
+ } ;
17
+ /// <summary>
18
+ /// This method checks if an expression has matching and balanced parentheses.
19
+ /// </summary>
20
+ /// <param name="expression">string containing parenthesis</param>
21
+ /// <returns>Boolean value</returns>
22
+ public static bool IsBalanced ( string expression )
23
+ {
24
+ Stack < char > stack = new Stack < char > ( ) ;
25
+ foreach ( char c in expression )
26
+ {
27
+ if ( c == '(' || c == '{' || c == '[' )
28
+ {
29
+ stack . Push ( c ) ;
30
+ }
31
+ else if ( c == ')' || c == '}' || c == ']' )
32
+ {
33
+ if ( stack . Count == 0 )
34
+ {
35
+ return false ;
36
+ }
37
+ char open = stack . Pop ( ) ;
38
+
39
+ if ( ! IsMatchingPair ( open , c ) )
40
+ {
41
+ return false ;
42
+ }
43
+ }
44
+ else
45
+ {
46
+ //since there are no other brackets, this is unreachable code
47
+ }
48
+ }
49
+ return stack . Count == 0 ;
50
+ }
51
+ private static bool IsMatchingPair ( char open , char close )
52
+ {
53
+ return ParenthesesMap . ContainsKey ( open ) && ParenthesesMap [ open ] == close ;
54
+ }
55
+ }
56
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ namespace DataStructures . Stack
3
+ {
4
+ /// <summary>
5
+ /// For each element in an array, the utility finds the next greater element on the right side using a stack.
6
+ /// @author Mohit Singh
7
+ /// @author <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
8
+ /// </summary>
9
+ public class NextGreaterElement
10
+ {
11
+ /// <summary>
12
+ /// For each element in an array, this method finds the next greater element on the right side using a stack.
13
+ /// </summary>
14
+ /// <param name="nums">Integer Array for which NextGreaterElement needs to be computed</param>
15
+ /// <returns>Integer array containing next greater elements</returns>
16
+ public static int [ ] FindNextGreaterElement ( int [ ] nums )
17
+ {
18
+ Stack < int > stack = new Stack < int > ( ) ;
19
+ int [ ] result = new int [ nums . Length ] ;
20
+
21
+ for ( int i = nums . Length - 1 ; i >= 0 ; i -- )
22
+ {
23
+ while ( stack . Count > 0 && stack . Peek ( ) <= nums [ i ] )
24
+ {
25
+ stack . Pop ( ) ;
26
+ }
27
+ result [ i ] = stack . Count == 0 ? - 1 : stack . Peek ( ) ;
28
+ stack . Push ( nums [ i ] ) ;
29
+ }
30
+ return result ;
31
+ }
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ namespace DataStructures . Stack
3
+ {
4
+ /// <summary>
5
+ /// Reverses the elements in a stack using recursion.
6
+ /// @author Mohit Singh
7
+ /// @author <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
8
+ /// </summary>
9
+ public class ReverseStack
10
+ {
11
+ /// <summary>
12
+ /// This method reverses the elements in a stack using recursion.
13
+ /// </summary>
14
+ /// <param name="stack">A Stack of Generic Type</param>
15
+ public static void Reverse < T > ( Stack < T > stack )
16
+ {
17
+ if ( stack . Count == 0 )
18
+ {
19
+ return ;
20
+ }
21
+ T temp = stack . Pop ( ) ;
22
+ Reverse ( stack ) ;
23
+ InsertAtBottom ( stack , temp ) ;
24
+ }
25
+
26
+ private static void InsertAtBottom < T > ( Stack < T > stack , T value )
27
+ {
28
+ if ( stack . Count == 0 )
29
+ {
30
+ stack . Push ( value ) ;
31
+ }
32
+ else
33
+ {
34
+ T temp = stack . Pop ( ) ;
35
+ InsertAtBottom ( stack , value ) ;
36
+ stack . Push ( temp ) ;
37
+ }
38
+ }
39
+ }
40
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -242,7 +242,9 @@ find more than one implementation for the same objective but using different alg
242
242
* [ Array-based Stack] ( ./DataStructures/Stack/ArrayBasedStack.cs )
243
243
* [ List-based Stack] ( ./DataStructures/Stack/ListBasedStack.cs )
244
244
* [ Queue-based Stack] ( ./DataStructures/Stack/QueueBasedStack.cs )
245
- * [ Stack Utils] ( ./DataStructures/Stack/StackUtils.cs )
245
+ * [ Next Greater Element] ( ./DataStructures/Stack/NextGreaterElement.cs )
246
+ * [ BalancedParenthesesChecker] ( ./DataStructures/Stack/BalancedParenthesesChecker.cs )
247
+ * [ Reverse Stack] ( ./DataStructures/Stack/ReverseStack.cs )
246
248
* [ Heap] ( ./DataStructures/Heap )
247
249
* [ Min-Max Heap] ( ./DataStructures/Heap/MinMaxHeap.cs )
248
250
* [ Binary Heap] ( ./DataStructures/Heap/BinaryHeap.cs )
You can’t perform that action at this time.
0 commit comments