如何在c或c++中创建异构链接列表

发布于 2024-12-20 02:14:05 字数 181 浏览 0 评论 0原文

一个可以保存浮点数、整数、字符等数据和算法的链接列表应该很好而且不是很复杂

我想到创建一个带有 void 指针的结构,该指针将指向后续节点。但问题是我不能使用具有结构的模板。

归结为c,我必须测试用户输入的每个字符来测试它是否是整数、浮点数或字符。然后我们可以进一步进行,

请建议一个有效的算法/代码

A link list that can hold float,integer,character,etc data and algorithm should be well and not very complex

I thought of creating a structure with void pointer that will point to subsequent nodes. but problem is that i cannot use templates with structure.

coming down to c, i have to test each character entered by user to test whether it is integer , float or character or not.then we can proceed further

please suggest an efficient algorithm/code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一杯敬自由 2024-12-27 02:14:05

如果您想自己执行此操作,您基本上需要创建一个数组或元素链接列表,对数据和数据类型进行编码。您可以使用包含类型指示符和要处理的各种类型的联合的结构,并创建该结构的数组或链接列表:

typedef struct {
    int type_indicator;
    union {
        float f;
        int i;
        double d;
        void *p;
        char c;
    }
} generic_item;

generic_item generic_array[10];

我将留给您为以下内容提供适当的枚举 :类型指示器并为您的算法添加函数指针。如果您想要一个链表而不是数组,那么显然您还需要添加一个 generic_item *next 指针。

我还没有研究过其他答案链接到的增强选项,但在尝试推出自己的解决方案之前,我可能会先看看那里。

If you want to do this yourself you'll basically want to create an array or linked list of elements that encode both the data and the type of data. You could use a struct that includes a type indicator and a union of the various types that you want to handle, and the create an array or linked list of that struct:

typedef struct {
    int type_indicator;
    union {
        float f;
        int i;
        double d;
        void *p;
        char c;
    }
} generic_item;

generic_item generic_array[10];

I'll leave it to you to come up with an appropriate enumeration for the type indicator and to add a function pointer for your algorithm. If you want a linked list instead of an array, you'll obviously also need to add a generic_item *next pointer.

I haven't looked into the boost options that other answers link to, but I'd probably look there first before trying to roll my own solution.

-小熊_ 2024-12-27 02:14:05

使用 boost::variantboost::any。取决于您的需求。

Using boost::variant or boost::any. Depends on your needs.

你的他你的她 2024-12-27 02:14:05

注意:这是一个纯粹的 C 答案。

这个数据结构就是我开始的内容:

typedef struct heterogeneous_list
{
    enum { CHAR, STRING, FLOAT, INT } type;
    void *item;
    struct heterogeneous_list *next;
}

当我从用户那里获得该项目时,我会将其存储在列表中(假设当前指向末尾)列表的):

current->next = malloc(sizeof(heterogeneous_list));
case (/* whether the user entered a char, string, float, or int */
{
    case /* char */:
        current->next.item = malloc(sizeof(char));
        current->next.type = CHAR;
        current->next.next = NULL;
        break;
/* and so forth, for string, int, and float */
}
current = current->next;

当迭代列表时,现在可以很容易地根据类型处理列表中的内容。以下代码假设 current 是在迭代中查看的列表中的当前项(for 循环遍历列表):

char currentItemChar;
char * currentItemString;
float currentItemFloat;
int currentItemInt;

case (current->type)
{
    case CHAR:
        currentItemChar = *((char*) current->item);
        // process a character
        break;
    case STRING:
        currentItemString = (char*) current->item;
        // process a string
        break;
    case FLOAT: 
        currentItemFloat = *((float*) current->item);
        // process a float
        break;
    .
    .
    .
};

这就是我要做的。

NOTE: This is a purely C answer.

This data structure is what I would start out with:

typedef struct heterogeneous_list
{
    enum { CHAR, STRING, FLOAT, INT } type;
    void *item;
    struct heterogeneous_list *next;
}

When I got the item from the user, I would store it in the list (assuming current points to the end of the list):

current->next = malloc(sizeof(heterogeneous_list));
case (/* whether the user entered a char, string, float, or int */
{
    case /* char */:
        current->next.item = malloc(sizeof(char));
        current->next.type = CHAR;
        current->next.next = NULL;
        break;
/* and so forth, for string, int, and float */
}
current = current->next;

When iterating through the list, it is easy now to process what is in the list based on type. The following code assumes current is the current item in the list being looked at in an iteration (a for-loop going through the list):

char currentItemChar;
char * currentItemString;
float currentItemFloat;
int currentItemInt;

case (current->type)
{
    case CHAR:
        currentItemChar = *((char*) current->item);
        // process a character
        break;
    case STRING:
        currentItemString = (char*) current->item;
        // process a string
        break;
    case FLOAT: 
        currentItemFloat = *((float*) current->item);
        // process a float
        break;
    .
    .
    .
};

That's what I would do.

昵称有卵用 2024-12-27 02:14:05

http://www.boost.org/doc/libs/1_48_0 /doc/html/variant.html

(当然,在解释 boost 变体给你带来了什么之前,它当然也提到了 C/C++ 联合)

http://www.boost.org/doc/libs/1_48_0/doc/html/variant.html

(which of course also mentions C/C++ unions before explaining what boost variants give you over this)

你げ笑在眉眼 2024-12-27 02:14:05

可以通过使用void *作为指向数据项的指针来创建异构链表:

struct Node
{
    Node * previous;
    Node * next;
    void * p_data;
};

在实现异构容器之前,人们可能会问是否可以更改设计以使用同类容器。

A heterogeneous linked list can be created by using a void * as a pointer to a data item:

struct Node
{
    Node * previous;
    Node * next;
    void * p_data;
};

Before implementing a heterogenous container, one might ask if the design can be changed to use homegeneous containers instead.

缘字诀 2024-12-27 02:14:05

正如您所提到的,您可以使用一些与一些或多或少棘手的宏相关的空指针来执行此类操作。

您可以定义一个包含三个指针的结构(或类):next、prev(用于下一个和上一个列表元素)和某种 void* 数据。此外,您还可以存储每个列表条目的类型(可以通过enum或等效的东西来实现)。

此外,您可以定义一个宏 - 给定一个列表项 - 检索数据并自动将其转换为给定类型:

#define get_list_item(item, type) *(type*)(((item)->data))

You could do this kind of things using - as you mentioned - some void pointers in connection with some more or less tricky macros.

You could define a struct (or a class) containing three pointers: next, prev (for the next and the previous list element), and some kind of void* data. You could moreover store the type for each list entry (which could be realized by an enum or something equivalent).

Moreover, you could define a macro that - given a list item - retrieves the data and automatically cast it into a given type:

#define get_list_item(item, type) *(type*)(((item)->data))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文