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