顺序栈
![结构](https://img-blog.csdnimg.cn/20201111184900272.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDg4MzE4MA==,size_1,color_FFFFFF,t_70#pic_center)
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
int * base;
int * top;
int stacksize;
}SqStack;
int InitStack(SqStack &S){
S.base = (int * )malloc(STACK_INIT_SIZE * sizeof(int));
if(!S.base)
exit(-2);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
}
int push(SqStack &S, int e){
if(S.top - S.base > STACK_INIT_SIZE){
S.base = (int * ) realloc (S.base, (S.stacksize + STACKINCREMENT) * sizeof (int));
if(!S.base)
exit(-2);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top = e;
S.top++;
return 1;
}
int pop(SqStack &S){
if (S.base == S.top)
return 0;
cout << * --S.top << ' ';
return 1;
}
int stack_destroy(SqStack &S)
{
free(S.base);
S.base = S.top = NULL;
S.stacksize = 0;
return 0;
}
int main(){
SqStack S;
InitStack(S);
push(S,2);
push(S,3);
push(S,4);
push(S,5);
pop(S);
pop(S);
stack_destroy(S);
pop(S);
}