#define STACK_SIZE 10000
struct stack{
char a[STACK_SIZE];
int top;
};
void push(struct stack *s, char c)
{
if(s->top == STACK_SIZE-1)
return;
else{
s->top++;
s->a[s->top] = c;
}
}
char pop(struct stack *s)
{
if(s->top == -1)
return -1;
else{
return s->a[s->top--];
}
}
bool isValid(char* s) {
struct stack stack;
stack.top = -1;
char c;
int tag = 0;
while(*s != '\0'){
printf("%c tag = %d\n", *s, tag);
if(*s == '(' || *s == '{' || *s == '['){
push(&stack, *s);
s++;
tag++;
}
else if(*s == ')' || *s == '}' || *s == ']'){
c = pop(&stack);
printf("c = %c\n", c);
if(c == '(' && *s == ')')
tag--,s++;
else if(c == '{' && *s == '}')
tag--,s++;
else if(c == '[' && *s == ']')
tag--,s++;
else
return false;
}else
s++;
}
if(tag == 0)
return true;
else
return false;
}
网友评论