#include <stdio.h>
#include <stdlib.h>
#include<assert.h>
#include<stdbool.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef int StDateType;
typedef struct Stack{
StDateType* a;
int top;
int capacity;
}Stack;
void StackInit(Stack* pst);//初始化
void StackDestory(Stack* pst);//格式化
void StackPush(Stack* pst, StDateType x);//入栈
void StackPop(Stack* pst);//出栈
int StackSize(Stack* pst);//栈的长度
void StackInit(Stack* pst)//初始化
{
pst->a = (StDateType*)malloc(sizeof(StDateType)*4);
pst->capacity = 4;
pst->top = 0;
}
void StackDestory(Stack* pst)//格式化
{
free(pst);
pst->capacity = 0;
pst->top = 0;
}
void StackPush(Stack* pst, StDateType x)//入栈
{
if(pst->top >= pst->capacity)
{
pst->capacity*=2;
pst->a = (StDateType*)realloc(pst->a, sizeof(StDateType)*pst->capacity);
}
pst->a[pst->top] = x;
pst->top++;
}
void StackPop(Stack* pst)//出栈
{
if(pst->top>0)
{
pst->top--;
}
}
//int StackSize(Stack* pst)//栈的长度
int main() {
Stack st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
StackPush(&st, 5);
while(!StackEmpty(&st)){
printf("%d ",StackTop(&st));
StackPop(&st);
}
return 0;
}
07-18
984
04-29
5158
11-03
06-02