为了自己学习,每天都要更一点数据结构知识,嘿嘿!
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef char ElemType;
typedef struct
{
ElemType * base;
ElemType * top;
int stackSize;
}SqStack;
/*栈的初始化*/
void InitStack(SqStack *s)
{
s->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if (!s->base)exit(0);
s->top=s->base;
s->stackSize=STACK_INIT_SIZE;
}
/*压栈*/
void Push(SqStack *s,ElemType e)
{
if(s->top-s->base >= s->stackSize)
{
s->base=(ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(ElemType));
if(!s->base)exit(0);
}s->top++;
*(s->top)=e;
}
/*出栈*/
void Pop(SqStack *s,ElemType *e)
{
if (s->top==s->base)
{
return;
}
*e=*(s->top);(s->top)--;
}
/*栈量*/
int StackLen(SqStack s)
{
return s.top-s.base;
}