第五周 项目1:建立顺序栈算法库

来源:互联网 发布:淘宝商品色差严重 编辑:程序博客网 时间:2024/05/17 02:37
/* Copyright (c)2016,烟台大学计算机与控制工程学院 All rights reserved. 文件名称:jiang.cbp 作    者:姜孝龙 完成日期:2016年9月29日 版 本 号:v1.0   问题描述:定义顺序栈存储结构,实现其基本运算,并完成测试。 输入描述:无 程序输出:测试数据 */  

#ifndef SQSTACK_H_INCLUDED  #define SQSTACK_H_INCLUDED      #define MaxSize 100  #include <stdio.h>  #include <malloc.h>  typedef char ElemType;  typedef struct  {      ElemType data[MaxSize];      int top;                //栈指针  } SqStack;                  //顺序栈类型定义      void InitStack(SqStack *&s);    //初始化栈  void DestroyStack(SqStack *&s);  //销毁栈  bool StackEmpty(SqStack *s);     //栈是否为空  int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  bool Push(SqStack *&s,ElemType e); //入栈  bool Pop(SqStack *&s,ElemType &e); //出栈  bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  void DispStack(SqStack *s);  //输出栈      #endif // SQSTACK_H_INCLUDED  

sqstack.cpp

  1. #include "sqstack.h"  
  2.   
  3.   
  4. //初始化栈  
  5. void InitStack(SqStack *&s)  
  6. {  
  7.     s=(SqStack *)malloc(sizeof(SqStack));  
  8.     s->top=-1;               //栈顶指针置为-1  
  9. }  
  10. //销毁栈  
  11. void DestroyStack(SqStack *&s)  
  12. {  
  13.     free(s);                 //释放栈空间  
  14. }  
  15. //返回栈中元素个数——栈长度  
  16. int StackLength(SqStack *s)  
  17. {  
  18.     return(s->top+1);  
  19. }  
  20. //栈是否为空  
  21. bool StackEmpty(SqStack *s)  
  22. {  
  23.     return(s->top==-1);  
  24. }  
  25. //进栈  
  26. bool Push(SqStack *&s,ElemType e)  
  27. {  
  28.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  29.         return false;  
  30.     s->top++;                 //栈顶指针加1  
  31.     s->data[s->top]=e;        //元素e放在栈顶指针处  
  32.     return true;  
  33. }  
  34. //出栈  
  35. bool Pop(SqStack *&s,ElemType &e)  
  36. {  
  37.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  38.         return false;  
  39.     e=s->data[s->top];  //取栈顶元素  
  40.     s->top--;           //栈顶指针减1  
  41.     return true;  
  42. }  
  43. //取栈顶元素  
  44. bool GetTop(SqStack *s,ElemType &e)  
  45. {  
  46.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  47.         return false;  
  48.     e=s->data[s->top];      //取栈顶元素  
  49.     return true;  
  50. }  
  51. //输出栈  
  52. void DispStack(SqStack *s)  
  53. {  
  54.     int i;  
  55.     for (i=s->top;i>=0;i--)  
  56.         printf("%c ",s->data[i]);  
  57.     printf("\n");  
  58. } 
main.cpp

  1. #include "sqstack.h"  
  2.   
  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. }  



0 0
原创粉丝点击