#include <bits/stdc++.h>
using namespace std;
#define ElemType int
#define MAXSIZE 200
#define OK 1
/* 可以定义一个 overflow是溢出,为-1 */
typedef struct //定义栈的结构体
{ElemType* base;ElemType* top;int stacksize;
}SqStack;void Init(SqStack* s) //初始化函数
{s->base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));memset(s->base, 0, MAXSIZE * sizeof(ElemType));if (!s->base) /**若栈底为空,则栈溢出*/exit(-1); /**停止程序*/s->top = s->base;s->stacksize = MAXSIZE;
}void Destroy(SqStack* s) //销毁栈
{free(s->base);s->stacksize = 0;s->base = NULL;s->top = NULL;
}void Clear(SqStack* s) //清空栈
{s->top = s->base;
}bool Empty(SqStack *s) //判断是否为空
{return s->top == s->base;
}ElemType Top(SqStack *s) //返回栈顶元素
{return *(s->top - 1);
}
bool Push(SqStack* s, ElemType e)
{if (s->top - s->base + 1 > s->stacksize){s->base = (ElemType*)realloc(s->base, (MAXSIZE + 1) * sizeof(ElemType));s->stacksize += 1;cout << "增加一个" << endl;}if (!s->base)exit(-1);*(s->top) = e;s->top++;return OK;
}bool Pop(SqStack* s)
{if (Empty(s)){cout << "删除错误" << endl;exit(-1);}s->top--;return OK;
}int main()
{SqStack S1;Init(&S1);Push(&S1, 1);Push(&S1, 2);Push(&S1, 3);Push(&S1, 4);Push(&S1, 5);Push(&S1, 6);SqStack S2;Init(&S2);Push(&S2, 1);Push(&S2, 2);Push(&S2, 3);Push(&S2, 4);Push(&S2, 5);Push(&S2, 6);while (!Empty(&S2)){cout << Top(&S2) << " ";Pop(&S2);}Destroy(&S1);Destroy(&S2);return 0;
}