#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//*****************************************************************************************************
typedef struct //定义顺序栈的结构
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S){ //创建新的栈
S.base=(SElemType *)malloc((STACK_INIT_SIZE)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top=S.base; //栈空标记
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status DestoryStack(SqStack &S){ //销毁栈
if(S.base)
free(S.base);
S.base=NULL;
return OK;
}
Status ClearStack(SqStack &S){ //清空栈
S.base=S.top;
return OK;
}
Status StackEmpty(SqStack &S){ //栈的判空
return (S.base==S.top);
}
Status GetTop(SqStack &S,SElemType &e){ //返回栈顶元素
if(S.top==S.base)
return ERROR;
e=*(S.top-1);
return OK;
}
Status Push(SqStack &S,SElemType e){ //入栈(插入e作为栈顶元素)
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e){ //出栈(删除栈顶元素,用e返回元素值)
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}
Status StackTraverse(SqStack &S){ //遍历顺序栈
SElemType *p;
p=S.top;
while(p>S.base){
p--;
cout<<*p;
}
return OK;
}
ElemType StackLength(SqStack S) //求栈长
{
return S.top-S.base;
}//StackLength
#define STACKINCREMENT 10
//*****************************************************************************************************
typedef struct //定义顺序栈的结构
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S){ //创建新的栈
S.base=(SElemType *)malloc((STACK_INIT_SIZE)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top=S.base; //栈空标记
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status DestoryStack(SqStack &S){ //销毁栈
if(S.base)
free(S.base);
S.base=NULL;
return OK;
}
Status ClearStack(SqStack &S){ //清空栈
S.base=S.top;
return OK;
}
Status StackEmpty(SqStack &S){ //栈的判空
return (S.base==S.top);
}
Status GetTop(SqStack &S,SElemType &e){ //返回栈顶元素
if(S.top==S.base)
return ERROR;
e=*(S.top-1);
return OK;
}
Status Push(SqStack &S,SElemType e){ //入栈(插入e作为栈顶元素)
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e){ //出栈(删除栈顶元素,用e返回元素值)
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}
Status StackTraverse(SqStack &S){ //遍历顺序栈
SElemType *p;
p=S.top;
while(p>S.base){
p--;
cout<<*p;
}
return OK;
}
ElemType StackLength(SqStack S) //求栈长
{
return S.top-S.base;
}//StackLength