Created
April 2, 2015 04:19
-
-
Save Poplava/b3c9ce722a1d6ea14e29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
using namespace std; | |
struct Stack | |
{ | |
char x; | |
Stack* prev; | |
}; | |
typedef Stack* ps; | |
void push(ps &top, char c) | |
{ | |
ps s = new Stack; | |
s->x = c; | |
s->prev = top; | |
top = s; | |
} | |
char pop(ps &top) | |
{ | |
ps s = top; | |
char c = top->x; | |
top = s->prev; | |
delete s; | |
return c; | |
} | |
bool isEmpty(ps top) | |
{ | |
return top == nullptr; | |
} | |
void destructList(ps top) | |
{ | |
while (!isEmpty(top)) { | |
pop(top); | |
} | |
} | |
bool checker(char* s) | |
{ | |
ps top = nullptr; | |
int i, l = strlen(s); | |
bool es = false; | |
for (i = 0; i < l; i++) { | |
if (s[i] == '(') { | |
push(top, s[i]); | |
} | |
else if (s[i] == ')') { | |
if (isEmpty(top)) { | |
destructList(top); | |
return false; | |
} | |
pop(top); | |
} | |
} | |
es = isEmpty(top); | |
destructList(top); | |
return es; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment