-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q26.cpp
131 lines (109 loc) · 2.13 KB
/
Q26.cpp
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
/*
Aledutron
SPPU 2019 SE DSL Lab
SPPU Computer Engineering Second Year (SE) Data Structure Lab (DSL) / Fundamentals of Data Structures (FDS) Assignments (2019 Pattern)
Youtube DSL / FDS Playlist Link: https://youtube.com/playlist?list=PLlShVH4JA0osUGQB95eJ8h5bTTzJO89vz&si=u12IYwo93Z7RU4e8
Problem Statement:
Group-D\Q26.cpp
In any language program mostly syntax error occurs due to unbalancing delimiter such as (),{},[]. Write C++ program using stack to check whether given expression is well parenthesized or not.
Explaination Video Link: https://www.youtube.com/watch?v=265DLjJRuG8&list=PLlShVH4JA0osUGQB95eJ8h5bTTzJO89vz&index=13&pp=iAQB
*/
#include <iostream>
using namespace std;
class Stack
{
public:
const static int size = 100;
int top = -1;
char array[size];
void push(char x)
{
if (top == size - 1)
{
cout << "Stack Overflow!!";
return;
}
array[++top] = x;
}
char pop()
{
if (top == -1)
{
cout << "Stack Underflow!!";
return -1;
}
return array[top--];
}
void display()
{
if (top == -1)
{
cout << "Stack is Empty";
}
cout << "Stack contains: ";
for (int i = 0; i <= top; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
char peek()
{
return array[top];
}
bool isEmpty()
{
if (top == -1)
{
return 1;
}
else
{
return 0;
}
}
};
int main()
{
Stack brackets;
string s;
cout << "Enter the parenthesis to check balancing: ";
cin >> s;
bool is_balanced = true;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(' or s[i] == '{' or s[i] == '[')
{
brackets.push(s[i]);
}
else if (s[i] == ')' or s[i] == '}' or s[i] == ']')
{
if (brackets.isEmpty())
{
is_balanced = false;
break;
}
char last_bracket = brackets.peek();
if (s[i] == ')' and last_bracket == '(')
{
brackets.pop();
}
else if (s[i] == '}' and last_bracket == '{')
{
brackets.pop();
}
else if (s[i] == ']' and last_bracket == '[')
{
brackets.pop();
}
}
}
if (not brackets.isEmpty())
{
is_balanced = false;
}
if (is_balanced)
cout << s << " is balanced." << endl;
else
cout << s << " is not balanced." << endl;
}