第五周项目-建立顺序栈算法库

建设自己的专业基础设施算法库。

顺序栈算法库采用程序的多文件组织形式,包括两个文件: 

1.头文件:sqstack.h,包含定义顺序栈数据结构的代码、宏定义、要实现算法的函数的声明;

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

2.源文件:sqstack.cpp,包含实现各种算法的函数的定义

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


3.在同一项目(project)中建立一个源文件(如main.cpp),编制main函数,完成相关的测试工作。 例:

[csharp]  view plain  copy
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值