forked from ilhanaydintr/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Odev2_parantez.java
80 lines (71 loc) · 1.96 KB
/
Odev2_parantez.java
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
package yigit;
class Stack {
int top,N;
char []a;
boolean isEmpty() {
return top ==-1;
}
boolean full(){
return top==N-1;
}
Stack(int N) {
top = -1;
a = new char[N]; // Maximum size of Stack
this.N=N;
}
void push(char x) {
if (!full()) {
top++;
a[top] = x;
}
}
char pop() {
if (!isEmpty()) {
top=top-1;
char x = a[top+1];
return x;
}
return ' ';
}
char peek(){
return a[top];
}
}
public class odev2_parantez {
static boolean eslesme(char c1, char c2)
{
if (c1 == '(' && c2 == ')')
return true;
else if (c1 == '{' && c2 == '}')
return true;
else if (c1 == '[' && c2 == ']')
return true;
else
return false;
}
static boolean dengelimi(String exp)
{
/* Boş Yıgıt tanımla */
Stack st=new Stack(100);
for(int i=0;i<exp.length();i++) {
/*Eğer exp[i] parantez açma ise ekle*/
if (exp.charAt(i) == '{' || exp.charAt(i) == '(' || exp.charAt(i) == '[')
st.push(exp.charAt(i));
if (exp.charAt(i) == '}' || exp.charAt(i) == ')' || exp.charAt(i) == ']')
{
if (st.isEmpty())
return false;
else if ( !eslesme(st.pop(), exp.charAt(i)) )
return false;
}
}
return st.isEmpty();
}
public static void main(String[] args) {
String S= "{()}[]";
if (dengelimi(S))
System.out.println("Dengeli parantez ");
else
System.out.println("Parantezler dengeli değil ");
}
}