栈的例题和总结
0. 目录
1. 有效的括号
static int top = 0;
static char* buf = NULL;
void stack(int size){
top = size;
buf = (char*)malloc(size+1);
buf[size] = ' ';
}
void push(char c) {buf[--top] = c; }
char pop() {return buf[top++];}
bool isValid(char * s){
int dev = 0;
stack(strlen(s)/2 + 1);
for(int i=0;i<strlen(s);i++){
if((s[i]=='(') || (s[i]=='[') || (s[i] == '{'))
push(s[i]);
else{
dev = s[i] - pop();
if((dev!=1) &&(dev!=2))//一对括号ascii相差1或2
return false;
}
}
if(buf[top] != ' ') return false;
return true;
}
2. 栈实现队列
这个参见队列的章节