第五周项目一

来源:互联网 发布:苹果手机估价淘宝 编辑:程序博客网 时间:2024/05/16 19:10
  1. /*  
  2. 烟台大学计算机学院  
  3.   
  4. 文件名称:ycddd.cpp  
  5.   
  6. 作者:刘照京 
  7.   
  8. 完成日期:2017年10月18日 
  9.   
  10. 问题描述:定义顺序栈存储结构,实现其基本运算 
  11.  
  12. 输入描述:无 
  13.  
  14. 输出描述:顺序栈的操作以及栈的操作后的元素输出,以及出栈的元素输出 
  15.   
  16. */   
  17.   
  18.   
  19.   
  20.   
  21. main:  
  22.   
  23.   
  24. #include <stdio.h>  
  25. #include "stlist.h"  
  26. int main()  
  27. {  
  28.     ElemType e;  
  29.     SqStack *s;  
  30.     printf("(1)初始化栈s\n");  
  31.     InitStack(s);  
  32.     printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  33.     printf("(3)依次进栈元素a,b,c,d,e\n");  
  34.     Push(s,'a');  
  35.     Push(s,'b');  
  36.     Push(s,'c');  
  37.     Push(s,'d');  
  38.     Push(s,'e');  
  39.     printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  40.     printf("(5)栈长度:%d\n",StackLength(s));  
  41.     printf("(6)从栈顶到栈底元素:");DispStack(s);  
  42.     printf("(7)出栈序列:");  
  43.     while (!StackEmpty(s))  
  44.     {  
  45.         Pop(s,e);  
  46.         printf("%c ",e);  
  47.     }  
  48.     printf("\n");  
  49.     printf("(8)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  50.     printf("(9)释放栈\n");  
  51.     DestroyStack(s);  
  52.     return 0;  
  53. }  
  54.   
  55.   
  56.   
  57. stlist.cpp:  
  58.   
  59. #include <stdio.h>  
  60. #include <malloc.h>  
  61. #include "stlist.h"  
  62.   
  63. void InitStack(SqStack *&s)  
  64. {  
  65.     s=(SqStack *)malloc(sizeof(SqStack));  
  66.     s->top=-1;  
  67. }  
  68. void DestroyStack(SqStack *&s)  
  69. {  
  70.     free(s);  
  71. }  
  72. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  73. {  
  74.     return(s->top+1);  
  75. }  
  76. bool StackEmpty(SqStack *s)  
  77. {  
  78.     return(s->top==-1);  
  79. }  
  80. bool Push(SqStack *&s,ElemType e)  
  81. {  
  82.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  83.         return false;  
  84.     s->top++;  
  85.     s->data[s->top]=e;  
  86.     return true;  
  87. }  
  88. bool Pop(SqStack *&s,ElemType &e)  
  89. {  
  90.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  91.         return false;  
  92.     e=s->data[s->top];  
  93.     s->top--;  
  94.     return true;  
  95. }  
  96. bool GetTop(SqStack *s,ElemType &e)  
  97. {  
  98.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  99.         return false;  
  100.     e=s->data[s->top];  
  101.     return true;  
  102. }  
  103.   
  104. void DispStack(SqStack *s)  //输出栈  
  105. {  
  106.     int i;  
  107.     for (i=s->top;i>=0;i--)  
  108.         printf("%c ",s->data[i]);  
  109.     printf("\n");  
  110. }  
  111.   
  112.   
  113.   
  114.   
  115. stlist.h:  
  116.   
  117. #define MaxSize 100  
  118. typedef char ElemType;  
  119. typedef struct  
  120. {  
  121.     ElemType data[MaxSize];  
  122.     int top;                //栈指针  
  123. } SqStack;                  //顺序栈类型定义  
  124.   
  125. void InitStack(SqStack *&s);    //初始化栈  
  126. void DestroyStack(SqStack *&s);  //销毁栈  
  127. bool StackEmpty(SqStack *s);     //栈是否为空  
  128. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  129. bool Push(SqStack *&s,ElemType e); //入栈  
  130. bool Pop(SqStack *&s,ElemType &e); //出栈  
  131. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  132. void DispStack(SqStack *s);  //输出栈  



运行结果:



知识点总结:需考虑栈上溢出和栈下溢出。

学习心得:学会了顺序栈的存储结构以及基本运算。

原创粉丝点击