理论部分
栈的模拟实现
typedef int STDataType;
typedef struct Stack {
STDataType* a;
int top;
int capacity;
} ST;
void STInit(ST* ps) {
assert(ps);
ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
if (ps->a == NULL) {
perror("malloc fail");
return;
}
ps->capacity = 4;
ps->top = 0;
}
void STDestroy(ST* ps) {
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STPush(ST* ps, STDataType x) {
assert(ps);
if (ps->top == ps->capacity) {
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
if (tmp == NULL) {
perror("realloc fail");
;
}
ps->a = tmp;
ps->capacity *= ;
}
ps->a[ps->top] = x;
ps->top++;
}
{
(ps);
(!(ps));
ps->top--;
}
{
(ps);
ps->top;
}
{
(ps);
ps->top == ;
}
{
(ps);
(!(ps));
ps->a[ps->top - ];
}