#include<iostream>
#define MAXSIZE 100
#define OVERFLOW -1
#define OK 1
#define ERROR 0
using namespace std;
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
//构造一个空栈;
Status InitStack(SqStack &S){
SqStack *base,*top;
S.base = new SElemType[MAXSIZE] ;
if(!S.base) return OVERFLOW;
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
//判断栈是否为空
bool StackEmpty(SqStack S){
if(S.top==S.base)
return true;
else
return false;
}
//求栈的长度
int StackLength(SqStack S){
return S.top-S.base;
}
//清空顺序栈
Status ClearStack(SqStack &S){
if(S.base) S.top =S.base;
return OK;
}
//销毁顺序栈
Status DestroyStack(SqStack &S){
if(S.base) {
delete S.base;
S.stacksize=0;
S.top= S.base=NULL;
}
return OK;
}
//入栈
Status Push(SqStack &S,SElemType e){
if(S.top-S.base==MAXSIZE)
return ERROR;
*S.top++ = e;
return OK;
}
//出栈
Status Pop(SqStack &S,SElemType e){
if(S.base==S.top)
return ERROR;
e = *--S.top;
return OK;
}
//取顶元素
Status GetTop(SqStack &S,SElemType &e) {
if(S.top==S.base)
return ERROR;
e = *(S.top-1);
return OK;
}
int main(){
SqStack s;
SElemType a,b;
cout<<"进栈元素是";
if(InitStack(s)==OK){
for(a=1;a<=20;a++){
Push(s,a);
cout<<a<<" ";
}
}
cout<<"出栈元素是";
while(GetTop(s,b)==OK){
cout<<b<<" ";
Pop(s,b);
}
}
#define MAXSIZE 100
#define OVERFLOW -1
#define OK 1
#define ERROR 0
using namespace std;
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
//构造一个空栈;
Status InitStack(SqStack &S){
SqStack *base,*top;
S.base = new SElemType[MAXSIZE] ;
if(!S.base) return OVERFLOW;
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
//判断栈是否为空
bool StackEmpty(SqStack S){
if(S.top==S.base)
return true;
else
return false;
}
//求栈的长度
int StackLength(SqStack S){
return S.top-S.base;
}
//清空顺序栈
Status ClearStack(SqStack &S){
if(S.base) S.top =S.base;
return OK;
}
//销毁顺序栈
Status DestroyStack(SqStack &S){
if(S.base) {
delete S.base;
S.stacksize=0;
S.top= S.base=NULL;
}
return OK;
}
//入栈
Status Push(SqStack &S,SElemType e){
if(S.top-S.base==MAXSIZE)
return ERROR;
*S.top++ = e;
return OK;
}
//出栈
Status Pop(SqStack &S,SElemType e){
if(S.base==S.top)
return ERROR;
e = *--S.top;
return OK;
}
//取顶元素
Status GetTop(SqStack &S,SElemType &e) {
if(S.top==S.base)
return ERROR;
e = *(S.top-1);
return OK;
}
int main(){
SqStack s;
SElemType a,b;
cout<<"进栈元素是";
if(InitStack(s)==OK){
for(a=1;a<=20;a++){
Push(s,a);
cout<<a<<" ";
}
}
cout<<"出栈元素是";
while(GetTop(s,b)==OK){
cout<<b<<" ";
Pop(s,b);
}
}