-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathValid_Parenthesis.c
75 lines (74 loc) · 1.15 KB
/
Valid_Parenthesis.c
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
#include<stdio.h>
int stack[50],top,x,i,n;
char par[30];
void push(int );
void pop();
int c;
c=0;
int main()
{
top=-1;
printf("Enter the paranthesized expression:");
scanf("%s",&par);
n=sizeof(par);
for (int i=0;i<n;i++){
if (par[i]=='('||par[i]=='['||par[i]=='{'){
push(par[i]);
}
else if(par[i]==')'){
if (stack[top]=='('){
pop();
}
else{
c=1;
printf("Invalid");
break;
}
}
else if(par[i]=='}'){
if (stack[top]=='{'){
pop();
}
else{
c=1;
printf("Invalid");
break;
}
}
else if(par[i]==']'){
if (stack[top]=='['){
pop();
}
else{
c=1;
printf("Invalid");
break;
}
}
}
if(c==0){
if(top==-1){
printf("Valid");
}
else{
printf("Invalid");
}
}
}
void push(int x){
if (top>=n-1){
printf("Stack is overflowed\n");
}
else{
stack[++top]=x;
}
}
void pop()
{
if(top<=-1){
printf("stack is underflown\n");
}
else{
top--;
}
}