#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERLOAD -2
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}Sqstack;
void InitStack(Sqstack &s)//初始化栈
{
s.base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!s.base)
{
exit(OVERLOAD);
}
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
}
int GetTop(Sqstack s, ElemType &e)
{
if(s.top == s.base) return ERROR;
e = *(s.top-1);
return OK;
}
void Push(Sqstack &s, ElemType e)//入栈
{
if(s.top-s.base>=s.stacksize)
{
s.base = (ElemType*)realloc(s.base, (s.stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!s.base) exit(OVERLOAD);
s.top = s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++ = e;
}
int Pop(Sqstack &s, ElemType &e)//出栈,并将元素赋给e
{
if(s.top == s.base) return ERROR;
e = *--s.top;
return OK;
}
int main()
{
}
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERLOAD -2
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}Sqstack;
void InitStack(Sqstack &s)//初始化栈
{
s.base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!s.base)
{
exit(OVERLOAD);
}
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
}
int GetTop(Sqstack s, ElemType &e)
{
if(s.top == s.base) return ERROR;
e = *(s.top-1);
return OK;
}
void Push(Sqstack &s, ElemType e)//入栈
{
if(s.top-s.base>=s.stacksize)
{
s.base = (ElemType*)realloc(s.base, (s.stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!s.base) exit(OVERLOAD);
s.top = s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++ = e;
}
int Pop(Sqstack &s, ElemType &e)//出栈,并将元素赋给e
{
if(s.top == s.base) return ERROR;
e = *--s.top;
return OK;
}
int main()
{
}