第五周项目一

  1. 烟台大学计算机学院  

  2. 作者:王雪行 
  3.   
  4. 问题描述:定义顺序栈存储结构,实现其基本运算 
  5.  
  6. 输入描述:无 
  7.  
  8. 输出描述:顺序栈的操作以及栈的操作后的元素输出,以及出栈的元素输出 
  9.   
  10. */   
  11.   
  12.   
  13.   
  14.   
  15. main:  
  16.   
  17.   
  18. #include <stdio.h>  
  19. #include "stlist.h"  
  20. int main()  
  21. {  
  22.     ElemType e;  
  23.     SqStack *s;  
  24.     printf("(1)初始化栈s\n");  
  25.     InitStack(s);  
  26.     printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  27.     printf("(3)依次进栈元素a,b,c,d,e\n");  
  28.     Push(s,'a');  
  29.     Push(s,'b');  
  30.     Push(s,'c');  
  31.     Push(s,'d');  
  32.     Push(s,'e');  
  33.     printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  34.     printf("(5)栈长度:%d\n",StackLength(s));  
  35.     printf("(6)从栈顶到栈底元素:");DispStack(s);  
  36.     printf("(7)出栈序列:");  
  37.     while (!StackEmpty(s))  
  38.     {  
  39.         Pop(s,e);  
  40.         printf("%c ",e);  
  41.     }  
  42.     printf("\n");  
  43.     printf("(8)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  44.     printf("(9)释放栈\n");  
  45.     DestroyStack(s);  
  46.     return 0;  
  47. }  
  48.   
  49.   
  50.   
  51. stlist.cpp:  
  52.   
  53. #include <stdio.h>  
  54. #include <malloc.h>  
  55. #include "stlist.h"  
  56.   
  57. void InitStack(SqStack *&s)  
  58. {  
  59.     s=(SqStack *)malloc(sizeof(SqStack));  
  60.     s->top=-1;  
  61. }  
  62. void DestroyStack(SqStack *&s)  
  63. {  
  64.     free(s);  
  65. }  
  66. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  67. {  
  68.     return(s->top+1);  
  69. }  
  70. bool StackEmpty(SqStack *s)  
  71. {  
  72.     return(s->top==-1);  
  73. }  
  74. bool Push(SqStack *&s,ElemType e)  
  75. {  
  76.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  77.         return false;  
  78.     s->top++;  
  79.     s->data[s->top]=e;  
  80.     return true;  
  81. }  
  82. bool Pop(SqStack *&s,ElemType &e)  
  83. {  
  84.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  85.         return false;  
  86.     e=s->data[s->top];  
  87.     s->top--;  
  88.     return true;  
  89. }  
  90. bool GetTop(SqStack *s,ElemType &e)  
  91. {  
  92.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  93.         return false;  
  94.     e=s->data[s->top];  
  95.     return true;  
  96. }  
  97.   
  98. void DispStack(SqStack *s)  //输出栈  
  99. {  
  100.     int i;  
  101.     for (i=s->top;i>=0;i--)  
  102.         printf("%c ",s->data[i]);  
  103.     printf("\n");  
  104. }  
  105.   
  106.   
  107.   
  108.   
  109. stlist.h:  
  110.   
  111. #define MaxSize 100  
  112. typedef char ElemType;  
  113. typedef struct  
  114. {  
  115.     ElemType data[MaxSize];  
  116.     int top;                //栈指针  
  117. } SqStack;                  //顺序栈类型定义  
  118.   
  119. void InitStack(SqStack *&s);    //初始化栈  
  120. void DestroyStack(SqStack *&s);  //销毁栈  
  121. bool StackEmpty(SqStack *s);     //栈是否为空  
  122. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  123. bool Push(SqStack *&s,ElemType e); //入栈  
  124. bool Pop(SqStack *&s,ElemType &e); //出栈  
  125. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  126. void DispStack(SqStack *s);  //输出栈  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值