栈的顺序存储结构实现
来源:互联网 发布:淘宝网学习视频 编辑:程序博客网 时间:2024/05/16 19:18
//栈和队列:顺序栈 #include<iostream>#include<cstdlib>#define MaxSize 100using namespace std;typedef char ElemType; typedef struct{ ElemType data[MaxSize]; int top; }SqStack;//初始化栈void InitStack(SqStack *&s){ s=(SqStack*)malloc(sizeof(SqStack)); s->top=-1;}//销毁栈void DestoryStack(SqStack *&s){ free(s);} //判断栈是否为空bool StackEmpty(SqStack *s){ return (s->top==-1);} //进栈:在栈不满的情况下,先将站顶指针增1,然后再栈顶指针指向位置插入元素ebool Push(SqStack *&s,ElemType e) { if(s->top==MaxSize-1) return false; s->top++; s->data[s->top]=e; return true;}//出栈:在栈不为空的情况下,先将栈顶元素赋给e,在将指针减1bool 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;}int main(){ SqStack *s; InitStack(*&s);}