数据结构基础5_顺序栈的实现

来源:互联网 发布:pkpm加密狗 淘宝 编辑:程序博客网 时间:2024/06/05 00:34
typedef int ElemType;
#define true 1
#define false 0
typedef int Status;
#include<stdlib.h>
#include<stdio.h>
#define stackinitsize 100
#define stackincrease 10
typedef struct{
        ElemType *base;
        ElemType *top;
        int stacksize;
        }sqstack;
        
        
Status Initstack(sqstack &S)
{
       S.base=(ElemType*)malloc(stackinitsize*sizeof(ElemType));
       if(!S.base)  return false;
       S.top=S.base;
       S.stacksize=stackinitsize;
       return true;
       }
 Status Destorystack(sqstack &S)
 {
        free(S.base);
        S.base=NULL;
        }
 Status Clearstack(sqstack &S)
 {
        S.top=S.base;
        return 0;
        }
  Status Stackempty(sqstack S)
  {
         if(S.top==S.base)
         {
                          return true;
                          }
                          else
                          return false;
                          }
    int Stacklength(sqstack S)
    {
      return S.top-S.base;
      }
 Status Gettop(sqstack S,ElemType &e)
 {
    if(S.top==S.base)
    {
                     return false;
                     }
     e=*(S.top-1);
     return true;
     }
 Status push(sqstack &S,ElemType e)
 {
   if(S.top-S.base>=S.stacksize)
   {
      S.base=(ElemType*)realloc(S.base,(S.stacksize+stackincrease)*sizeof(ElemType));
      if(!S.base)
      {
          return false;
          }
    S.top=S.base+S.stacksize;
    S.stacksize+=stackincrease;
}
*S.top++=e;
return true;
}                                            
  Status pop(sqstack &S,ElemType &e)
  {
       if(S.top==S.base)
       {
                  return false;
                  }
          e=*--S.top;
          return true;
          }
 Status stacktraverse(sqstack S)
 {
        ElemType *p;
        p=S.base;
        while(p!=S.top)
        {
           printf("%d",*p);
           p++;
           }
           return true;
           }
                                                
                          
                              
         
#include<stdio.h>#include<sqstack1.h>#include<stdlib.h>int main(){    sqstack s1;    Initstack(s1);    push(s1,1);        push(s1,2);    push(s1,3);    push(s1,4);    push(s1,5);    int m;    pop(s1,m);    stacktraverse(s1);    system("pause");}    

         
         
         
         
         
         
         
         
         
         
         
         
         
                            
0 0
原创粉丝点击