顺序栈的C语言实现

该程序定义了顺序栈(栈的顺序存储结构)的存储结构,并实现了顺序栈的基本操作,例如:初始化、销毁、查找、插入、删除、遍历等。

//SqStack.h

该头文件定义了顺序栈的存储结构,对顺序栈的基本操作的函数原型进行了声明。

Code:
  1. #include "windows.h"   
  2.   
  3. //定义函数结果状态代码   
  4. #define TRUE        1   
  5. #define FALSE       0   
  6. #define OK          1   
  7. #define ERROR       0   
  8. #define OVERFLOW    -1   
  9. #define UNDERFLOW   -2   
  10.   
  11. //定义函数返回值变量的类型   
  12. typedef INT32   Status;   
  13. //定义顺序栈的数据元素的类型   
  14. typedef INT32   ElemType;   
  15.   
  16. #pragma once   
  17. //定义顺序栈的存储结构   
  18. #define INCREMENTSIZE   10 //存储空间分配增量   
  19. typedef struct{   
  20.     ElemType    *bottom;    //栈底指针   
  21.     ElemType    *top;       //栈顶指针   
  22.     LONG32      size;       //栈存储空间大小   
  23. }SqStack;   
  24.   
  25.   
  26. //声明相关的辅助函数   
  27. Status  output(ElemType);   
  28. //定义函数指针类型   
  29. typedef Status (*pOutput)(ElemType);   
  30.   
  31. //声明顺序栈的基本操作   
  32. Status  InitStack(SqStack&,LONG32);   
  33. Status  DestroyStack(SqStack&);   
  34. Status  ClearStack(SqStack&);   
  35. Status  StackEmpty(SqStack&);   
  36. LONG32  StackLength(SqStack&);   
  37. Status  GetTop(SqStack&,ElemType&);   
  38. Status  Push(SqStack&,ElemType);   
  39. Status  Pop(SqStack&,ElemType&);   
  40. Status  StackTraverse(SqStack&,pOutput);  

//SqStack.cpp

该源文件实现了顺序栈的基本操作

