C语言实现顺序栈基本操作
代码如下:
#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
#define INIT_SIZE 100 //初始栈空间大小
#define STACKINCREMENT 10 //栈空间再分配增量
typedef int ElemType;//可以定义栈中元素类型
typedef struct SqStack //构造栈
{
ElemType *base;
ElemType *top;
int StackSize;
}SqStack, *PSqStack;
void initStack(PSqStack S)
{
S->base = (ElemType*)malloc(INIT_SIZE*sizeof(ElemType));//顺序存储在初始化时必须分配好一片连续空间
if(!S->base)
return;
S->top = S->base;
S->StackSize = INIT_SIZE;
}
void DestroyStack(PSqStack S)//销毁栈
{
free(S->base);
S->base = S->top = NULL;
S->StackSize = 0;
}
void ClearStack(PSqStack S)//清空栈
{
if(S->base == S->top)
return;
S->top = S->base;
}
bool StackEmpty(PSqStack S)//判断栈空
{
if(S->base == S->top)
return 1;
else
return 0;
}
int StackL