数据结构:栈(Stack)的定义和它的函数们
栈结构体的定义:
#define MAXSIZE 100
typedef char ElemType;
typedef struct _stack
{
ElemType* base;
ElemType* top;
int StackSize;
}sqStack;
栈的初始化:
void InitStack(sqStack* S)
{
S->base = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
if(!S->base) exit(0);
S->top = S->base;
S->StackSize = MAXSIZE;
}
入栈:
void PushStack(sqStack* S , ElemType e)
{
if(S->top - S->base == S->StackSize)
S->base = (ElemType*)realloc(S->base,sizeof(ElemType)*(S->StackSize + MAXSIZE));
e = *S->top++;
}
出栈:
void PopStack(sqStack* S, ElemType* e)
{
if(S->base == S->top) printf("栈已空");
*e = *--S->top;
}
求栈长:
int StackLen(sqStack S)
{
return (S.top - S.base);
}