栈的基本操作


  1. #include "stdafx.h"  
  2. #include<cstdio>  
  3. #include<cstdlib>  
  4. #define OK 1  
  5. #define TRUE 1  
  6. #define ERROR 0  
  7. #define FALSE 0  
  8. #define overflow -2  
  9. #define STACK_INTT_SIZE 100  
  10. #define STACK_INIT_INCREMENT 20  
  11. #define Status int  
  12. #define ElemType int  
  13. typedef struct   
  14. {  
  15.     ElemType *base,*top;  
  16.     int stackSize;  
  17. }SqStack;  
  18. /*   栈的操作  
  19.    Status InitStatck(SqStack &s)   初始化栈  
  20.    Status DestoryStatck(SqStack &s)  销毁栈  
  21.    Status ClearStack(SqStack &s)    清除栈  
  22.    bool StackEmpty(SqStack s)         栈是否为空  
  23.    int StackLength(SqStack s)        栈的长度  
  24.    Status GetTop(SqStack s,SElemType &e)  得到栈顶  
  25.    Status Push(SqStack &s,SElemType e)   压栈  
  26.    Status Pop(SqStack &s,SElemType &e)   出栈 
  27.    void DisplayStack(SqStack s);    显示栈内的元素 
  28.    */  
  29. Status InitStatck(SqStack &s)  
  30. {  
  31.     s.base=(ElemType*)malloc(STACK_INTT_SIZE*(sizeof(ElemType)));  
  32.     if(!s.base)  
  33.         return ERROR;  
  34.     else  
  35.         s.top=s.base;  
  36.     s.stackSize=STACK_INTT_SIZE;  
  37. }  
  38. Status DestoryStatck(SqStack &s)  
  39. {  
  40.     s.top=s.base;  
  41.     free(s.base);  
  42.     s.base=NULL;  
  43.     s.top=NULL;  
  44.      return OK;    
  45. }  
  46. bool StackEmpty(SqStack s)  
  47. {  
  48.     if(s.base==s.top)     
  49.       return TRUE;     
  50.      else      
  51.       return FALSE;      
  52. }  
  53. int StackLength(SqStack s)    
  54. {  
  55.     if(s.base=s.top)      
  56.       return ERROR;     
  57.      else      
  58.       return (s.top-s.base);    
  59. }  
  60. Status GetTop(SqStack s,ElemType &e)  
  61. {  
  62.     if(StackEmpty(s))  
  63.     {  
  64.         printf("This stack is empty.");  
  65.         return ERROR;  
  66.     }  
  67.     else  
  68.     {  
  69.         s.top--;  
  70.         e=*s.top;  
  71.         return OK;  
  72.     }  
  73. }  
  74. Status Push(SqStack &s,ElemType e)  
  75. {  
  76.     if(StackLength(s)==STACK_INTT_SIZE)  
  77.     {  
  78.         ElemType*temp=(ElemType*)realloc(s.base,(STACK_INTT_SIZE+STACK_INIT_INCREMENT)*(sizeof(ElemType)));  
  79.         if(!temp)  
  80.             return ERROR;  
  81.         s.base=temp;  
  82.         s.top=s.base+STACK_INTT_SIZE;  
  83.         s.stackSize=STACK_INTT_SIZE+STACK_INIT_INCREMENT;  
  84.         *(s.top++)=e;  
  85.         return OK;  
  86.     }  
  87.     else  
  88.     {  
  89.         *s.top=e;  
  90.         s.top++;  
  91.         return OK;  
  92.     }  
  93. }  
  94. Status Pop(SqStack &s,ElemType &e)  
  95. {  
  96.     if(StackEmpty(s))  
  97.     {  
  98.         printf("This stack is empty.");  
  99.         return ERROR;  
  100.     }  
  101.     else  
  102.     {  
  103.         e=*(--s.top);  
  104.         return OK;  
  105.     }  
  106. }  
  107. Status ClearStack(SqStack &s)  
  108. {  
  109.     s.top=s.base;  
  110.     s.stackSize=0;  
  111.      return OK;    
  112. }  
  113. void DisplayStack(SqStack s)  
  114. {  
  115.     if(StackEmpty(s))  
  116.         exit(-1);  
  117.     while(s.top!=s.base)  
  118.         printf("%d\n",*(--s.top));  
  119. }  
  120. int _tmain(int argc, _TCHAR* argv[])  
  121. {  
  122.     SqStack statck;    
  123.     InitStatck(statck);    
  124.     for(int i=0;i<5;i++)    
  125.      {    
  126.           if(Push(statck,i))    
  127.               printf("%d is push in this statck success!\n",i);    
  128.           else     
  129.              printf("/n/thappen a error\n\t");    
  130.      }    
  131.     DisplayStack(statck);//显示栈内的元素  
  132.     int tem;    
  133.     printf("now  i will print this top of statck !\n");    
  134.     GetTop(statck,tem);    
  135.     printf("%d is top of this statck\n",tem);    
  136.   
  137.     DestoryStatck(statck);  
  138.   
  139.     system("pause");  
  140.     return 0;  
  141. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值