#include <stdio.h>
#include <stdlib.h>
#define stack_init_size 10
#define stack_increment 1
#define OVERFLOW -1
#define ERROR 0
#define OK 1
typedef int elemType;
typedef int status;
typedef struct
{
elemType *base;
elemType *top;
int stack_size;
}sqstack;
status init_stack(sqstack &s)
{
s.base = (elemType*)malloc(stack_init_size * sizeof(elemType));
if(!s.base)
return ERROR;
s.top = s.base;
s.stack_size = stack_init_size;
return OK;
}
status get_top(sqstack s,elemType &e)
{
if(s.base == s.top)
return ERROR;
else
{
e = *(s.top-1);
return OK;
}
}
status push_stack(sqstack &s,elemType e)
{
if((s.top - s.base) >= s.stack_size)
{
s.base = (elemType*)realloc(s.base,(s.stack_size + stack_increment)*sizeof(elemType));
if(!s.base)
exit(OVERFLOW);
s.top = s.base + s.stack_size;
s.stack_size += stack_increment;
}
*s.top++ = e;
return OK;
}
status pop_stack(sqstack &s)
{ elemType e;
if(s.top == s.base)
return ERROR;
e = *(s.top-1);
s.top--;
return e;
}
void main()
{
sqstack s;
int k = init_stack(s);
printf("k = %d/n",k);
elemType *a;
printf("压栈:/n");
for(int m = 0; m < 20;m++)
{
push_stack(s,m);
// printf("%d/n",*a);
}
a = s.base;
while(a != s.top)
{
printf("%d/n",*a);
a++;
}
// elemType *t;
//printf("%d/n",*s.base);
// printf("%d/n",*(s.top-1));
elemType temp,lk;
printf("出栈:/n");
for(lk = 0;lk < 20;lk++)
{
temp = pop_stack(s);
printf("%d/n",temp);
}
int sm;
get_top(s,sm);
printf("%d/n",sm);
}