头文件:
#include<iostream.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *base; //在栈构造之前和销毁之后,base的值为NULL
SElemType *top; //栈顶指针
int stacksize; //当前已分配的存储空间,以元素为单位
}SqStack;
Status InitStack(SqStack &S); //构造一个空栈
Status DestroyStack(SqStack &S); //销毁栈
Status ClearStack(SqStack &S); //清空栈
Status StackEmpty(SqStack S); //判断是否为空栈
Status IsSqStackFull(SqStack &S); //判断是否为满
Status StackLength(SqStack S); //返回栈元素的元素个数
Status GetTop(SqStack &S,SElemType &e); //若栈不为空,则用e返回栈顶元素,并返回OK;否则返回error
Status Push(SqStack &S,SElemType e); //插入元素e为新的栈顶元素
Status Pop(SqStack &S,SElemType &e); //若栈不为空,则删除栈顶元素,用e返回其值,并返回OK;否则返回error
Status PrintSqStack(SqStack &S); //输出栈元素
//构造一个空栈
Status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)
{
cout<<"存储分配失败!"<<endl;
return 0;
}
S.top=S.base; //分配后为空栈的标志
S.stacksize=STACK_INIT_SIZE;
cout<<"初始化完成!"<<endl;
return 1;
}
//销毁栈
Status DestroyStack(SqStack &S)
{
if(!S.base)
{
cout<<"不存在栈!"<<endl;
return 0;
}
free(S.base);
S.base=NULL;
S.top=NULL;
S.stacksize=0;
cout<<"栈已销毁!"<<endl;
return 1;
}
//清空栈
Status ClearStack(SqStack &S)
{
if(!S.base)
{
cout<<"不存在栈!"<<endl;
return 0;
}
S.top=S.base;
S.stacksize=0;
return 1;
}
//判断是否为空栈
Status StackEmpty(SqStack S)
{
if(!S.base)
{
cout<<"不存在栈!"<<endl;
return 0;
}
if(S.base==S.top)
{
cout<<"该栈为空栈!"<<endl;
}
return 1;
}
//返回栈元素的元素个数
Status StackLength(SqStack S)
{
if(!S.base)
{
cout<<"不存在栈!"<<endl;
return 0;
}
int i=0;
while(S.base<=S.top-1)
{
i++;
S.top=S.top-1;
}
cout<<"栈元素个数为:"<<i<<endl;
return 1;
}
//若栈不为空,则用e返回栈顶元素,并返回OK;否则返回error
Status GetTop(SqStack &S,SElemType &e)
{
if(S.base==S.top)
{
cout<<"栈为空!不存在栈顶元素!"<<endl;
return 0;
}
e=*(S.top-1);
cout<<"栈顶元素为:"<<e<<endl;
return 1;
}
//栈是否为空
Status IsSqStackEmpty(SqStack &S)
{
if(S.top==S.base )
{
cout<<"栈为空!"<<endl;
return 1;
}
else
{
cout<<"栈不为空!"<<endl;
return 0;
}
}
//栈是否已满!
Status IsSqStackFull(SqStack &S)
{
if(S.top>=S.base+STACK_INIT_SIZE)
{
cout<<"栈已满!"<<endl;
return 1;
}
else
{
cout<<"栈未满!"<<endl;
return 0;
}
}
//插入元素e为新的栈顶元素
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize) //栈满,追加存储空间
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
{
cout<<"存储分配失败!"<<endl;
return 0;
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT; //增加存储容量
}
*S.top++=e;
/*
S.base[S.top]=e;
S.top=S.top+1;
*/
return 1;
}
//若栈不为空,则删除栈顶元素,用e返回其值,并返回OK;否则返回error
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)
{
cout<<"栈为空!"<<endl;
return 0;
}
e=*(S.top-1);
S.top=S.top-1;
//e=*--S.top;
cout<<e<<endl;
return 1;
}
/*
status GetSqStackTop(SqStack &s,ElementType &e)
{
if(s.top >s.base)
{
e=*(s.top -1);
return 1;
}
else
return 0;
}
*/
//输出栈元素
Status PrintSqStack(SqStack &S)
{
SElemType *p;
for(p=S.base; p<S.top; p++)
{
cout<<*p<<",";
}
cout<<endl;
return 1;
}
源文件:
#include"stack.h"
void main()
{
SqStack S;
SElemType e;
InitStack(S);
int m,n;
cout<<"请输入要插入的元素个数:";
cin>>n;
for(int i=0; i<n; i++)
{
cout<<"输入插入元素为:";
cin>>m;
Push(S,m);
}
PrintSqStack(S);//输出栈元素
StackEmpty(S);//判断是否为空
StackLength(S);//长度
cout<<"栈顶元素为:";
Pop(S,e);
IsSqStackFull(S);//判断是否满
ClearStack(S);//清空
StackEmpty(S);//判断是否为空
DestroyStack(S);//销毁
}
执行结果: