-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_appl.cpp
More file actions
94 lines (80 loc) · 1.31 KB
/
Copy pathstack_appl.cpp
File metadata and controls
94 lines (80 loc) · 1.31 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define N 100
typedef struct StackType {
int top;
char stack[N];
}StackType;
void init(StackType* s) {
s->top=-1;
}
bool isEmpty(StackType* s) {
return s->top==-1;
}
bool isFull(StackType* s) {
return s->top==N-1;
}
void push(StackType* s, char c) {
if (isFull(s)) {
printf("Overflow\n");
} else {
s->top++;
s->stack[s->top] = c;
}
}
void print(StackType* s) {
for(int i=0; i<=s->top; i++) {
printf("%c\n", s->stack[i]);
}
}
char pop(StackType* s) {
if(isEmpty(s)) {
printf("Empty\n");
return '.';
} else {
char c = s->stack[s->top];
s->top--;
return c;
}
}
char peak(StackType* s) {
if(isEmpty(s)) {
printf("Empty\n");
return '.';
} else {
return s->stack[s->top];
}
}
bool check(char expr[]) {
StackType S;
init(&S);
char c, t;
int n = strlen(expr);
for (int i=0; i<n; i++) {
c = expr[i];
if (c=='(' || c=='{' || c=='[') {
push(&S, c);
} else if (c==')' || c=='}' || c==']') {
t = pop(&S);
if ((t=='(' && c!=')') ||
(t=='{' && c!='}') ||
(t=='[' && c!=']')) {
return false;
}
}
}
if (isEmpty(&S)) {
return true;
} else return false;
}
int main() {
char expr[N];
scanf("%s", expr);
if(check(expr)) {
printf("success\n");
} else {
printf("fail\n");
}
return 0;
}