Skip to content

Instantly share code, notes, and snippets.

@Poplava
Created April 2, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Poplava/b3c9ce722a1d6ea14e29 to your computer and use it in GitHub Desktop.
Save Poplava/b3c9ce722a1d6ea14e29 to your computer and use it in GitHub Desktop.
#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