源程序:
//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;
}
//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;
}