-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_balanced_parantheses.cpp
180 lines (149 loc) · 3.64 KB
/
check_balanced_parantheses.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
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
/****************************************************************************
File name: check_balanced_parantheses.cpp
Author: babajr
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
struct stack
{
int size; // size of stack.
int top; // index always pointing to the top of the stack.
// int *s; // pointer to dynamic array.
char *s;
};
typedef struct stack stack;
/*
API to add element to the stack at the top.
*/
void push(stack *st, char value)
{
// Check if stack is full or not.
if(st->top == st->size-1) // -1 as stack index starts from 0.
{
printf("Stack Overflow\n");
}
else
{
// point the top to next element and add the value at top.
st->top++;
st->s[st->top] = value;
}
}
/*
API to delete / pop out element from the stack. It will return popped out value.
*/
char pop(stack *st)
{
int value = -1; // if stack is empty, return -1.
// Check if stack is empty.
if(st->top == -1)
printf("Stack is Empty\n");
else
{
value = st->s[st->top];
st->top--;
}
return value;
}
/*
API to display the elements of the stack.
*/
void display(stack st)
{
// Traverse the stack.
for(int i = st.top; i >= 0; i--)
{
printf("%d\t", st.s[i]);
}
printf("\n");
}
/*
API to check is stack is empty.
*/
int isEmpty(stack st)
{
if(st.top == -1)
return 1;
else
return 0;
}
/*
API to get the element at the top.
*/
char stackTop(stack st)
{
if(isEmpty(st) == 0)
return st.s[st.top];
else
return -1;
}
/*
Helper API to check whether two characters are opening and closing of
same type.
*/
bool are_pair(char opening, char closing)
{
if(opening == '(' && closing == ')')
return true;
else if(opening == '{' && closing == '}')
return true;
else if(opening == '[' && closing == ']')
return true;
return false;
}
/*
API to check for balanced parantheses using the stack data structure.
Algo:
--> scan the array from the left to right.
--> while scanning,
--> if(str[i] == ( == { == [) i.e. char is opening symbol
then push it to the stack
--> if(str[i] == ) == } == ]) i.e. char is closing symbol
then, pop last opening symbol from stack
check if closing and opening symbol matches.
--> if at the end of string, if stack becomes empty, means
expression ==> BALANCED PARANTHESES.
*/
bool are_balanced_parantheses(char *exp)
{
// create stack to store the opening and closing symbols.
stack st;
st.size = strlen(exp);
st.top = -1;
st.s = (char *)malloc(sizeof(char) * (st.size));
// traverse the expression from left to right
for(int i = 0; i < st.size; i++)
{
// check for opening brackets and push them to the stack.
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
{
push(&st, exp[i]);
}
// check for the closing brackets.
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if(isEmpty(st) == 1 || are_pair(stackTop(st), exp[i]) != 1)
{
return false; // not balanced parantheses.
}
else
pop(&st);
}
}
if(isEmpty(st) == 1)
return true;
else
return false;
}
int main(void)
{
// char str[] = "{(a+b)*(a-b)}";
char str[] = "{";
if(are_balanced_parantheses(str) == 1)
printf("YES balanced parantheses. \n");
else
printf("NOT balanced parantheses. \n");
return 0;
}