栈的基本操作

[cpp] view plaincopyprint?
#include "stdafx.h"   
#include<cstdio>   
#include<cstdlib>   
#define OK 1   
#define TRUE 1   
#define ERROR 0   
#define FALSE 0   
#define overflow -2   
#define STACK_INTT_SIZE 100   
#define STACK_INIT_INCREMENT 20   
#define Status int   
#define ElemType int   
typedef struct   
{  
    ElemType *base,*top;  
    int stackSize;  
}SqStack;  
/*   栈的操作  
   Status InitStatck(SqStack &s)   初始化栈  
   Status DestoryStatck(SqStack &s)  销毁栈  
   Status ClearStack(SqStack &s)    清除栈  
   bool StackEmpty(SqStack s)         栈是否为空  
   int StackLength(SqStack s)        栈的长度  
   Status GetTop(SqStack s,SElemType &e)  得到栈顶  
   Status Push(SqStack &s,SElemType e)   压栈  
   Status Pop(SqStack &s,SElemType &e)   出栈 
   void DisplayStack(SqStack s);    显示栈内的元素 
   */  
Status InitStatck(SqStack &s)  
{  
    s.base=(ElemType*)malloc(STACK_INTT_SIZE*(sizeof(ElemType)));  
    if(!s.base)  
        return ERROR;  
    else  
        s.top=s.base;  
    s.stackSize=STACK_INTT_SIZE;  
}  
Status DestoryStatck(SqStack &s)  
{  
    s.top=s.base;  
    free(s.base);  
    s.base=NULL;  
    s.top=NULL;  
     return OK;    
}  
bool StackEmpty(SqStack s)  
{  
    if(s.base==s.top)     
      return TRUE;     
     else      
      return FALSE;      
}  
int StackLength(SqStack s)    
{  
    if(s.base=s.top)      
      return ERROR;     
     else      
      return (s.top-s.base);    
}  
Status GetTop(SqStack s,ElemType &e)  
{  
    if(StackEmpty(s))  
    {  
        printf("This stack is empty.");  
        return ERROR;  
    }  
    else  
    {  
        s.top--;  
        e=*s.top;  
        return OK;  
    }  
}  
Status Push(SqStack &s,ElemType e)  
{  
    if(StackLength(s)==STACK_INTT_SIZE)  
    {  
        ElemType*temp=(ElemType*)realloc(s.base,(STACK_INTT_SIZE+STACK_INIT_INCREMENT)*(sizeof(ElemType)));  
        if(!temp)  
            return ERROR;  
        s.base=temp;  
        s.top=s.base+STACK_INTT_SIZE;  
        s.stackSize=STACK_INTT_SIZE+STACK_INIT_INCREMENT;  
        *(s.top++)=e;  
        return OK;  
    }  
    else  
    {  
        *s.top=e;  
        s.top++;  
        return OK;  
    }  
}  
Status Pop(SqStack &s,ElemType &e)  
{  
    if(StackEmpty(s))  
    {  
        printf("This stack is empty.");  
        return ERROR;  
    }  
    else  
    {  
        e=*(--s.top);  
        return OK;  
    }  
}  
Status ClearStack(SqStack &s)  
{  
    s.top=s.base;  
    s.stackSize=0;  
     return OK;    
}  
void DisplayStack(SqStack s)  
{  
    if(StackEmpty(s))  
        exit(-1);  
    while(s.top!=s.base)  
        printf("%d\n",*(--s.top));  
}  
int _tmain(int argc, _TCHAR* argv[])  
{  
    SqStack statck;    
    InitStatck(statck);    
    for(int i=0;i<5;i++)    
     {    
          if(Push(statck,i))    
              printf("%d is push in this statck success!\n",i);    
          else     
             printf("/n/thappen a error\n\t");    
     }    
    DisplayStack(statck);//显示栈内的元素   
    int tem;    
    printf("now  i will print this top of statck !\n");    
    GetTop(statck,tem);    
    printf("%d is top of this statck\n",tem);    
  
    DestoryStatck(statck);  
  
    system("pause");  
    return 0;  
}  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值