-
Notifications
You must be signed in to change notification settings - Fork 43
/
stack.cpp
85 lines (83 loc) · 1.46 KB
/
stack.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
//bracket checking
#include <bits/stdc++.h>
using namespace std;
#define MAXSIZE 100
struct lifo
{
char st[MAXSIZE];
int top;
};
typedef struct lifo sta;
void create(sta *s)
{
s->top = -1;
}
void push (sta *s,char e)
{
if(s->top == (MAXSIZE-1))
{
cout<<"stack overflow";
exit(-1);
}
else
{
s->top++;
s->st[s->top] = e;
}
}
void pop(sta *s)
{
if(s->top == -1)
{
cout<<"stack underflow";
exit(-1);
}
else
{
s->top--;
}
}
int isempty(sta *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
int isfull(sta *s)
{
if(s->top == MAXSIZE-1)
return 1;
else
return 0;
}
int main() {
sta A;
sta *pi;
pi = &A;
create(&A);
int n,i;
i=0;
string s;
cout<<"Enter expression: ";
cin>>s;
while(i!=s.size())
{
if(s[i]==40 || s[i]==91 || s[i]==123)
push(&A,s[i]);
else if(pi->top!= -1)
{
if(((s[i]==')' && pi->st[pi->top]=='(')||(s[i]=='}' &&pi->st[pi->top]=='{')||(s[i]==']' && pi->st[pi->top]=='[')))
pop(&A);
else if(((s[i]==')' && pi->st[pi->top]!='(')||(s[i]=='}' &&pi->st[pi->top]!='{')||(s[i]==']' && pi->st[pi->top]!='[')))
{
cout<<"misbalance";
break;
}
}
i++;
}
if(i==s.size())
cout<<"balance";
return 0;
}