存储堆栈信息的结构体
#define stackType char(int等等)
struct stack{
int capacity;
int topStack;
stackType *base;
};
建立堆栈
/*
传入参数:指向堆栈结构体信息的指针,以及希望建立的堆栈大小
*/
void createStack(stack *S, int size)
{
S->capasity = size;
S->topStack = -1;
S->base = malloc(size * sizeof(stackType));
}
堆栈的push操作
/*
传入参数:指向堆栈结构体的指针 和 想要push的元素
*/
void push(stack *S, stackType e)
{
assert(!isFull(S)); // 判断堆栈不为满
S->topStack += 1;
S->base[S->topStack] = e;
}
堆栈的pop操作
/*
传入参数:指向堆栈结构体的指针
*/
void pop(stack *S)
{
assert(isEmpty(S));
S->topStack -= 1;
free(S->base);
S->base -= 1;
}