//实现顺序栈各种基本运算的算法
#include<>
#include<>
#define MaxSize 50
typedef int ElemType;
typedef struct{
ElemType data[MaxSize]; //数据域
int top; //栈顶指针(保存栈顶元素下标)
}SqStack;
//初始化栈
void InitStack(SqStack *&s){
s=(SqStack *)malloc(sizeof(SqStack));
s->top = -1; //注意top初始化位置
}
//销毁顺序栈
void DestroyStack(SqStack *&s){
free(s);
}
//判断栈是否为空
bool StackEmpty(SqStack *s){
return(s->top == -1); //为空返回turn,否则返回false
}
//进栈
bool Push(SqStack *&s,ElemType e){
if(s -> top == MaxSize -1) //如果栈满,返回false
return false;
s->top++; //栈顶指针加一
s->data[s->top] = e; //进栈
return true;
}
//出栈
bool Pop(SqStack *&s,ElemType &e){
if(s->top == -1)
return false;
e=s->data[s->top]; //先出栈
s->top--; //栈顶指针减一
return true;
}
//取栈顶元素
bool GetTop(SqStack * s,ElemType &e){
if(s->top == -1)
return false;
e=s->data[s->top]; //栈顶指针不变
return true;
}