- #define ERROR 0
-
#define TRUE 1
-
#define FALSE 0
-
#define OK 1
-
#define EQUAL 1
-
#define OVERFLOW -1
-
#define STACK_INIT_SIZE 100 //存储空间初始化分配量
-
#define STACKINCREMENT 10 //存储空间分配增量
-
-
typedef int Status;
-
-
typedef struct
-
{
-
SElemType *base;
-
SElemType *top; //栈顶指针
-
int stacksize;
-
}SqStack,*pSqStack;
-
-
Status InitStack(SqStack **S); //初始化
-
Status DestroyStack(SqStack *S);
-
Status ClearStack(SqStack *S);
-
Status StackEmpty(SqStack S); //判断栈是否为空
-
int StackLength(SqStack S); //返回栈的长度
-
Status GetTop(SqStack S,SElemType *e); //用e返回栈顶元素
-
Status Push(SqStack *S,SElemType e); //向栈订插入元素
-
Status Pop(SqStack *S,SElemType *e); //出栈
-
Status StackTraverse(SqStack S,Status (*visit)()); //从栈底到栈顶对栈S中每个元素调用函数visit()
-
-
Status InitStack(SqStack **S)
-
{
-
(*S)=(SqStack *)malloc(sizeof(SqStack));
-
(*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
-
if(!(*S)->base)exit(OVERFLOW);
-
(*S)->top=(*S)->base;
-
(*S)->stacksize=STACK_INIT_SIZE;
-
return OK;
-
}
-
-
Status DestroyStack(SqStack *S)
-
{
-
free(S->base);
-
free(S);
-
}
-
-
Status ClearStack(SqStack *S)
-
{
-
S->top=S->base;
-
}
-
-
Status StackEmpty(SqStack S)
-
{
-
if(S.top==S.base) return TRUE;
-
else
-
return FALSE;
-
}
-
-
int StackLength(SqStack S)
-
{
-
int i;
-
SElemType *p;
-
i=0;
-
p=S.top;
-
while(p!=S.base)
-
{p++;
-
i++;
-
}
-
}
-
-
Status GetTop(SqStack S,SElemType *e)
-
{
-
if(S.top==S.base) return ERROR;
-
*e=*(S.top-1);
-
return OK;
-
}
-
-
Status Push(SqStack *S,SElemType e)
-
{
-
if(S->top - S->base>=S->stacksize)
-
{ S->base=(SElemType *) realloc(S->base,
-
(S->stacksize + STACKINCREMENT) * sizeof(SElemType));
-
if(!S->base)exit(OVERFLOW);
-
S->top=S->base+S->stacksize;
-
S->stacksize += STACKINCREMENT;
-
}
-
*(S->top++)=e;
-
return OK;
-
}
-
-
Status Pop(SqStack *S,SElemType *e)
-
{
-
if(S->top==S->base) return ERROR;
-
*e=*--S->top;
-
return OK;
-
}
-
-
Status StackTraverse(SqStack S,Status (*visit)())
-
{
-
while(S.top!=S.base)
-
visit(*(--S.top));
-
}
阅读(4820) | 评论(0) | 转发(0) |