#include <iostream>
using namespace std;
typedef int ElemType;
//栈的结构类型
#define MaxSize 50//定义栈中元素的最大个数
typedef struct
{
ElemType data[MaxSize];//存放栈中元素
int top;//栈顶指针
}SqStack;
//初始化栈
void InitStack(SqStack &S)
{
S.top=-1;
}
//判断栈空
bool StackEmpty(SqStack S)
{
cout<<"判断栈空的结果为:";
if(S.top==-1)
{
cout<<"栈空"<<endl;
return true;
}
else
{
cout<<"栈不空"<<endl;
return false;
}
}
//进栈
bool Push(SqStack &S, ElemType x)
{
if(S.top==MaxSize-1)//判断栈满则无法进栈
return false;
S.data[++S.top]=x;//先改变指针在放元素
return true;
}
//出栈
bool Pop(SqStack &S,ElemType &x)
{
if(S.top==-1)//判断栈空则无法出栈
return false;
x=S.data[S.top--];//元素先出栈再改变指针
return true;
}
//读栈顶元素
bool GetTop(SqStack S,ElemType &x)
{
if(S.top==-1)
return false;
x=S.data[S.to
C++顺序栈的实现(进栈,出栈,判断栈空,打印输出,获取栈顶元素)
最新推荐文章于 2023-10-19 21:08:17 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)