栈的基本操作:
1,存储结构:
2,创建空战:
3,获取栈顶元素:(判空)
4,插入和删除:
5,销毁栈:
# define stack_init_size 100
# define stackincrement 10
typedef int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
void DestoryStack(SqStack &S)
{
S.top=S.base;
free(S.base);
}
void TraverseStack(SqStack S)
{
while(S.top!=S.base)
{
printf("%d ",*--S.top);
}
}
int CreatStack(SqStack &S)
{
S.base=(SElemType *)malloc(stack_init_size*sizeof( SElemType ) );
if(!S.base) exit(-2) ;
S.top=S.base;
S.stacksize=stack_init_size;
}
void GetTop(SqStack &S,int &e)
{
if(S.base==S.top) return ;
e=*(S.top-1);
}
void Pop(SqStack &S,int &e)
{
if(S.top==S.base) return ;
e=*--S.top;
}
void InsertStack(SqStack &S,int &e)
{
if(S.top-S.base>S.stacksize)
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+stackincrement)*sizeof(SElemType));
if(!S.base) exit(-2);
S.top=S.base+S.stacksize;
S.stacksize+=stackincrement;
}
*S.top++=e;
}