-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.Parenthesis_checking_using_Stack.c
More file actions
67 lines (58 loc) · 1.22 KB
/
Copy path31.Parenthesis_checking_using_Stack.c
File metadata and controls
67 lines (58 loc) · 1.22 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
int stackSize;
int top;
char *arr;
}stack;
int isEmpty(stack * ptr){
if(ptr->top == -1)
return 1;
return 0;
}
int isFull(stack * ptr){
if(ptr->top == ptr->stackSize-1)
return 1;
return 0;
}
void push(stack * ptr, char value){
if(isFull(ptr))
printf("Stack Full!");
ptr->top++;
ptr->arr[ptr->top] = value;
}
char pop(stack * ptr){
if(isEmpty(ptr))
return -1;
char val = ptr->arr[ptr->top];
ptr->top--;
return val;
}
int paranthesisCheck(char *exp){
stack * sp = (stack *)malloc(sp->stackSize * sizeof(stack));
sp->stackSize = strlen(exp);
sp->top = -1;
sp->arr = (char *)malloc(sizeof(char));
for(int i = 0; exp[i] != '\0'; i++){
if(exp[i] == '('){
push(sp, '(');
}
else if(exp[i] == ')'){
if(isEmpty(sp))
return 0;
pop(sp);
}
}
if(isEmpty(sp))
return 1;
return 0;
}
int main(){
char * exp = "8*(9)";
if(paranthesisCheck(exp))
printf("All Parenthesis are Matched \n");
else
printf("Parenthesis are Not Matched \n");
return 0;
}