我们如何阅读异构链接列表? [英] how do we read heterogenous linklist?

查看:67
本文介绍了我们如何阅读异构链接列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建异构链接列表时,我们知道我们使用void指针。但是当它到了阅读信息的时候。

我们如何做到这一点?(意思是

,同时阅读我们如何知道数据所属的数据类型)

While creating a heterogenous linklist we know that we use void pointer. But when it comes the time to read the information.
How do we exactly do that?( means
while reading how do we figure out to what datatype does the data belong)

推荐答案

我会说你在列表节点类型中嵌入了一些类型标识符。一个int或enum会这样做,然后你就可以看到那个id并知道那个节点中有哪种类型的东西。


另外,我假设你正在编写C代码?如果你有C ++可用,你不应该写清单。
I''d say you embed some type identifier into your list node type. An int or enum would do, then you can see that id and know which type of thing is in that node.

Also, I''m assuming you''re writing C code? If you have C++ available you shouldn''t be writing lists.


好吧,如果你用C ++编写,那么你可以使用继承和虚方法来安全地控制元素的清单。这意味着你永远不必明确地测试元素的类型。


在C中,你必须以某种方式识别你的元素。正如mac11所建议的那样,一种有效的方法是将所有元素构建为通用模型的扩展,该模型具有类型和类型。作为它的第一个属性。


例如:
Well, if you''re writing in C++, then you can just use inheritence and virtual methods to safely control the elements of the list. Meaning that you should never have to explicitly test the type of an "element".

In C, you have to identify your elements somehow. As mac11 suggested, an effective way to do this is to build all of your elements as extensions of a common model, which has a "type" as its first attribute.

For example:
展开 | 选择 | Wrap | 行号


使用联合对于各种异类型,你可以避免类型转换:
Use a union for the various heterogenous types and you can avoid the type casts:
展开 | 选择 | 换行 | 行号


这篇关于我们如何阅读异构链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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