数据结构上机实践第五周项目1- 建立顺序栈算法库

来源:互联网 发布:知乎 发现 编辑:程序博客网 时间:2024/06/07 09:25
建立顺序栈算法库
本次实践将建立顺序栈的算法库,用以解决更多的工程问题,提供便利。(编译环境:VC++6.0)
建立算法库将会用到多文件组织工程的建立,本次实践不再细说,点击此处可参考。
建立好的工程文件视角图如下:

程序源码如下:

1.sqstack.h
  1.    
  2. #ifndef SQSTACK_H_INCLUDED  
  3. #define SQSTACK_H_INCLUDED  
  4.   
  5. #define MaxSize 100  
  6. typedef char ElemType;  
  7. typedef struct  
  8. {  
  9.     ElemType data[MaxSize];  
  10.     int top;                //栈指针  
  11. } SqStack;                  //顺序栈类型定义  
  12.   
  13. void InitStack(SqStack *&s);    //初始化栈  
  14. void DestroyStack(SqStack *&s);  //销毁栈  
  15. bool StackEmpty(SqStack *s);     //栈是否为空  
  16. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  17. bool Push(SqStack *&s,ElemType e); //入栈  
  18. bool Pop(SqStack *&s,ElemType &e); //出栈  
  19. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  20. void DispStack(SqStack *s);  //输出栈  
  21.   
  22. #endif // SQSTACK_H_INCLUDED  


2.sqstack.cpp
  1.      
  2. #include <stdio.h>  
  3. #include <malloc.h>  
  4. #include "sqstack.h"  
  5.   
  6. void InitStack(SqStack *&s)  
  7. {  
  8.     s=(SqStack *)malloc(sizeof(SqStack));  
  9.     s->top=-1;  
  10. }  
  11. void DestroyStack(SqStack *&s)  
  12. {  
  13.     free(s);  
  14. }  
  15. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  16. {  
  17.     return(s->top+1);  
  18. }  
  19. bool StackEmpty(SqStack *s)  
  20. {  
  21.     return(s->top==-1);  
  22. }  
  23. bool Push(SqStack *&s,ElemType e)  
  24. {  
  25.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  26.         return false;  
  27.     s->top++;  
  28.     s->data[s->top]=e;  
  29.     return true;  
  30. }  
  31. bool Pop(SqStack *&s,ElemType &e)  
  32. {  
  33.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  34.         return false;  
  35.     e=s->data[s->top];  
  36.     s->top--;  
  37.     return true;  
  38. }  
  39. bool GetTop(SqStack *s,ElemType &e)  
  40. {  
  41.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  42.         return false;  
  43.     e=s->data[s->top];  
  44.     return true;  
  45. }  
  46.   
  47. void DispStack(SqStack *s)  //输出栈  
  48. {  
  49.     int i;  
  50.     for (i=s->top;i>=0;i--)  
  51.         printf("%c ",s->data[i]);  
  52.     printf("\n");  
  53. }  


3.main.cpp
[
  1. #include <stdio.h>  
  2. #include "sqstack.h"  
  3.   
  4. int main()  
  5. {  
  6.     ElemType e;  
  7.     SqStack *s;  
  8.     printf("(1)初始化栈s\n");  
  9.     InitStack(s);  
  10.     printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  11.     printf("(3)依次进栈元素a,b,c,d,e\n");  
  12.     Push(s,'a');  
  13.     Push(s,'b');  
  14.     Push(s,'c');  
  15.     Push(s,'d');  
  16.     Push(s,'e');  
  17.     printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  18.     printf("(5)栈长度:%d\n",StackLength(s));  
  19.     printf("(6)从栈顶到栈底元素:");DispStack(s);  
  20.     printf("(7)出栈序列:");  
  21.     while (!StackEmpty(s))  
  22.     {  
  23.         Pop(s,e);  
  24.         printf("%c ",e);  
  25.     }  
  26.     printf("\n");  
  27.     printf("(8)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  28.     printf("(9)释放栈\n");  
  29.     DestroyStack(s);  
  30.     return 0;  
  31. }  


测试函数运行结果如下:
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 自住房申请 商业住房 普通住房 农村住房 商品住房 公寓住房 住房精装修 公住房 老人住房 住房按揭贷款 住房公积金有什么用 住房贷款计算器 住房贷款利率 住房公积金贷款计算器 陕西省住房公积金 住房公积金什么用 西安市住房保障和房屋管理局 西安市住房保障房屋管理局 陕西省住房公积金查询 住房贷款利率调整 济宁住房公积金 住房公积金管理中心 个人住房贷款调整 济宁住房公积金查询 住房公积金电话 沧州住房公积金 北京住房公积金管理中心 四川住房和城乡建设厅 佛山市住房公积金管理中心 住房公积金查询个人账户 19住房商业贷款利率 四川省住房和城乡建设厅首页 住房商业贷款利率 佛山住房公积金管理中心 四川住房城乡建设厅 西安住房公积金管理中心 住房公积金贷款额度 沧州住房公积金查询 个人住房公积金查询入口 银行住房贷款利率计算器 合肥市住房公积金管理中心