#include<stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];//数据域
int top;//栈顶指针(类似指针的作用,并非指针)
}SqStack;
//初始化栈
bool InitStack(SqStack &L)
{
L.top = -1;//仅作初始化
return true;
}
//判空
bool IsEmpty(SqStack &L)
{
if(L.top == -1)
return true;
else
return false;
}
//压数据进栈操作
bool Push(SqStack &L, int x)
{
if(L.top == MaxSize-1)
return false;
L.top += 1;
L.data[L.top] = x;
return true;
}
//出栈操作(并获取栈顶元素)
bool Pop(SqStack &L, int &x)
{
if(L.top == -1)
return false;
x = L.data[L.top];
L.top--;
return true;
}
//读栈顶元素
bool GetTop(SqStack &L, int &x)
{
if(L.top == -1)
return false;
x = L.data[L.top];
return true;
}
int main()
{
SqStack L;
InitStack(L);
Push(L,7);
int x=0;
GetTop(L,x);
printf("%d",IsEmpty(L));
return 0;
}
栈的顺序存储实现(C语言)
最新推荐文章于 2022-08-02 23:01:19 发布