#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define Maxsize 50
typedef int ElemType;
typedef struct{
ElemType data[Maxsize];
ElemType top;
}SqStack;
//初始化栈
void IniStack(SqStack &S)
{
S.top = -1;
}
//判断栈是否为空
bool SaStackEmpty(SqStack S)
{
if (-1 == S.top)
return true;
}
//元素入栈
bool push(SqStack &S, ElemType x)
{
if (S.top==Maxsize-1)
{
return false;//栈为满
}
S.data[++S.top]=x;
return true;
}
//获取栈顶元素
bool GetTop(SqStack S, ElemType &m)
{
if (S.top == -1) {
return false;
}
m = S.data[S.top];
return true;
}
//元素出栈
bool Pop(SqStack& S, ElemType& x)
{
if (SaStackEmpty(S))
{
return false;
}
x = S.data[S.top--];
return true;
}
int main()
{
SqStack S;
IniStack(S);
ElemType m;//存放栈顶元素
bool flag;
flag = SaStackEmpty(S);
if (flag)
{
printf("栈是空的");
}
push(S, 3);//元素入栈
push(S, 4);
push(S, 5);
flag=GetTop(S, m);//获取栈顶元素
if (flag)
{
printf("\n栈顶元素为%d\n", m);
}
flag=Pop(S, m);
{
printf("弹出的元素为%d\n", m);
}
}
栈(23王道督学营)
最新推荐文章于 2025-08-25 16:42:42 发布