关于顺序栈的基本操作
▲01初始化顺序栈,构造空栈S InitStack_Sq(SqStack &S)
▲02销毁栈 DestoryStack_Sq(SqStack &S)
▲03置空栈S ClearStack_Sq(SqStack &S)
▲04 判断栈是否为空 StackEmpty_Sq(SqStack S)
▲05返回栈S中元素个数 StackLength_Sq(SqStack S)
▲06用e返回S的栈顶元素 GetTop_Sq(SqStack S,ElemType &e)
▲07元素e进栈 Push_Sq(SqStack &S,const ElemType &e)
▲08出栈,并用e返回其值 Pop_Sq(SqStack &S,ElemType &e)
▲09遍历/访问顺序栈 StackTraverse_Sq(SqStack S)
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100//顺序栈存储空间的初始分配量
#define STACKINCREMENT 10//顺序栈存储空间的分配增量
#define OVERFLOW -2 //堆栈上溢
#define UNDERFLOW -3 //堆栈下溢
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType *base;//在栈构造之前和销毁之后,base的值为NULL
ElemType *top; //栈顶指针
int stacksize;//当前已分配的存储空间,以元素为单位
}SqStack;
//▲01初始化顺序栈,构造空栈S
Status InitStack_Sq(SqStack &S){
S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base){
exit(OVERFLOW);
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}
//▲02销毁栈
Status DestoryStack_Sq(SqStack &S){
free(S.base);
S.base=NULL;
S.top=NULL;
S.stacksize=0;
return 1;
}
//▲03置空栈S
Status ClearStack_Sq(SqStack &S){
S.top=S.base;
return 1;
}
//▲04 判断栈是否为空
Status StackEmpty_Sq(SqStack S){
if(S.top==S.base){
return 1;
}else{
return 0;
}
}
//▲05返回栈S中元素个数
int StackLength_Sq(SqStack S){
return S.top-S.base;
}
//▲06用e返回S的栈顶元素
Status GetTop_Sq(SqStack S,ElemType &e){
if(S.top==S.base){
return 0;
}
e=*(S.top-1);
return 1;
}
//▲07元素e进栈
Status Push_Sq(SqStack &S,const ElemType &e){
if(S.top-S.base>=S.stacksize){//栈满,追加存储空间
S.base=(ElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S.base){
exit(OVERFLOW);
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
//以下两句可以写成*S.top++=e;
*(S.top)=e;
S.top++;
}
//▲08出栈,并用e返回其值
Status Pop_Sq(SqStack &S,ElemType &e){
if(S.top==S.base){
return 0;
}
//以下两句可以写成e=*--S.top;
S.top--; //出栈栈顶指针先递减,再赋值
e=*(S.top);
}
//▲09遍历/访问S
Status StackTraverse_Sq(SqStack S){
ElemType *p=S.base;
while(p<S.top){
printf("%d ",*p++);
}
printf("\n");
}
int main(){
SqStack S;
int i;
ElemType e;
printf("———————————————————————▲01初始化顺序栈\n");
InitStack_Sq(S);
printf("———————————————————————▲04判空\n");
StackEmpty_Sq(S) ? printf("S 为空\n") : printf("S不为空\n");
printf("———————————————————————▲07 入栈\n");
for(i=1;i<=6;i++){
Push_Sq(S,i*4); //将i*4入栈
}
StackTraverse_Sq(S);
printf("当前栈内的元素个数为:%d\n",StackLength_Sq(S));
printf("———————————————————————▲06 返回栈顶元素\n");
GetTop_Sq(S,e);
printf("栈顶元素的值为:%d\n", e);
printf("———————————————————————▲07 出栈\n");
for(i=1;i<=4;i++){
Pop_Sq(S,e);
printf("第%d次出栈后的序列为:",i);
StackTraverse_Sq(S);
}
printf("此时栈内的元素个数为:%d\n",StackLength_Sq(S));
printf("———————————————————————▲03 置空\n");
printf("清空S前:");
StackEmpty_Sq(S) ? printf(" S为空!\n") : printf(" S不为空!\n");
ClearStack_Sq(S); //置空
printf("清空S后:");
StackEmpty_Sq(S) ? printf(" S为空!\n") : printf(" S不为空!\n");
printf("———————————————————————▲02 销毁\n");
printf("销毁S前:");
if(S.base!=NULL&&S.top!=NULL){
printf("S存在!\n");
} else{
printf("S不存在!\n");
}
DestoryStack_Sq(S); //销毁
printf("销毁S后:");
if(S.base!=NULL&&S.top!=NULL){
printf("S存在!\n");
} else{
printf("S不存在!\n");
}
return 0;
}