为什么不能在不将节点创建为指针的情况下创建链接列表? [英] Why can't you create a linked lists without creating nodes as pointers?

查看:53
本文介绍了为什么不能在不将节点创建为指针的情况下创建链接列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>创建链接列表中有一个答案而不将节点声明为指针.但是我想知道是否还有其他原因导致您无法将节点创建为指针,因此请弄清楚.

There is an answer in Creating a linked list without declaring node as a pointer. But I wanted to know if there were any other reasons due to which you can't create nodes as pointers, just so, to be clear.

原因之一是新节点的范围将在功能之外消失-是否无法解决此问题?还有其他原因吗?

One of the reasons is that the scope of the new nodes will die outside the function-Is there no way you can solve this problem?, and Is there any other reason?

推荐答案

我一直在使用很多链接列表(甚至更复杂的结构),其中没有节点在堆上单独分配,但是所有节点都是元素在单个阵列中.

I've been using a lot of linked lists (and even more complex structures) in which no node was allocated separately on the heap but all the nodes were elements in a single array.

具有指向节点的指针并在堆上分配单个节点的情况很常见,但到目前为止并不是唯一的选择.例如,某些应用程序最好在页面"中分配节点以提高效率或简化处理.

Having pointers to nodes and allocating single nodes on the heap is common, but by far not the only option. For example it may be better for certain applications to allocate nodes in "pages" for efficiency or to simplify disposal.

另一种非常普遍且经常有用的选项是通过使用数组中的数字索引来创建根本没有指针(甚至在节点内部也没有指针)的链表或树.例如

Another option that is quite common and often useful is creating linked lists or trees where there are no pointers at all (not even inside nodes) by using numeric indexes in an array instead. E.g.

struct Tree {
    struct Node {
        double value;
        int left, right; // Index of left/right child, -1 if missing
    };
    int root = -1;
    std::vector<Node> nodes;
};

这篇关于为什么不能在不将节点创建为指针的情况下创建链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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