5.21学习打卡
链式串
1 #define MaxSize 100 //顺序栈的初始分配空间大小 2 3 4 typedef struct { 5 6 int data[MaxSize]; 7 //保存栈中的元素 8 int top; 9 //栈顶指针 10 }SqStack; 11 12 //初始化栈 13 void InitStack(SqStack *st){ 14 st = (SqStack* )malloc(sizeof(SqStack)); 15 st->top = 0; 16 } 17 18 //销毁栈 19 void DestroyStack(SqStack *st){ 20 free(st); 21 } 22 23 //进栈 24 int Push(SqStack &st,int x){ 25 if(st.top == MaxSize-1) return 0; 26 st.data[st.top] = x; 27 st.top++; 28 return 1; 29 } 30 31 //退栈 32 int Pop(SqStack &st,int &x){ 33 if(st.top == 0) return 0; 34 x = st.data[st.top-1]; 35 st.top--; 36 return x; 37 } 38 39 //获取栈顶元素 40 int GetTop(SqStack &st,int &x){ 41 x = st.data[st.top-1]; 42 return x; 43 }