栈

Stack.h

#ifndef _STACK_H
#define _STACK_H

#include<stdbool.h>

#define MALLOC(n,type)  \
        ((type*)malloc((n)*sizeof(type)))
#define INCREASE_SIZE 10
#define STACK_SIZE 20

typedef  char StackElement;//StackElement类型根据用户需求确定

typedef enum { OK = 1,
               ERROR = 0 } Status;

typedef struct{
  StackElement *pBase;
  StackElement *pTop;
  unsigned stackSize;//栈容量
} Stack;

Status BuildAStack(Stack *pStack);//构建一个栈
Status DestructAStack(Stack *pStack);//销毁一个栈
bool IsEmpty(Stack *pStack);//判断栈是否为空
bool IsFull(Stack *pStack);//判断栈是否为满
unsigned LenOfStack(Stack *pStack);//栈已储存数据的长度
Status CleanAStack(Stack *pStack);//清空一个栈
Status IncreaseASTack(Stack *pStack);//增加栈容量
Status PushIntoStack(Stack *pStack, StackElement stackElement);//数据入栈
Status PopFromStack(Stack *pStack,StackElement *popElement);//数据出栈
Status GetTopOfStack(Stack *pStack,StackElement *topElement);//获得栈顶数据但数据不出栈

#endif

Stack.c

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<stdbool.h>
#include "Stack.h"

Status BuildAStack(Stack *pStack){
  pStack->pBase = MALLOC(STACK_SIZE, StackElement);
  if(pStack->pBase==NULL){
    return ERROR;
  }
  pStack->pTop=pStack->pBase;//一开始忽略了这一步
  pStack->stackSize = STACK_SIZE;
  return OK;
}

Status DestructAStack(Stack *pStack){
  free(pStack->pBase);
  free(pStack);
  pStack = NULL;
  return OK;
}

bool IsEmpty(Stack *pStack){
  int len = LenOfStack(pStack);
  if(len==0){
    return true;
  }
  return false;
}

bool IsFull(Stack *pStack){
  int len = LenOfStack(pStack);
  if(len==pStack->stackSize){
    return true;
  }
  return false;
}

unsigned LenOfStack(Stack *pStack){
  return pStack->pTop - pStack->pBase;
}

Status CleanAStack(Stack *pStack){
  pStack->pTop = pStack->pBase;
  return OK;
}

Status IncreaseASTack(Stack *pStack){
  pStack->pBase = (StackElement *)realloc(pStack->pBase, INCREASE_SIZE * sizeof(StackElement));
  if(pStack->pBase==NULL){
    return ERROR;
  }
  pStack->pTop = pStack->pBase + pStack->stackSize;
  pStack->stackSize += INCREASE_SIZE;
  return OK;
}

Status PushIntoStack(Stack *pStack, StackElement stackElement){
  if(IsFull(pStack)){
    IncreaseASTack(pStack);
  }
  *(pStack->pTop) = stackElement;
  pStack->pTop++;
  return OK;
}

Status PopFromStack(Stack *pStack,StackElement *popElement){
  if(IsEmpty(pStack)){
    return ERROR;
  }
  pStack->pTop--;
  *popElement = *(pStack->pTop);
  return OK;
}

Status GetTopOfStack(Stack *pStack,StackElement *topElement){
  if(IsEmpty(pStack)){
    return ERROR;
  }
  *topElement = *(pStack->pTop-1);
  return OK;
}

main.c(此处为字符串反转的简单例子)

#include<stdio.h>
#include "Stack.h"

int main(int argc,char *argv[]){
  Stack b;
  char ch;
  BuildAStack(&b);
  printf("Please enter a string: ");
  while(ch=getchar(),ch!='\n'&&ch!=EOF){
    PushIntoStack(&b, ch);
  }
  while (!IsEmpty(&b)){
    PopFromStack(&b, &ch);
    printf("%c", ch);
  }
  DestructAStack(&b);
  return 0;
}

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容