File tree Expand file tree Collapse file tree 4 files changed +87
-1
lines changed
Data-Structures/STACKS/MISC-STACKS Expand file tree Collapse file tree 4 files changed +87
-1
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ public class BracketsBalancedOrNot
3
+ {
4
+ public static void main (String [] args )
5
+ {
6
+ Scanner in = new Scanner (System .in );
7
+ String expression = in .nextLine ();
8
+ in .close ();
9
+ if (BalancedOrNot (expression ))
10
+ {
11
+ System .out .println ("BRACKET BALANCED" );
12
+ }
13
+ else
14
+ {
15
+ System .out .println ("BRACKET NOT BALANCED" );
16
+ }
17
+ }
18
+ private static char reverseBracket (char ch )
19
+ {
20
+ switch (ch )
21
+ {
22
+ case ')' :
23
+ return '(' ;
24
+ case ']' :
25
+ return '[' ;
26
+ case '}' :
27
+ return '{' ;
28
+ default :
29
+ return '0' ;
30
+ }
31
+ }
32
+ //this method will throw empty stack exception
33
+ private static boolean BalancedOrNot (String expression ) throws EmptyStackException
34
+ {
35
+ Stack <Character > S = new Stack <Character >();
36
+ try
37
+ {
38
+ for (int i =0 ;i <expression .length ();i ++)
39
+ {
40
+ char ch = expression .charAt (i );
41
+ if (ch =='(' || ch =='[' || ch =='{' )
42
+ {
43
+ S .push (ch );
44
+ }
45
+ else if (ch ==')' || ch ==']' || ch =='}' )
46
+ {
47
+ char rev = reverseBracket (ch );
48
+ if (S .empty ())
49
+ {
50
+ throw new EmptyStackException ();
51
+ }
52
+ if (rev == S .peek ())
53
+ {
54
+ S .pop ();
55
+ }
56
+ else
57
+ {
58
+ return false ;
59
+ }
60
+ }
61
+ }
62
+ if (S .empty ())
63
+ {
64
+ return true ;
65
+ }
66
+ }
67
+ catch (Exception e )
68
+ {
69
+ System .out .println ("STACK IS EMPTY" );
70
+ }
71
+ return false ;
72
+ }
73
+ }
Original file line number Diff line number Diff line change 32
32
* UNROLLED LINKED LIST
33
33
* SKIP LIST
34
34
* INBUILT LISTS
35
+ * [ LINKED LIST]
35
36
* [ ARRAYLIST] ( Data-Structures/LISTS/arrayList.java )
36
37
* [ VECTORS] ( Data-Structures/LISTS/vectors.java )
37
38
* MISC
46
47
* INBUILT STACKS
47
48
* [ Stack class] ( Data-Structures/STACKS/INBUILT-STACK/Stacks.java )
48
49
* MISC STACKS
50
+ * [ Given expression have balanced brackets or not] ( Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java )
49
51
50
52
#### QUEUES
51
53
Original file line number Diff line number Diff line change @@ -111,6 +111,8 @@ Indexer for Data Structures Lover
111
111
* implementation
112
112
* [ C++] ( C++/Data-Structures/LISTS/VECTORS/Main.cpp )
113
113
114
+ #### LINKED LIST
115
+
114
116
#### LIST PY
115
117
116
118
* blog
@@ -154,6 +156,13 @@ Indexer for Data Structures Lover
154
156
* implementation
155
157
* [ C] ( C/Data-Structures/STACKS/MISC-STACKS/minimum_bracket_reversal_for_balanced_expression.c )
156
158
159
+ #### Given expression has balanced brackets or not
160
+
161
+ * blog
162
+ * docs
163
+ * implementation
164
+ * [ JAVA] ( Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java )
165
+
157
166
#### Postfix Evaluation
158
167
159
168
* blog
Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ This page contains the complexities of different algorithms in this repository.
83
83
* [ UNROLLED LINKED LIST] ( #unrolled-linked-list )
84
84
* [ SKIP LIST] ( #skip-list )
85
85
* INBUILT LISTS
86
+ * LINKED LIST
86
87
* [ ARRAYLISTS] ( #arraylists-(JAVA) )
87
88
* [ VECTORS] ( #vectors-(C++JAVA) )
88
89
* MISC
@@ -93,8 +94,9 @@ This page contains the complexities of different algorithms in this repository.
93
94
* [ INBUILT STACK] ( #inbuilt-stack )
94
95
* JAVA
95
96
* C++
97
+ * TWO WAY STACK
96
98
* [ MISC STACKS] ( #misc-stacks )
97
- * TWO WAY STACK
99
+ * [ BracketsBalancedOrNot ]
98
100
* QUEUES
99
101
* SIMPLE QUEUE
100
102
* FIXED ARRAY SIMPLE QUEUE
You can’t perform that action at this time.
0 commit comments