다양한 유형의 범용 창고 지원

4038 단어

Stack.h

#ifndef _STACK_H
#define _STACK_H

#include 

#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
#include
#include
#include
#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 
#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!='
'&&ch!=EOF){ PushIntoStack(&b, &ch); } while (!IsEmpty(&b)){ PopFromStack(&b, &ch); printf("%c", ch); } DestructAStack(&b); printf("
"); 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; }

좋은 웹페이지 즐겨찾기