Code:
  1. #include "stdafx.h"   
  2. #include "windows.h"   
  3.   
  4. #include "c_lib.h"   
  5. #include "cpp_lib.h"   
  6.   
  7. #include "SqStack.h"   
  8.   
  9. Status  output(ElemType e)   
  10. {   
  11.     cout<<e<<" ";   
  12.     return OK;   
  13. }   
  14.   
  15. Status  InitStack(SqStack &S,LONG32 _size)   
  16. //操作结果:构造一个空栈S   
  17. {   
  18.     S.bottom=(ElemType*)malloc(_size*sizeof(ElemType));   
  19.     if(S.bottom == NULL){   
  20.         cout<<"严重错误:顺序栈初始存储空间分配失败,程序退出";   
  21.         exit(EXIT_FAILURE);   
  22.     }   
  23.   
  24.     S.size=_size;   
  25.     S.top=S.bottom;   
  26.     return OK;   
  27. }   
  28.   
  29. Status  DestroyStack(SqStack &S)   
  30. //初始条件:栈S已存在   
  31. //操作结果:栈S已被销毁   
  32. {   
  33.     free(S.bottom);   
  34.     S.bottom=S.top=NULL;  //S.bottom的值为NULL,表示顺序栈不存在   
  35.     S.size=0;   
  36.     return OK;   
  37. }   
  38.   
  39. Status  ClearStack(SqStack &S)   
  40. //初始条件:栈S已存在   
  41. //操作结果:将S清为空栈   
  42. {   
  43.     S.top=S.bottom;  //S.top == S.bottom作为顺序栈空的标记   
  44.     return OK;   
  45. }   
  46.   
  47. Status  StackEmpty(SqStack &S)   
  48. //初始条件:栈S已存在   
  49. //操作结果:若栈S为空栈,则返回TRUE,否则FALSE   
  50. {   
  51.     if(S.top == S.bottom)   
  52.         return TRUE;   
  53.     else  
  54.         return FALSE;   
  55. }   
  56.   
  57. LONG32  StackLength(SqStack &S)   
  58. //初始条件:栈S已存在   
  59. //操作结果:返回S的元素个数,即栈的长度   
  60. {   
  61.     return S.top-S.bottom;   
  62. }   
  63.   
  64. Status  GetTop(SqStack &S,ElemType &e)   
  65. //初始条件:栈S已存在且非空   
  66. //操作结果:用e返回S的栈顶元素   
  67. {   
  68.     if( StackEmpty(S) == TRUE ){   
  69.         cout<<"访问栈顶元素发生错误:栈为空"<<endl;   
  70.         return ERROR;   
  71.     }   
  72.   
  73.     return *(S.top-1);   
  74. }   
  75.   
  76. Status  Push(SqStack &S,ElemType e)   
  77. //初始条件:栈S已存在   
  78. //操作结果:插入元素e为新的栈顶元素   
  79. {   
  80.     LONG32  length=StackLength(S);   
  81.     if( length >= S.size ){   
  82.         ElemType *newbase=(ElemType*)realloc(S.bottom,(S.size+INCREMENTSIZE)*sizeof(ElemType));   
  83.         if(newbase == NULL){   
  84.             cout<<"严重错误:顺序栈增量存储空间分配失败,程序退出!";   
  85.             exit(EXIT_FAILURE);   
  86.         }   
  87.         S.bottom=newbase;   
  88.         S.top=S.bottom+length;   
  89.         S.size+=INCREMENTSIZE;   
  90.     }   
  91.   
  92.     *(S.top++)=e;   
  93.     return OK;   
  94. }   
  95.   
  96. Status  Pop(SqStack &S,ElemType &e)   
  97. //初始条件:栈S已存在且非空   
  98. //操作结果:删除S的栈顶元素,并且用e返回其值   
  99. {   
  100.     if(StackEmpty(S) == TRUE){   
  101.         cout<<"顺序栈为空,出栈失败!"<<endl;   
  102.         return ERROR;   
  103.     }   
  104.   
  105.     e=*(--S.top);   
  106.     return OK;   
  107. }   
  108.   
  109. Status  StackTraverse(SqStack &S,pOutput outFun)   
  110. //初始条件:栈S已存在且非空   
  111. //操作结果:从栈底到栈顶依次对S的每个数据元素调用函数output()。一旦output失败,则操作失效   
  112. {   
  113.     if(S.bottom == NULL){   
  114.         cout<<"严重错误:栈不存在,程序退出!";   
  115.         exit(EXIT_FAILURE);   
  116.     }   
  117.   
  118.     for(LONG32 i=0;i < StackLength(S);i++)   
  119.         outFun(S.bottom[i]);   
  120.     cout<<endl;   
  121.     return OK;   
  122. }  

//test.cpp

该源文件为测试程序

Code:
  1. #include "stdafx.h"   
  2. #include "windows.h"   
  3.   
  4. #include "c_lib.h"   
  5. #include "cpp_lib.h"   
  6.   
  7. #include "SqStack.h"   
  8.   
  9. INT32 RangedRand( int range_min, int range_max)   
  10. {   
  11.    // Generate random numbers in the half-closed interval   
  12.    // [range_min, range_max). In other words,   
  13.    // range_min <= random number < range_max   
  14.    INT32 u =(INT32)( (double)rand() / (RAND_MAX + 1) * (range_max - range_min)   
  15.             + range_min );    
  16.    return u;   
  17. }   
  18.   
  19.   
  20. int main()   
  21. {   
  22.     SqStack S;   
  23.     InitStack(S,100);   
  24.   
  25.     srand( (unsigned)time( NULL ) );   
  26.     ElemType data[200];   
  27.     for(LONG32 i=0;i<200;i++){   
  28.         data[i]=RangedRand(-500,500);   
  29.         Push(S,data[i]);   
  30.     }   
  31.   
  32.     StackTraverse(S,output);   
  33.     cout<<"顺序栈的长度是:"<<StackLength(S)<<endl;   
  34.   
  35.     ElemType e;   
  36.     Pop(S,e);   
  37.     cout<<"栈顶元素"<<e<<"出栈"<<endl;   
  38.     cout<<"此时的顺序栈:"<<endl;   
  39.     StackTraverse(S,output);   
  40.     cout<<"顺序栈的长度是:"<<StackLength(S)<<endl;   
  41.   
  42.     DestroyStack(S);   
  43.     StackTraverse(S,output);   
  44.     cout<<"顺序栈的长度是:"<<StackLength(S)<<endl;   
  45.   
  46.     return 0;   
  47. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值