您的位置:首页 > 其它

顺序栈各种基本运算的算法

2017-10-18 23:16 337 查看
【代码】//文件名:sqstack.cpp

#include <stdio.h>
#include <malloc.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) //判断栈是否为空
{ return s->top==-1;
}
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: