數據結構——棧的講解(超詳細)-騰訊云開發者社區-騰訊云
#include"Stack.h"
void STInit(ST* ps)
{ps->arr = NULL;ps->capacity = ps->top = 0; //總空間個數和有用空間個數都初始化為0
}void STDestroy(ST* ps)
{if (ps -> arr) //先判斷是否進行動態內存開辟了{free(ps -> arr);}ps->capacity = ps->top = 0;
}void STPush(ST* ps, STDataType x) //類似順序表的尾插
{if (ps->capacity == ps->top){int newcaopacity = ps->capacity == 0 ? 4 : 2 * ps -> capacity;STDataType* arr1 = (STDataType*)realloc(ps->arr, newcaopacity * sizeof(STDataType));assert(arr1);ps->arr = arr1;ps->capacity = newcaopacity;} //擴容完成ps->arr[ps->top++] = x;
}bool panduan(ST * ps)
{assert(ps);return ps -> top == 0; //這個是來判斷棧是不是空了
}void STPop(ST* ps)
{assert(ps);assert(!panduan(ps));ps->top--;
}STDataType STTop(ST* ps)
{return ps->arr[ps->top - 1];
}int STSize(ST* ps)
{return ps->top;
}