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

问题及代码:

main.cpp:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*            
  2. Copyright (c)2016,烟台大学计算机与控制工程学院            
  3. All rights reserved.            
  4. 文件名称:顺序栈算法库.cpp            
  5. 作    者:   陈朋       
  6. 完成日期:2016年9月29日            
  7. 版 本 号:v1.0               
  8. 问题描述:       
  9. 输入描述:无           
  10. 程序输出:若干数据。         
  11. */   
  12. #include <stdio.h>  
  13. #include "sqstack.h"  
  14.   
  15. int main()  
  16. {  
  17.     ElemType e;  
  18.     SqStack *s;  
  19.     printf("(1)初始化栈s\n");  
  20.     InitStack(s);  
  21.     printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  22.     printf("(3)依次进栈元素a,b,c,d,e\n");  
  23.     Push(s,'a');  
  24.     Push(s,'b');  
  25.     Push(s,'c');  
  26.     Push(s,'d');  
  27.     Push(s,'e');  
  28.     printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  29.     printf("(5)栈长度:%d\n",StackLength(s));  
  30.     printf("(6)从栈顶到栈底元素:");DispStack(s);  
  31.     printf("(7)出栈序列:");  
  32.     while (!StackEmpty(s))  
  33.     {  
  34.         Pop(s,e);  
  35.         printf("%c ",e);  
  36.     }  
  37.     printf("\n");  
  38.     printf("(8)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  39.     printf("(9)释放栈\n");  
  40.     DestroyStack(s);  
  41.     return 0;  
  42. }  

sqstack.h:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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  
sqstack.cpp:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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. }  

  53. 学习心得:顺序栈是栈里面比较简单的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值