#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
//初始化
void InitStack(SqStack* &s)
{
s=(SqStack*)malloc(sizeof(SqStack));
s->top=-1;
}
//销毁
void DestroyStack(SqStack* &s)
{
free(s);
}
//判断是否为空
bool StackEmpty(SqStack* &s)
{
if(s->top==-1)
return true;
else
return false;
}
//入栈
bool Push(SqStack* &s,ElemType &e)
{
if(s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
//出栈
bool Pop(SqStack* &s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];//取栈顶元素
s->top--;
return true;
}
//得到栈顶元素
bool GetTop(SqStack* &s,ElemType &e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
return true;
}
//输出顺序栈
void printtStack(SqStack* &s)
{
while(s->top!=-1)
{
printf("%d ",s->data[s->top]);
s->top--;
}
}
int main()
{
bool a;
int b=5;
SqStack *s;
InitStack(s);
a=Push(s,b);//c语言传参引用
printf("%d\n",a);
printtStack(s);
return 0;
}顺序栈的操作实现
最新推荐文章于 2025-06-17 23:55:47 发布
本文介绍了一种使用C语言实现顺序栈的方法,并提供了完整的代码示例。文章详细解释了顺序栈的基本操作,如初始化、销毁、判断空栈、入栈、出栈及获取栈顶元素等。
2534

被折叠的 条评论
为什么被折叠?



