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

本文介绍了顺序栈的C语言实现,包括初始化、销毁、判断空栈、获取栈长、入栈、出栈、取栈顶元素和显示栈内元素等功能,并通过一个主程序展示了其实例操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建立顺序栈算法库
本次实践将建立顺序栈的算法库,用以解决更多的工程问题,提供便利。(编译环境: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. }  


测试函数运行结果如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值