C语言中的通用数据结构 [英] generic data structure in C

查看:67
本文介绍了C语言中的通用数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在C中创建通用数据结构并根据存储的数据类型使用函数,该结构具有各种数据类型,例如可以根据存储的数据进行打印.

Is there any way to create generic data structure in C and use functions in accordance with the stored data type, a structure that has various types of data and for example can be printed according to the stored data.

例如,

假设我想创建一个二进制搜索树,该树只存储有float,int.自然的做法是用int和float来创建一个枚举.看起来像这样:

Suppose I wish to make a binary search tree that has just float's, int's stored. The natural approach to do would be to create an enumeration with int's and float's. it would look something like this:

Typedef enum {INT, FLOAT} DataType;

Typedef struct node
{
    void *data;
    DataType t;
    struct node *left,
                *right;
}Node;

如果我想将其打印出来:

if i want print it out:

void printTree(Node *n)
{
    if (n != NULL)
    {
        if (n->t == INT)
        {
            int *a = (int *) n->data;
            printf("%d ", *a);
        }
        else
        {
            float *a = (float *) n->data;
            printf("%f ", *a);
        }

        printTree(n->left);
        printTree(n->right);
    }
}

没关系,但我想将另一种数据类型存储为堆栈,查询或其他内容.这就是为什么我创建了一个不依赖于特定数据类型的树的原因,例如:

That's ok but i want to store another data type as a stack, query or something else. So that's why I created a tree that does not depends on a specific data type, such as:

Typedef struct node
{
    void *data;
    struct node *left,
                *right;
}Node;

如果我想打印出来,可以使用回调函数,例如:

If i want to print it out i use callback functions, such as:

Node *printTree(Node *n, void (*print)(const void *))
{
    if (n != NULL)
    {
        print(n->data);
        printTree(a->left);
        printTree(a->right);
    }
}

但是当我尝试插入一个整数和一个浮点数并将其打印出来时,它就会掉下来.我的问题是,对于混合数据类型,有没有一种方法可以创建一种常规数据结构,该程序在一种情况下依赖于特定数据类型,而在另一种情况下则不依赖于特定数据类型?在这种情况下,我应该创建一个存储int和float的结构,并使用类似第一个打印代码中的打印功能作为回调函数中的功能?

But it falls down when i try to insert a integer and a float and print it out. My question is, Is there a way of creating a generic data structure that a routine depends on a specific data type in one situation but another situation it doesn't , for mixed data type? In this situation i should create a structure that stores int's and float's stores it and use a print function like in the first print code for that in the callback function?

观察:我只是在结构中声明了一个节点,并对其进行了所有尝试以简化操作,但其想法是将该结构与.h和.c以及所有涉及数据结构的所有抽象一起使用.

推荐答案

C不支持这种通用数据类型/结构.您可以选择几种选择:

C doesn't support this kind of generic data types/structures. You have a few options you can go with:

  • 如果您有机会使用 Clang 作为编译器,则在C中重载函数.但您必须将参数强制转换为特定类型,因此编译器知道要调用哪个函数.

  • If you have the opportunity to use Clang as the compiler, there's a language extension to overload functions in C. But you have to cast the argument to the specific type, so the compiler knows which function to call.

使用C ++

  • 尽管您仍然必须强制转换参数,所以编译器知道他必须调用哪些可用的称为 print 的函数.

使用模板

创建一个名为 print 的函数,该函数需要类似

Create a function called print which takes something like

struct data_info {
  void *data;
  enum_describing_type type;
}

print 进行切换并调用相应的 printInt printFloat 等.

print does a switch and calls the appropriate printInt, printFloat etc.

这篇关于C语言中的通用数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