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

来源:互联网 发布:mysql 添加分区出错 编辑:程序博客网 时间:2024/05/12 08:50

【代码】//文件名:sqstack.cpp

#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef 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;}


原创粉丝点击