-
Notifications
You must be signed in to change notification settings - Fork 0
/
33_paranthesis.c
84 lines (72 loc) · 1.64 KB
/
33_paranthesis.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
76
77
78
79
80
81
82
83
84
#include <stdlib.h>
#include <stdio.h>
struct stack {
int size;
char *arr;
int top;
};
int isEmpty(struct stack *ptr) {
if (ptr->top == -1) {
return 1;
} else {
return 0;
}
}
int isFull(struct stack *ptr) {
if (ptr->top == ptr->size - 1) {
return 1;
} else {
return 0;
}
}
char pop(struct stack *ptr){
if (isEmpty(ptr)){
printf("\nstack underflow\n");
return -1;
}
else{
int value = ptr -> arr[ptr -> top];
ptr -> top = ptr -> top - 1;
return value;
}
}
void push(struct stack *ptr, char value){
if (isFull(ptr)){
printf("stack overflow\n");
}
else{
ptr -> top = ptr -> top + 1;
ptr -> arr[ptr -> top] = value;
}
}
int countParanthesis(char *a){
struct stack *sp = (struct stack*)malloc(sizeof(struct stack));
sp -> size = 80;
sp -> top = -1;
sp -> arr = (char*)malloc(sp ->size * sizeof(char));
// String Comparison Error: The comparison a[i] != "\0" should be a[i] != '\0' because "\0" is a string literal and '\0' is a character literal.
for (int i = 0; a[i] != '\0';i++){
if (a[i] == '('){
push(sp, '(');
}
else if (a[i] == ')'){
if (isEmpty(sp)){
return 0;
}
pop(sp);
}
}
if (isEmpty(sp)){
return 1;
}
return 0;
}
int main() {
char *val = "((()) )";
if (countParanthesis(val)){
printf("Equal Equal\n");
}
else{
printf("Not Equal");
}
}