#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;
}
顺序栈的操作实现
最新推荐文章于 2022-09-28 22:37:19 发布