栈 函数的实现

来源:互联网 发布:mac 查杀进程 编辑:程序博客网 时间:2024/06/06 01:31
#ifndef _SEQSTACK_H#define _SEQSTACK_H#include<iostream>#include<assert.h>using namespace std;typedef int ElemType;#define STACK_INIT_SIZE 5#define STACKINCREMENT  2typedef struct Stack{ElemType *base;    int      top;int      capacity;}Stack;    bool IsFull(Stack *st)//判断栈满{    return st->top >= st->capacity;}bool IsEmpty(Stack *st)//判断栈空{return st->top == 0;}void InitStack(Stack *st)//初始化栈{st->base=(ElemType *)malloc(sizeof(ElemType)*STACK_INIT_SIZE);assert(st->base!=NULL);st->capacity = STACK_INIT_SIZE;st->top = 0;}bool Push(Stack *st, ElemType x)//进栈{if(IsFull(st))        st->base=(ElemType *)malloc(sizeof(ElemType)*(st->capacity+STACKINCREMENT));assert(st->base!=NULL);st->capacity+=STACKINCREMENT;st->base[st->top++] = x;return true;}bool Pop(Stack *st,ElemType *v)//用v接收出栈元素{{if(IsEmpty(st))cout<<"栈以空,不能出栈!"<<endl;return false;}*v = st->base[--st->top];return true;}ElemType GetTop(Stack *st)//取栈顶元素{if(IsEmpty(st)){cout<<"栈以空,不能出栈!"<<endl;}    return st->base[st->top-1];}void clear(Stack *st)//清空栈{st->top=0;}void destroy(Stack *st)//销毁栈{free(st->base);st->base = NULL;st->top = 0;st->capacity = 0;}void ShowStack(Stack *st)//显示{for(int i=st->top-1; i>=0; --i){cout<<st->base[i]<<endl;}}#endif

0 0
原创粉丝点击