栈的基本操作(附带一个数据结构、C语言版)

来源:互联网 发布:cctv发现之旅骗局知乎 编辑:程序博客网 时间:2024/06/06 04:34
/******************************************************Description  :This File Tell U How To Use StackHistory      :Date         :2017/7/7Author       :wangzaiModification :******************************************************/#include <stdio.h>#include <malloc.h>#include <string.h>#define STACK_INIT_SIZE  10#define STACKINCREMENT   10#define STRLEN           20#define OK               0#define ERROR            -1typedef struct selemtype{  int  value;  char cStr[STRLEN];}SElemType;typedef struct sqstack{  SElemType * base;  SElemType * top;  int   stacksize;}SqStack;/******************************************************Description  :Init a StackInput        :SqStack *SOutPut       :NoneReturn Value :OK/ERROR(0/-1)Calls        :Call By      :******************************************************/int InitStack(SqStack *S){  S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));  if(!S->base)  {    printf("Malloc Failed\n");    return ERROR;  }  S->top = S->base;  S->stacksize = STACK_INIT_SIZE;  return OK;}/******************************************************Description  :Get Stack Top ElemTypeInput        :SqStack *S              SElemType **eOutPut       :NoneReturn Value :OK/ERROR(0/-1)Calls        :Call By      :******************************************************/int GetTop(SqStack * S,SElemType **e){  if(S->base == S->top)  {    printf("\n Empty Stack\n");    return ERROR;  }  *e = S->top-1;  return OK;}/******************************************************Description  :Push a Element In The StackInput        :SqStack *S              SElemType eOutPut       :NoneReturn Value :OK/ERROR(0/-1)Calls        :Call By      :******************************************************/int Push(SqStack *S,SElemType e){  if(S->top - S->base >= S->stacksize)  {    S->base = (SElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT)*sizeof(SElemType));    if(!S->base)    {      printf("\n Realloc Fail\n");      return ERROR;    }    S->top = S->base + S->stacksize;    S->stacksize += STACKINCREMENT;  }  *S->top++ = e;  return OK;}/******************************************************Description  :check Stack is EmptyInput        :SqStack *SOutPut       :NoneReturn Value :OK/ERROR(0/-1)Calls        :Call By      :******************************************************/int IsEmpty(SqStack *S){  if(S->top == S->base)  {    return 1;  }  else  {    return 0;  }}/******************************************************Description  :Pop a Element From The StackInput        :SqStack *S              SElemType **eOutPut       :NoneReturn Value :OK/ERROR(0/-1)Calls        :Call By      :******************************************************/int Pop(SqStack *S,SElemType **e){  if(S->top == S->base)  {    printf("\n Empty Stack \n");    return ERROR;  }  *e = --S->top;    return OK;}/******************************************************Description  :Clear The StackInput        :SqStack *SOutPut       :NoneReturn Value :Calls        :Call By      :******************************************************/void ClearStack(SqStack *S){  S->top = S->base;  return;}/******************************************************Description  :Destroy The StackInput        :SqStack *SOutPut       :NoneReturn Value :Calls        :Call By      :******************************************************/void DestroyStack(SqStack *S){  printf("Destroy The Stack \n");  free(S->base);  S->base = NULL;  S->top  = NULL;  S->stacksize = 0;  return;}int main(){  SqStack ST   = {0};  SqStack *pST = &ST;    SElemType stElem = {0};  SElemType *getTopElem = (SElemType *)malloc(sizeof(SElemType));  char strDemo[] = "Hello  BAT";  int  Ret       = 0;  int  index     = 0;  char Store[] = "[(){}]";  Ret = InitStack(pST);    for(index=0;index<STACK_INIT_SIZE;index++)  {    stElem.value = index;    strcpy(stElem.cStr,strDemo);    Ret = Push(pST,stElem);  }  while(!Pop(pST,&getTopElem))  {    printf("Stack pop %s and %d \n",getTopElem->cStr,getTopElem->value);  }  DestroyStack(pST);}



阅读全文
0 0