-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_Class_Implementation
More file actions
98 lines (86 loc) · 1.32 KB
/
Stack_Class_Implementation
File metadata and controls
98 lines (86 loc) · 1.32 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
#include <iostream>
class Stack {
private:
char ch[100];
int TOP;
public:
Stack() {
ch[0] = '\0';
TOP = -1;
}
void push(char ch) {
if (TOP < 99) {
this->ch[++TOP] = ch;
}
}
void pop() {
if (TOP >= 0) {
TOP--;
}
}
char top() const{
if (TOP >= 0) return ch[TOP];
}
bool empty()const {
return TOP <0;
}
};
int main() {
Stack s1;
std::string expr;
std::cout << "Enter expression: ";
std::cin >> expr;
int len = expr.length();
bool flag = true;
for (int i = 0; i < len; i++) {
if (expr[i] == '(' || expr[i] == '{' || expr[i] == '[') {
s1.push(expr[i]);
}
else if (expr[i] == ')' || expr[i] == '}' || expr[i] == ']') {
if (s1.empty()) {
flag = false;
break;
}
else if (expr[i] == ')') {
if (s1.top() == '(') {
s1.pop();
continue;
}
else {
flag = false;
break;
}
}
else if (expr[i] == '}') {
if (s1.top() == '{') {
s1.pop();
continue;
}
else {
flag = false;
break;
}
}
else if (expr[i] == ']') {
if (s1.top() == '[') {
s1.pop();
continue;
}
else {
flag = false;
break;
}
}
if (s1.empty()) {
flag = false;
break;
}
}
}
if (flag && s1.empty()) {
std::cout << "Brackets Balanced";
}
else {
std::cout << "Brackets not Balanced";
}
}