b站 《数据结构与算法基础(青岛大学-王卓)》
所用教材《数据结构C语言版第二版》 中国工信出版集团 人民邮电出版社出版 严蔚敏 编著
#include<iostream>
using namespace std;
#define MAXSIZE 100
class SElemType {
public:
char name;
};
class SqStack {
public:
SElemType* base; //栈底指针
SElemType* top; //栈顶指针
int stacksize; //栈的最大容量
};
void InitStack(SqStack &S)
{
S.base = new SElemType[MAXSIZE];
if (!S.base) {
exit(1);
}
S.top = S.base;
S.stacksize = MAXSIZE;
}
bool StackEmpty(SqStack S) //栈空则输出true,否则输出false
{
if (S.base == S.top)
return true;
else return false;
}
int StackLength(SqStack S) //返回栈的长度
{
return S.top - S.base;
}
void ClearStack(SqStack S) //清空顺序栈
{
if (S.base)
{
S.top = S.base;
}
}
void DestroyStack(SqStack S) //销毁顺序栈
{
delete S.base;
S.base = S.top = NULL;
S.stacksize = 0;
}
int push(SqStack S, SElemType e) //入栈操作
{
if (S.top - S.base != S.stacksize) {
*S.top++ = e;
return 0;
}
else
return 1;
}
int pop(SqStack S, SElemType& e) //出栈操作
{
if (S.base == S.top) {
return 1;
}
else{
S.top --; //e=*--S.top
e = *S.top;
return 0;
}
}