-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipleParenthesisMatching.cpp
75 lines (71 loc) · 1.47 KB
/
multipleParenthesisMatching.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
#include<iostream>
using namespace std;
struct DStack{
char data;
DStack* next = NULL;
};
DStack* top = NULL;
void push(char i);
char pop();
bool isEmpty();
bool parenthesisMatching();
int main(){
cout<<"Is Parenthesis Matching: "<<parenthesisMatching;
}
bool parenthesisMatching(){
char *exp;
exp = "{([a+b]*[c-d])\e}";
for(int i=0;i<exp[i]!='\0';i++){
if(exp[i]=='(' ||exp[i]=='{' || exp[i]=='['){
push(exp[i]);
}
else if(exp[i]==')' || '}' || ']'){
if(isEmpty()){
return false;
} else{
int x;
x = pop();
if(x!=exp[i]){
return false;
}
}
}
}
if(isEmpty()){
return true;
} else{
return false;
}
}
void push(char element){
DStack *newNode = new DStack;
cin>>newNode->data;
if(newNode==NULL){
cout<<"Stack Is Full (STACK-OVER-FLOW)";
}
if(top==NULL){
top = newNode;
}else{
newNode->next = top;
top = newNode;
}
}
char pop(){
char x = -1;
DStack *p = top;
if(top==NULL){
cout<<"Stack Is Empty (STACK-UNDER-FLOW)";
}else{
x = top->data;
top = top->next;
delete p;
}
return x;
}
bool isEmpty() {
if (top == NULL) {
return true;
} else {
return false;
}
}