|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
- typedef struct stack{
- int capacity;//最大容量
- int top;//栈顶
- int *pst;
- }stack;
- // 初始化栈
- void stackinit(stack* ps);
- // 入栈
- void stackpush(stack* ps, int data);
- // 出栈
- void stackpop(stack* ps);
- // 获取栈顶元素
- int stacktop(stack* ps);
- // 获取栈中有效元素个数
- int stacksize(stack* ps);
- // 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
- bool stackempty(stack* ps);
- // 销毁栈
- void stackdestroy(stack *ps);
- int maze[8][8]={1,1,0,1,1,1,0,1,
- 1,1,0,1,1,1,0,1,
- 1,1,1,1,0,0,1,1,
- 1,0,0,0,1,1,1,1,
- 1,1,1,0,1,1,1,1,
- 1,0,1,1,1,0,1,1,
- 1,0,0,0,1,0,0,1,
- 0,1,1,1,1,1,1,1,};//1表示通,0表示不通,表示迷宫
- int map[8][8]={0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,};//地图用于表示迷宫位置是否已走过
- void stackinit(stack *ps){
- ps->pst=NULL;
- ps->top=0;
- ps->capacity=0;//栈总的容量
- }
- void stackpush(stack *ps,int data){
- assert(ps); //断言ps是否存在
- if(ps->top == ps->capacity)
- {
- int newcapacity=ps->capacity==0?4 : ps->capacity*2;
- int *tmp=(int *)realloc(ps->pst,newcapacity*sizeof(int));
- if(tmp==NULL)
- {
- perror("realloc fail");
- return;
- }
- ps->pst=tmp;
- ps->capacity=newcapacity;
- }
- ps->pst[ps->top]=data;
- ps->top++;
- }
- void stackpop(stack *ps){
- assert(ps);
- assert(!stackempty(ps));
- ps->top--;
- }
- int stacktop(stack *ps)
- {
- assert(ps);
- int top=ps->top;
- return (ps->pst)[top-1];
- }
- int stacksize(stack *ps){
- assert(ps);
- return ps->top;
- }
- bool stackempty(stack *ps){
- assert(ps);
- if(ps->top==0)
- {
- return true;
- }
- return false;
- }
- void stackdestroy(stack *ps){
- assert(ps);
- free(ps->pst);
- }
- int main()
- {
- int currentpoint=1,finalpoint=64;
- stack people;
- stack * p=&people;
- stackinit(p);
- stackpush(p,currentpoint);
- map[0][0]=1;//
- while(currentpoint!=finalpoint)//直到终点停止
- {
- int i,j;//标记地图
- //判断能否上
- j=((currentpoint-8)%8)-1;
- i=((currentpoint-8)/8);
- if(currentpoint-8>1 && maze[i][j]==1 && map[i][j]==0)
- {
- currentpoint=currentpoint-8;
- stackpush(p,currentpoint);
- map[i][j]=1; //标记已走
- printf("w ");
- continue;
- }
- //能否右走
- j=((currentpoint+1)%8)-1;
- i=((currentpoint+1)/8);
- if(currentpoint%8!=0 && maze[i][j]==1 && map[i][j]==0){
- currentpoint=currentpoint+1;
- stackpush(p,currentpoint);
- map[i][j]=1;
- printf("d ");
- continue;
- }
- //能否下走
- j=((currentpoint+8)%8)-1;
- i=((currentpoint+8)/8);
- if(currentpoint+8<=64 && maze[i][j]==1 && map[i][j]==0){
- currentpoint=currentpoint+8;
- stackpush(p,currentpoint);
- map[i][j]=1;
- printf("s ");
- continue;
- }
- //能否左走
- j=((currentpoint-1)%8)-1;
- i=((currentpoint-1)/8);
- if((currentpoint-1)%8!=0 && maze[i][j]==1 && map[i][j]==0){
- currentpoint=currentpoint-1;
- stackpush(p,currentpoint);
- map[i][j]=1;
- printf("a ");
- continue;
- }
- //都不能走
- stackpop(p);
- currentpoint=stacktop(p);
- printf("返回位置%d\n",currentpoint);
- }
- printf("well");
- }
复制代码
希望能对栈有更深刻的认识 |
|