【王道数据结构】栈的顺序存储——共享栈
作者:
Skuy
,
2023-03-22 20:26:06
,
所有人可见
,
阅读 227
#include <stdio.h>
#define MaxSize 10
typedef struct {
int data[MaxSize];
int top0;
int top1;
} SqSList;
// 共享栈的初始化
void InitStack(SqSList &S)
{
S.top0 = -1;
S.top1 = MaxSize;
}
// 判空
bool StackEmpty(SqSList S)
{
if (S.top0 == -1 && S.top1 == MaxSize) return true;
else return false;
}
// 入栈
bool Push(SqSList &S, int e)
{
if (S.top1 - S.top0 == 1) return false;
S.data[++ S.top0] = e;
// S.data[-- S.top1] = e;
return true;
}
// 出栈
bool Pop(SqSList &S, int &x)
{
if (S.top0 == -1 || S.top1 == MaxSize) return false;
x = S.data[S.top0 --];
// x = S.data[S.top1 ++];
return true;
}
// 读取栈顶元素
bool GetTop(SqSList S, int &x)
{
if (S.top0 == -1 || S.top1 == MaxSize) return false;
x = S.data[S.top0];
// x = S.data[S.top1];
return true;
}
void test()
{
SqSList S;
InitStack(S);
}
int main()
{
test();
return 0;
}