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

支持多类型的泛型栈

作者: 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;
}

相关文章

  • 支持多类型的泛型栈

    Stack.h Stack.c main.c

  • swift泛型

    泛型 泛型可以将类型参数化,提高代码复用率,减少代码量 使用泛型实现栈 关联类型(Associated Type)...

  • Java自学-泛型 支持泛型的类

    Java 如何设计支持泛型的类 步骤 1 : 不支持泛型的Stack 以Stack栈为例子,如果不使用泛型当需要一...

  • 泛型(Generics)

    泛型可以将类型参数化,提高代码服用率,减少代码 类实现栈结构 结构体实现栈结构 结构体关联值使用泛型 关联类型(A...

  • Swift中的泛型

    1. 泛型函数 T 是占位类型名,用来代替实际类型名。 2. 泛型类型 下面的例子定义了一个泛型的栈(stack)...

  • TypeScript(三)

    五. 泛型 泛型定义:泛型就是解决类、接口、方法的复用性,以及对不特定数据类型的支持。 可以支持不特定的数据类型,...

  • Java基础

    1、泛型 jdk1.5增加了泛型支持很大程度是为了让集合记住其元素的类型。知识点:泛型类、泛型接口、类型通配符、泛...

  • Swift中的泛型(Generics)

    泛型(Generics) 1、泛型可以将类型参数化,提高代码复用率,减少代码量 栈 汇编分析泛型的实现原理 通过汇...

  • Kotlin 泛型

    Kotlin 支持泛型, 语法和 Java 类似。例如,泛型类: 泛型函数: 类型变异 Java 的泛型中,最难理...

  • 5_泛型

    泛型概述 泛型就是一个标签:<数据类型> 泛型可以在编译阶段结束,只能操作某种数据类型 泛型和集合都智能支持引用数...

网友评论

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

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