#ifndef _SEQSTACK_H
#define _SEQSTACK_H
#include<iostream>
#include<assert.h>
using namespace std;
typedef int ElemType;
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 2
typedef 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
栈 函数的实现
最新推荐文章于 2020-07-08 23:21:31 发布