设为首页 加入收藏

TOP

用栈实现迷宫问题求解
2014-11-23 21:36:46 】 浏览:447
Tags:实现 迷宫 问题 求解

  源程序:


  //base.h


  #include


  #include


  #include


  #define TRUE 1


  #define FALSE 0


  #define OK 1


  #define ERROR 0


  #define OVERFLOW -2


  typedef int Status;


  //stack.h


  #include "base.h"


  #define INIT_SIZE 100 //存储空间初始分配量


  #define INCREMENT 10 //存储空间分配增量


  typedef struct{ //迷宫中r行c列的位置


  int r;


  int c;


  }PostType;


  typedef struct{


  int ord; //当前位置在路径上的序号


  PostType seat;//当前坐标


  int di; //往下一坐标的方向


  }SElemType; //栈元素类型


  typedef struct{


  SElemType* base;//栈基址,构造前销毁后为空


  SElemType* top;//栈顶


  int stackSize; //栈容量


  }Stack; //栈类型


  Status InitStack(Stack &S){ //构造空栈s


  S.base=(SElemType*)malloc(INIT_SIZE *sizeof(SElemType));


  if(!S.base)


  exit(OVERFLOW);//存储分配失败


  S.top=S.base;


  S.stackSize=INIT_SIZE;


  return OK;


  }//InitStack


  Status StackEmpty(Stack S){


  //若s为空返回TRUE,否则返回FALSE


  if(S.top==S.base)


  return TRUE;


  return FALSE;


  }//StackEmpty


  Status Push(Stack &S,SElemType e){


  //插入元素e为新的栈顶元素


  if(S.top-S.base >=S.stackSize){//栈满,加空间


  S.base=(SElemType *)realloc(S.base,(S.stackSize+INCREMENT)*sizeof(SElemType));


  if(!S.base)


  exit(OVERFLOW); //存储分配失败


  S.top=S.base+S.stackSize;


  S.stackSize+=INCREMENT;


  }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇堆排序算法的过程演示 下一篇利用栈来实现单链表的逆序

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目