该程序定义了顺序栈(栈的顺序存储结构)的存储结构,并实现了顺序栈的基本操作,例如:初始化、销毁、查找、插入、删除、遍历等。
//SqStack.h
该头文件定义了顺序栈的存储结构,对顺序栈的基本操作的函数原型进行了声明。
- #include "windows.h"
- //定义函数结果状态代码
- #define TRUE 1
- #define FALSE 0
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -1
- #define UNDERFLOW -2
- //定义函数返回值变量的类型
- typedef INT32 Status;
- //定义顺序栈的数据元素的类型
- typedef INT32 ElemType;
- #pragma once
- //定义顺序栈的存储结构
- #define INCREMENTSIZE 10 //存储空间分配增量
- typedef struct{
- ElemType *bottom; //栈底指针
- ElemType *top; //栈顶指针
- LONG32 size; //栈存储空间大小
- }SqStack;
- //声明相关的辅助函数
- Status output(ElemType);
- //定义函数指针类型
- typedef Status (*pOutput)(ElemType);
- //声明顺序栈的基本操作
- Status InitStack(SqStack&,LONG32);
- Status DestroyStack(SqStack&);
- Status ClearStack(SqStack&);
- Status StackEmpty(SqStack&);
- LONG32 StackLength(SqStack&);
- Status GetTop(SqStack&,ElemType&);
- Status Push(SqStack&,ElemType);
- Status Pop(SqStack&,ElemType&);
- Status StackTraverse(SqStack&,pOutput);
//SqStack.cpp
该源文件实现了顺序栈的基本操作
- #include "stdafx.h"
- #include "windows.h"
- #include "c_lib.h"
- #include "cpp_lib.h"
- #include "SqStack.h"
- Status output(ElemType e)
- {
- cout<<e<<" ";
- return OK;
- }
- Status InitStack(SqStack &S,LONG32 _size)
- //操作结果:构造一个空栈S
- {
- S.bottom=(ElemType*)malloc(_size*sizeof(ElemType));
- if(S.bottom == NULL){
- cout<<"严重错误:顺序栈初始存储空间分配失败,程序退出";
- exit(EXIT_FAILURE);
- }
- S.size=_size;
- S.top=S.bottom;
- return OK;
- }
- Status DestroyStack(SqStack &S)
- //初始条件:栈S已存在
- //操作结果:栈S已被销毁
- {
- free(S.bottom);
- S.bottom=S.top=NULL; //S.bottom的值为NULL,表示顺序栈不存在
- S.size=0;
- return OK;
- }
- Status ClearStack(SqStack &S)
- //初始条件:栈S已存在
- //操作结果:将S清为空栈
- {
- S.top=S.bottom; //S.top == S.bottom作为顺序栈空的标记
- return OK;
- }
- Status StackEmpty(SqStack &S)
- //初始条件:栈S已存在
- //操作结果:若栈S为空栈,则返回TRUE,否则FALSE
- {
- if(S.top == S.bottom)
- return TRUE;
- else
- return FALSE;
- }
- LONG32 StackLength(SqStack &S)
- //初始条件:栈S已存在
- //操作结果:返回S的元素个数,即栈的长度
- {
- return S.top-S.bottom;
- }
- Status GetTop(SqStack &S,ElemType &e)
- //初始条件:栈S已存在且非空
- //操作结果:用e返回S的栈顶元素
- {
- if( StackEmpty(S) == TRUE ){
- cout<<"访问栈顶元素发生错误:栈为空"<<endl;
- return ERROR;
- }
- return *(S.top-1);
- }
- Status Push(SqStack &S,ElemType e)
- //初始条件:栈S已存在
- //操作结果:插入元素e为新的栈顶元素
- {
- LONG32 length=StackLength(S);
- if( length >= S.size ){
- ElemType *newbase=(ElemType*)realloc(S.bottom,(S.size+INCREMENTSIZE)*sizeof(ElemType));
- if(newbase == NULL){
- cout<<"严重错误:顺序栈增量存储空间分配失败,程序退出!";
- exit(EXIT_FAILURE);
- }
- S.bottom=newbase;
- S.top=S.bottom+length;
- S.size+=INCREMENTSIZE;
- }
- *(S.top++)=e;
- return OK;
- }
- Status Pop(SqStack &S,ElemType &e)
- //初始条件:栈S已存在且非空
- //操作结果:删除S的栈顶元素,并且用e返回其值
- {
- if(StackEmpty(S) == TRUE){
- cout<<"顺序栈为空,出栈失败!"<<endl;
- return ERROR;
- }
- e=*(--S.top);
- return OK;
- }
- Status StackTraverse(SqStack &S,pOutput outFun)
- //初始条件:栈S已存在且非空
- //操作结果:从栈底到栈顶依次对S的每个数据元素调用函数output()。一旦output失败,则操作失效
- {
- if(S.bottom == NULL){
- cout<<"严重错误:栈不存在,程序退出!";
- exit(EXIT_FAILURE);
- }
- for(LONG32 i=0;i < StackLength(S);i++)
- outFun(S.bottom[i]);
- cout<<endl;
- return OK;
- }
//test.cpp
该源文件为测试程序
- #include "stdafx.h"
- #include "windows.h"
- #include "c_lib.h"
- #include "cpp_lib.h"
- #include "SqStack.h"
- INT32 RangedRand( int range_min, int range_max)
- {
- // Generate random numbers in the half-closed interval
- // [range_min, range_max). In other words,
- // range_min <= random number < range_max
- INT32 u =(INT32)( (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
- + range_min );
- return u;
- }
- int main()
- {
- SqStack S;
- InitStack(S,100);
- srand( (unsigned)time( NULL ) );
- ElemType data[200];
- for(LONG32 i=0;i<200;i++){
- data[i]=RangedRand(-500,500);
- Push(S,data[i]);
- }
- StackTraverse(S,output);
- cout<<"顺序栈的长度是:"<<StackLength(S)<<endl;
- ElemType e;
- Pop(S,e);
- cout<<"栈顶元素"<<e<<"出栈"<<endl;
- cout<<"此时的顺序栈:"<<endl;
- StackTraverse(S,output);
- cout<<"顺序栈的长度是:"<<StackLength(S)<<endl;
- DestroyStack(S);
- StackTraverse(S,output);
- cout<<"顺序栈的长度是:"<<StackLength(S)<<endl;
- return 0;
- }