简体   繁体   English

打印包含void *的链表的元素

[英]Printing elements of a linked list containing void*

I'm implementing a generic doubly linked list in C, and I've written functions for forward and backward traversal. 我在C中实现了一个通用的双向链表,我编写了前向和后向遍历的函数。 Within these functions, I'd like to print the data contained in the list, but since it's generic, I'm having a hard time figuring out how this can be done. 在这些函数中,我想打印列表中包含的数据,但由于它是通用的,我很难弄清楚如何做到这一点。 Obviously I can't just printf using %d or something because the list could contain any data type. 很显然,我不能只printf使用%d或东西,因为这个名单可以包含任何数据类型。 Any suggestions to approach this differently would be appreciated because I've been thinking about it for quite some time and I'm at a loss. 任何以不同方式处理这种问题的建议都会受到赞赏,因为我已经考虑了很长一段时间而且我很茫然。 Thanks! 谢谢!

You can do a lot of things. 你可以做很多事情。

For example, you can store structs that not only contain a void * to your element's data, but also an indication for the possible data type, or even just the format string necessary to printf the object. 例如,你可以存储结构,不仅包含void *您元素的数据,也为可能的数据类型的指示,甚至只是必要的格式字符串printf对象。

You could also think of a struct that contains a void * to your data, and a function pointer that will allow you to convert your data to a string. 您还可以考虑一个包含数据void *的结构,以及一个允许您将数据转换为字符串的函数指针。 That is basically minimally emulating C++'s polymorphism in C. 这基本上是在C语言中最低限度地模仿C ++的多态性。

EDIT: as wickstopher pointed out, you just don't get compile-time type safety with this. 编辑:正如Wickstopher指出的那样,你只是没有得到编译时类型的安全性。 Mess up the function pointer, and you'll have an unsuitable function working on your data, possibly causing your program to segfault, run over your kitten, burn down your apartment, run away with your youngest child or smoke crack in your kitchen. 弄乱功能指针,你将有一个不合适的功能处理你的数据,可能导致你的程序发生段错误,碾过你的小猫,烧毁你的公寓,与你最小的孩子一起逃跑或在厨房里冒烟。

You need a tag field in the struct you declared for node. 您在为节点声明的struct需要一个标记字段。 Define an enum for data types as 将数据类型的enum定义为

enum {INT_TYPE, FLOAT_TYPE, DOUBLE_TYPE, CHAR_TYPE} type;

For every data type you need to assign type with corresponding enumeration constant. 对于每种数据类型,您需要为相应的枚举常量指定type In printing function you will have to check the value of type and then use the appropriate specifier. 在打印功能中,您必须检查type的值,然后使用适当的说明符。

C does not support any sort of runtime type checking, so this is not possible. C不支持任何类型的运行时类型检查,因此这是不可能的。 See runtime determine type for C (similar question). 请参阅运行时确定C的类型 (类似问题)。

If you want to support a limited range of data types, you might consider an adding an enum to your node structure, but this won't get you true generic functionality and won't be enforceable at compile time. 如果要支持有限范围的数据类型,可以考虑在节点结构中添加枚举,但这不会为您提供真正的通用功能,并且在编译时不会强制执行。

Assuming your node has a void * pointer to node contents, among pointers for referring the next and previous elements in the list, provide a function to print a node, such as 假设您的节点有一个指向节点内容的void *指针,在引用列表中下一个和前一个元素的指针中,提供一个打印节点的函数,例如

void PrintNode (Node_t *node, void (*fprint)(void *));

This function will get the elements of a node, and then call a user-provided function to actually print the contents of a node. 此函数将获取节点的元素,然后调用用户提供的函数以实际打印节点的内容。

typedef struct stNode {
  void *NodeContents;
  struct stNode *prev;
  struct stNode *next;
} Node_t;

void PrintNode (Node_t *node, void (*print)(void *))
{
  if (node && node->NodeContents && print)
    print(node->NodeContents);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM