#ifndef SQSTACK_H__
#define SQSTACK_H__
#include"head.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
struct stack{
SElementType *base;
SElementType *top;
int stackSize;
};
typedef struct stack sqStack;
status initStack(sqStack *S);
status destroyStack(sqStack *S);
status clearStack(sqStack *S);
int length(sqStack S);
boolean empty(sqStack S);
boolean push(sqStack *S,SElementType e);
boolean pop(sqStack *S,SElementType *e);
status getTop(sqStack S,SElementType *e);
void traverse(sqStack S,void (*view)(SElementType*));
#endif
#ifndef MAZE_H__#define MAZE_H__#include"head.h"#include"sqStack.h"#include<stdio.h>#define MAXMAZE 10typedef int MazeType[MAXMAZE][MAXMAZE];status mazePath(PositionType start,PositionType end);void displayMaze(int x,int y);#endif
#ifndef HEAD_H__
#define HEAD_H__
#include<stdlib.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int status;
typedef int boolean;
typedef int ElementType;
typedef int SElementType;//调用conversion()函数的时候使用此类型
typedef int QElementType;
/*/定义迷宫的结构体//
typedef struct{
int x;
int y;
}PositionType;
typedef struct{
int ord;
PositionType seat;
int di;
}SElementType;
//*/
#define MAX 100
//常用函数合集
void view(ElementType *);
void view(SElementType *);
void view(QElementType *);
status compare(ElementType ,ElementType);
void buildSet(ElementType a[],int length,int beg,int end);
void printSet(ElementType a[],int length);
void swap(ElementType *e1,ElementType *e2);
void quickSort(ElementType a[],int p,int r);
void randomizedQuickSort(ElementType a[],int p,int r);
//void conversion(SElementType N,int d);//使用栈实现的数制转换
#endif
#include"sqStack.h"
status initStack(sqStack *S)
{
S->base=(SElementType*)malloc(STACK_INIT_SIZE*sizeof(SElementType));
if(!S->base)
exit(OVERFLOW);
S->top=S->base;
S->stackSize=STACK_INIT_SIZE;
return OK;
}
status destroyStack(sqStack *S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stackSize=0;
return OK;
}
status clearStack(sqStack *S)
{
S->top=S->base;
return OK;
}
int length(sqStack S)
{
return S.top-S.base;//这个速度是O(1);
/*
SElementType *p;
int i=0;
p=S.base;
for(p;p!=S.top;++p)
++i;
return i;
*/
}
boolean empty(sqStack S)
{
return (S.top==S.base)?1:0;
}
boolean push(sqStack *S,SElementType e)
{
if(S->top-S->base>=S->stackSize)//此处注意stackSize有可能扩展过
{
S->base=(SElementType *)realloc(S->base,(S->stackSize+STACKINCREMENT)*sizeof(SElementType));
if(!S->base)
exit(OVERFLOW);
S->top=S->base+S->stackSize;
S->stackSize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
boolean pop(sqStack *S,SElementType *e)
{
if(S->top==S->base)
return ERROR;
*e=*--S->top;
return OK;
}
status getTop(sqStack S,SElementType *e)
{
if(S.base==S.top)
return ERROR;
*e=*(S.top-1);//此处并不是要弹出元素,只是获得元素故不能使用*--S.top。
return OK;
}
void traverse(sqStack S,void (*view)(SElementType*))
{
SElementType *p;
p=S.base;
while(p!=S.top)
view(p++);
}
#include"head.h"
#include"Queue.h"
/*
status initQueue(linkQueue *Q);
status destroyQueue(linkQueue *Q);
status clearQueue(linkQueue *Q);
boolean empty(linkQueue Q);
int length(linkQueue Q);
status getHead(linkQueue Q,QElementType *e);
status enQueue(linkQueue *Q,QElementType e);
status deQueue(linkQueue *Q,QElementType *e);
void traverse(linkQueue Q,void (*view)(QElementType *));
*/
int main()
{
linkQueue Q;
QElementType e;
int i;
initQueue(&Q);
printf("the queue's address is %d\n",Q);
if(empty(Q))
printf("the queue is empty\n");
for(i=0;i<10;i++)
enQueue(&Q,i);
printf("after enQueue() the queue is :\n");
traverse(Q,view);
putchar('\n');
for(i=0;i<5;i++)
{
if(getHead(Q,&e))
printf("the first element of the queue is: %d\n",e);
deQueue(&Q,&e);
}
printf("after delete the queue is:\n");
traverse(Q,view);
putchar('\n');
if(clearQueue(&Q))
printf("the queue is cleared\n");
printf("after clear the queue the length is : %d\n",length(Q));
destroyQueue(&Q);
printf("after destroy the queue the address is :%d\n",Q);
return 0;
}
#include"head.h"
#include"sqStack.h"
#include"maze.h"
int main()
{
PositionType beg={1,1},end={8,8};
mazePath(beg,end);
displayMaze(10,10);
return 0;
}