欢迎来到天天文库
浏览记录
ID:35342685
大小:70.01 KB
页数:8页
时间:2019-03-23
《数据结构(c语言版)顺序栈(完整可运行程序)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、调试结果:33C:windowssystem3Acmd.exe数整■I-J进3进:F十:1十狹•io•2、pp,为单文件可运行的程序。/*pubuse.h*/#include#include#include/*malloc()等*/#include/*INT_MAX等*/#include/*EOF(=AZ或F6)ZNULL*/#include/*atoi()*/#include/*eof()*/#include/*floor(),ceil(),abs()*/#include/*exit()*//*3、函数结果状态代码*/#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1/*#defineOVERFLOW-2因为在math.h中己定义0VERFLOW的值为3,故去掉此行*/typedefintStatus;/*Status是函数的类型,其值是函数结果状态代码,如OK等*/typedefintBoolean;/*Boolean是布尔类型,其值是TRUE或FALSE*//*Def.h*/#defineSTACK_INIT_SIZE10/*存储空间初始分配暈*/#defin4、eSTACKINCREMENT2/*存储空间分增量*/typedefstructSqStack{SEIemType*base;/*在栈构造之前和销毁之后,base的值为NULL*/SEIemType*top;/*栈顶指针*/intstacksize;/*当前已分配的存储空间,以元素为单位*/JSqStack;/*顺序栈*//*Algo.h*/StatuslnitStack(SqStack&S){/*构造一个空栈S*/S.base=(SEIemType*)malloc(STACK_INIT_SIZE*sizeof(SEIemType));if(!S.ba5、se)exit(OVERFLOW);/*存储分配失败*/S.top=S.base;S.stacksize=STACK」NIT_SIZE;returnOK;}StatusDestroyStack(SqStack&S){/*销毁栈S,S不再存在*/free(S.base);S.base=NULL;S.top=NULL;S.stacksize=O;returnOK;}StatusClearStack(SqStack&S){/*把S置为空栈*/S.top二S.base;returnOK;}StatusStackEmpty(SqStackS){/*若栈s为空栈,6、则返回TRUE,否则返回FALSE*/if(S.top==S.base)returnTRUE;elsereturnFALSE;}intStackLengthfSqStackS){/*返回S的元素个数,即栈的长度*/returnS.top-S.base;}StatusGetTop(SqStackS,SEIemType&e){/*若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR*/if(S.top>S.base){e=*(S.top-l);returnOK;}elsereturnERROR;}StatusPush(SqStack&S,SEI7、emTypee){/*插入元素e为新的栈顶元素*/if(S.top-S.base>=S.stacksize)/*栈满,追加存储空间*/{S.base=(SEIemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SEIemType));if(!S.base)exit(OVERFLOW);/*存储分配失败S.top二S.base+S.stacksize;S.stacksize+二STACKINCREMENT;}*(S.top)++=e;returnOK;}StatusPop(SqStack&8、S,SEIemType&e){/*若栈不空,则删除S的栈顶元素,用e返回其值,并返回0K;否则
2、pp,为单文件可运行的程序。/*pubuse.h*/#include#include#include/*malloc()等*/#include/*INT_MAX等*/#include/*EOF(=AZ或F6)ZNULL*/#include/*atoi()*/#include/*eof()*/#include/*floor(),ceil(),abs()*/#include/*exit()*//*
3、函数结果状态代码*/#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1/*#defineOVERFLOW-2因为在math.h中己定义0VERFLOW的值为3,故去掉此行*/typedefintStatus;/*Status是函数的类型,其值是函数结果状态代码,如OK等*/typedefintBoolean;/*Boolean是布尔类型,其值是TRUE或FALSE*//*Def.h*/#defineSTACK_INIT_SIZE10/*存储空间初始分配暈*/#defin
4、eSTACKINCREMENT2/*存储空间分增量*/typedefstructSqStack{SEIemType*base;/*在栈构造之前和销毁之后,base的值为NULL*/SEIemType*top;/*栈顶指针*/intstacksize;/*当前已分配的存储空间,以元素为单位*/JSqStack;/*顺序栈*//*Algo.h*/StatuslnitStack(SqStack&S){/*构造一个空栈S*/S.base=(SEIemType*)malloc(STACK_INIT_SIZE*sizeof(SEIemType));if(!S.ba
5、se)exit(OVERFLOW);/*存储分配失败*/S.top=S.base;S.stacksize=STACK」NIT_SIZE;returnOK;}StatusDestroyStack(SqStack&S){/*销毁栈S,S不再存在*/free(S.base);S.base=NULL;S.top=NULL;S.stacksize=O;returnOK;}StatusClearStack(SqStack&S){/*把S置为空栈*/S.top二S.base;returnOK;}StatusStackEmpty(SqStackS){/*若栈s为空栈,
6、则返回TRUE,否则返回FALSE*/if(S.top==S.base)returnTRUE;elsereturnFALSE;}intStackLengthfSqStackS){/*返回S的元素个数,即栈的长度*/returnS.top-S.base;}StatusGetTop(SqStackS,SEIemType&e){/*若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR*/if(S.top>S.base){e=*(S.top-l);returnOK;}elsereturnERROR;}StatusPush(SqStack&S,SEI
7、emTypee){/*插入元素e为新的栈顶元素*/if(S.top-S.base>=S.stacksize)/*栈满,追加存储空间*/{S.base=(SEIemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SEIemType));if(!S.base)exit(OVERFLOW);/*存储分配失败S.top二S.base+S.stacksize;S.stacksize+二STACKINCREMENT;}*(S.top)++=e;returnOK;}StatusPop(SqStack&
8、S,SEIemType&e){/*若栈不空,则删除S的栈顶元素,用e返回其值,并返回0K;否则
此文档下载收益归作者所有