美文网首页
支持多类型的泛型栈

支持多类型的泛型栈

作者: crabor | 来源:发表于2018-04-03 21:57 被阅读0次

    Stack.h

    #ifndef _STACK_H
    #define _STACK_H
    
    #include <stdbool.h>
    
    #define INCREASE_SIZE 10
    #define STACK_SIZE 20
    
    typedef enum { OK = 1, ERROR = 0 } Status;
    
    typedef struct STACK {
      unsigned int typeSize;//sizeof(栈元素类型)
      char *pBase;//栈底指针
      char *pTop;//栈顶指针
      int stackSize;//栈容量
    } Stack;
    
    Status InitStack(Stack *pStack, unsigned int typeSize);
    Status DestructAStack(Stack *pStack);
    bool IsEmpty(Stack *pStack);
    bool IsFull(Stack *pStack);
    unsigned int LenOfStack(Stack *pStack);
    Status CleanAStack(Stack *pStack);
    Status IncreaseASTack(Stack *pStack);
    Status PushIntoStack(Stack *pStack, const void *value);
    Status PopFromStack(Stack *pStack, void *popElement);
    Status GetTopOfStack(Stack *pStack, void *topElement);
    #endif
    

    Stack.c

    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #include<stdbool.h>
    #include "Stack.h"
    
    
    
    Status InitStack(Stack *pStack, unsigned int typeSize) {
      pStack->typeSize = typeSize;
      pStack->pBase = (char *)malloc(sizeof(char) *pStack->typeSize * STACK_SIZE);
      if (pStack->pBase == NULL) {
        return ERROR;
      }
      pStack->pTop = pStack->pBase;
      pStack->stackSize = STACK_SIZE;
      return OK;
    }
    
    Status DestructAStack(Stack *pStack) {
      free(pStack->pBase);
      pStack->pBase = NULL;
      pStack->pTop = NULL;
      pStack->stackSize = 0;
      pStack->typeSize = 0;
      return OK;
    }
    
    bool IsEmpty(Stack *pStack) {
      return (pStack->pTop - pStack->pBase) / pStack->typeSize == 0;
    }
    
    bool IsFull(Stack *pStack) {
      return (pStack->pTop - pStack->pBase) / pStack->typeSize == pStack->stackSize;
    }
    
    unsigned int LenOfStack(Stack *pStack) {
      return (pStack->pTop - pStack->pBase) / pStack->typeSize;
    }
    
    Status CleanAStack(Stack *pStack) {
      pStack->pTop = pStack->pBase;
      return OK;
    }
    
    Status IncreaseASTack(Stack *pStack) {
      pStack->pBase = (char *)realloc(
          pStack->pBase,
          sizeof(char) * pStack->typeSize * INCREASE_SIZE);
      if (pStack->pBase == NULL) {
        return ERROR;
      }
      pStack->pTop = pStack->pBase + pStack->stackSize * pStack->typeSize;
      pStack->stackSize += INCREASE_SIZE;
      return OK;
    }
    
    Status PushIntoStack(Stack *pStack, const void *value) {
      int i;
      char *e = (char *)value;
      if (IsFull(pStack)) {
        IncreaseASTack(pStack);
      }
      for (i = 0; i < pStack->typeSize; i++) {
        pStack->pTop[i] = e[i];
      }
      pStack->pTop += pStack->typeSize;
      return OK;
    }
    
    Status PopFromStack(Stack *pStack, void *popElement) {
      int i;
      char *e = (char *)popElement;
      if (IsEmpty(pStack)) {
        return ERROR;
      }
      pStack->pTop-=pStack->typeSize;
      for (i = 0; i < pStack->typeSize;i++){
        e[i] = pStack->pTop[i];
      }
      return OK;
    }
    
    Status GetTopOfStack(Stack *pStack, void *topElement){
      int i;
      char *e = (char *)topElement;
      if (IsEmpty(pStack)) {
        return ERROR;
      }
      pStack->pTop-=pStack->typeSize;
      for (i = 0; i < pStack->typeSize;i++){
        e[i] = pStack->pTop[i];
      }
      pStack->pTop += pStack->typeSize;
      return OK;
    }
    

    main.c

    #include <stdio.h>
    #include "Stack.h"
    
    int main(int argc,char *argv[]){
      Stack a,b;
      char ch;
      int integer;
      InitStack(&a,sizeof(int));
      InitStack(&b,sizeof(char));
    
      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);
    
      printf("\n");
    
      printf("Please enter some integer divided by space: ");
      while(scanf("%d",&integer)!=EOF){//跳出循环按Enter->Ctrl+Z->Enter
        PushIntoStack(&a, &integer);
      }
      while (!IsEmpty(&a)){
        PopFromStack(&a, &integer);
        printf("%d ", integer);
      }
      DestructAStack(&a);
    
      return 0;
    }
    

    相关文章

      网友评论

          本文标题:支持多类型的泛型栈

          本文链接:https://www.haomeiwen.com/subject/xwachftx.html