作者在 2010-05-10 22:17:32 发布以下内容
栈的类型定义
#define INIT_SIZE 100;
#define ADD_SIZE 10;
typedef struct
{
SDataType *base;
SDataType *top;
int stacksize;
}SStack;
初始化栈
void InitStack(SStack *S)
{
s->base=(SDataType *)malloc(INIT_SIZE*sizeof(SDataType));
assert(s->base!=NULL);
s->top=s->base;
s->stacksize=INIT_SIZE;
}
判断栈是否为空
bool StackEmpty(SStack S)
{
if(S.top==S.base)
return true;
else
return false;
}
入栈
void Push(SStack *S,SDataType elem)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(SDataType *)realloc(S->base,(S->stacksize+ADD_SIZE)*sizeof(SDataType));
assert(S->base!=NULL);
S->top=S->base+S->stacksize;
S->stacksize+=ADD_SIZE;
}
*S->top++=elem;
}
出栈
SDataType Pop(SStack *S)
{
SDataType elem;
if(StackEmpty(s))
{printf("Stack is empty");return;}
else
{
elem=*--S->top;
return elem;
}
}
获取栈顶元素
SDataType GetTop(SStack S)
{
SDataType elem;
if(StackEmpty(S))
{printf("Stack is empty"); return ;}
else
{
elem=*(S->top-1);
return elem;
}
}
#define INIT_SIZE 100;
#define ADD_SIZE 10;
typedef struct
{
SDataType *base;
SDataType *top;
int stacksize;
}SStack;
初始化栈
void InitStack(SStack *S)
{
s->base=(SDataType *)malloc(INIT_SIZE*sizeof(SDataType));
assert(s->base!=NULL);
s->top=s->base;
s->stacksize=INIT_SIZE;
}
判断栈是否为空
bool StackEmpty(SStack S)
{
if(S.top==S.base)
return true;
else
return false;
}
入栈
void Push(SStack *S,SDataType elem)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(SDataType *)realloc(S->base,(S->stacksize+ADD_SIZE)*sizeof(SDataType));
assert(S->base!=NULL);
S->top=S->base+S->stacksize;
S->stacksize+=ADD_SIZE;
}
*S->top++=elem;
}
出栈
SDataType Pop(SStack *S)
{
SDataType elem;
if(StackEmpty(s))
{printf("Stack is empty");return;}
else
{
elem=*--S->top;
return elem;
}
}
获取栈顶元素
SDataType GetTop(SStack S)
{
SDataType elem;
if(StackEmpty(S))
{printf("Stack is empty"); return ;}
else
{
elem=*(S->top-1);
return elem;
}
}