#include
#include
#include
#include
typedef struct Stack
{
int *a;
int top;
int capacity;
}ST;
void StackInit(ST* arr)
{
assert(arr);
arr->a = NULL;
arr->top = arr->capacity = 0;
}
void StackDestory(ST* arr)
{
assert(arr);
free(arr->a);
arr->capacity = arr->top = 0;
}
void StackPush(ST* arr, int x)
{
assert(arr);
if (arr->top == arr->capacity)
{
int newcapacity = arr->capacity == 0 ? 4 : arr->capacity * 2;
ST* new = realloc(arr->a,sizeof(int) * newcapacity);
if (new == NULL)
{
exit(-1);
}
arr->a = new;
arr->capacity = newcapacity;
}
arr->a[arr->top] = x;
arr->top++;
arr->capacity++;
}
void StackPop(ST* arr)
{
assert(arr);
assert(arr->top >0);
arr->top--;
}
int StackTop(ST* arr)
{
return arr->a[arr->top - 1];
}
int StackSize(ST* arr)
{
return arr->top;
}
bool StackEmpty(ST* arr)
{
return arr->a[arr->top] == 0;
}
int main()
{
ST ps;
StackInit(&ps);
StackPush(&ps, 1);
StackPush(&ps, 1);
StackPush(&ps, 1);
StackPush(&ps, 1);
while (!StackEmpty)
{
printf("%d\n", StackTop(&ps));
StackPop(&ps);
}
return 0;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网页标题:c语言栈的实现-创新互联
网页网址:http://lswzjz.com/article/pcdpp.html