-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07. Stack.java
More file actions
207 lines (156 loc) · 5.68 KB
/
07. Stack.java
File metadata and controls
207 lines (156 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/// Name : Soumik Deb Niloy
/// Sec : 04
/// ID : 20301207
/// LAB Assignment- 04
////////////////////////////////////////////////////////////// Task 01: Stack Using Array //////////////////////////////////////////////////////////////
public class Main {
public static String paranthesesBalance(String s_1){
Arraystack a_1=new Arraystack();
for(int i=0;i<s_1.length();i++){
char c = s_1.charAt(i);
if (c=='[' || c=='{' || c=='('){
a_1.push(c);
}else if ((c==']' || c=='}' || c==')') && a_1.size!=0) {
if (((char) a_1.peek()=='(' && c==')') || ((char) a_1.peek()=='{' && c=='}') || ((char) a_1.peek()=='[' && c==']')){
a_1.pop();
}else {
return "This expression is NOT correct"+'\n'+"Error at charatcer #"+(s_1.indexOf((char)a_1.peek())+1)+". "+a_1.peek()+" not closed.";
}
}else{
if (c==']' || c=='}' || c==')' && a_1.peek()==null){
return "Error at character #"+(s_1.indexOf(c)+1)+". "+c+" not opened";
}
}
}
if (a_1.size==0){
return "This expression is correct.";
}else {
return "This expression is NOT correct.";
}
}
public static void main(String[] args){
System.out.println("////////// OUTPUT 01 //////////");
String s_1="1+2*(3/4)";
System.out.println(paranthesesBalance(s_1));
System.out.println("\n");
System.out.println("////////// OUTPUT 02 //////////");
String s_2="1+2*[3*3+{4–5(6(7/8/9)+10)–11+(12*8)]+14";
System.out.println(paranthesesBalance(s_2));
System.out.println("\n");
System.out.println("////////// OUTPUT 03 //////////");
String s_3="1+2*[3*3+{4–5(6(7/8/9)+10)}–11+(12*8)/{13+13}]+14";
System.out.println(paranthesesBalance(s_3));
System.out.println("\n");
System.out.println("////////// OUTPUT 04 //////////");
String s_4="1+2]*[3*3+{4–5(6(7/8/9)+10)–11+(12*8)]+14";
System.out.println(paranthesesBalance(s_4));
}
}
class Arraystack{
Object [] a;
int size;
public Arraystack(){
a = new Object[100];
size =0;
}
public void push(Object e){
if(size==a.length){
return;
}
a[size]=e;
size++;
}
public Object peek(){
if (size==0){
return null;
}
Object val = a[size-1];
return val;
}
public Object pop(){
if(size!=0){
Object val = a[size-1];
a[size-1]=null;
size--;
return val;
}else {
return null;
}
}
}
////////////////////////////////////////////////////////////// Task 02: Stack Using Linked List //////////////////////////////////////////////////////////////
public class Main {
public static String paranthesesBalance(String s_1){
Liststack a_1=new Liststack();
for(int i=0;i<s_1.length();i++){
char c = s_1.charAt(i);
if (c=='[' || c=='{' || c=='('){
a_1.push(c);
}else if ((c==']' || c=='}' || c==')') && a_1.head!=null) {
if (((char) a_1.peek()=='(' && c==')') || ((char) a_1.peek()=='{' && c=='}') || ((char) a_1.peek()=='[' && c==']')){
a_1.pop();
}else {
return "This expression is NOT correct"+'\n'+"Error at charatcer #"+(s_1.indexOf((char)a_1.peek())+1)+". "+a_1.peek()+" not closed.";
}
}else{
if (c==']' || c=='}' || c==')' && a_1.peek()==null){
return "Error at character #"+(s_1.indexOf(c)+1)+". "+c+" not opened";
}
}
}
if (a_1.head==null){
return "This expression is correct.";
}else {
return "This expression is NOT correct.";
}
}
public static void main(String[] args){
System.out.println("////////// OUTPUT 01 //////////");
String s_1="1+2*(3/4)";
System.out.println(paranthesesBalance(s_1));
System.out.println("\n");
System.out.println("////////// OUTPUT 02 //////////");
String s_2="1+2*[3*3+{4–5(6(7/8/9)+10)–11+(12*8)]+14";
System.out.println(paranthesesBalance(s_2));
System.out.println("\n");
System.out.println("////////// OUTPUT 03 //////////");
String s_3="1+2*[3*3+{4–5(6(7/8/9)+10)}–11+(12*8)/{13+13}]+14";
System.out.println(paranthesesBalance(s_3));
System.out.println("\n");
System.out.println("////////// OUTPUT 04 //////////");
String s_4="1+2]*[3*3+{4–5(6(7/8/9)+10)–11+(12*8)]+14";
System.out.println(paranthesesBalance(s_4));
}
}
class Node{
Object element;
Node next;
public Node (Object e, Node n){
element = e;
next = n;
}
}
class Liststack{
Node head;
public void push(Object e){
Node n=new Node(e,head);
head = n;
}
public Object peek(){
if (head!=null){
return head.element;
}else{
return null;
}
}
public Object pop(){
if(head!=null){
Node remove = head;
head = head.next;
remove.next = null;
return remove.element;
}else {
return null;
}
}
}