第五周项目一

来源:互联网 发布:python 向量化编程 编辑:程序博客网 时间:2024/06/05 18:04

[#include "hhh.h"  
  1.   
  2. int main()  
  3. {  
  4.     ElemType e;  
  5.     SqStack *s;  
  6.     printf("(1)初始化栈s\n");  
  7.     InitStack(s);  
  8.     printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  9.     printf("(3)依次进栈元素a,b,c,d,e\n");  
  10.     Push(s,'a');  
  11.     Push(s,'b');  
  12.     Push(s,'c');  
  13.     Push(s,'d');  
  14.     Push(s,'e');  
  15.     printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  16.     printf("(5)栈长度:%d\n",StackLength(s));  
  17.     printf("(6)从栈顶到栈底元素:");DispStack(s);  
  18.     printf("(7)出栈序列:");  
  19.     while (!StackEmpty(s))  
  20.     {  
  21.         Pop(s,e);  
  22.         printf("%c ",e);  
  23.     }  
  24.     printf("\n");  
  25.     printf("(8)栈为%s\n",(StackEmpty(s)?"空":"非空"));  
  26.     printf("(9)释放栈\n");  
  27.     DestroyStack(s);  
  28.     return 0;  
  29. }  
2.自定义函数

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

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