#include "stdio.h"
#include "string.h"
#include "stdlib.h"
//#include <alloc.h>
#define stack_size 2 //定义栈的初始化空间大小
#define stack_add_size 3
typedef int ElemType;
typedef struct sqstack
{
ElemType *top;
ElemType *bottom;
int stacksize;//记录当前已分配的空间
}sqStack;
sqStack initStack()
{
sqstack s;
s.bottom =(ElemType*)malloc(stack_size*sizeof(ElemType));
if (!s.bottom)
{
return s;
}
s.top=s.bottom;
s.stacksize=stack_size;
return s;
free(s.bottom);
}
/*
元素进栈
*/
sqStack push(sqstack s,ElemType e)
{
if ((s.top-s.bottom)>(s.stacksize-1))
{
s.bottom=(ElemType*)realloc(s.bottom,(s.stacksize+stack_add_size)*sizeof(ElemType));
if (!s.bottom)
{
return s;
}
s.top=s.bottom+s.stacksize;
s.stacksize+=stack_add_size;
}
*s.top=e;
s.top++;
return s;
free(s.bottom);
}
sqStack pop(sqstack s)
{
int t=0;
if (s.bottom!=s.top)
{
s.top--;
t=*s.top;
}
return s;
}
void Print(sqstack s)
{
while(s.top!=s.bottom)
{
s.top--;
printf("%d
",*s.top);
}
}
void main()
{
sqstack a;
a=initStack();
a=push(a,10);
a=push(a,20);
a=push(a,30);
a=push(a,31);
a=push(a,130);
Print(a);
a=pop(a);
printf("退出一个元素!
");
Print(a);
getchar();
}