Why can't you create a linked lists without creating nodes as pointers?

倖福魔咒の 提交于 2021-01-27 19:14:19

问题


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?


回答1:


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;
};



回答2:


A linked list is literally defined as a structure in which objects point to one another:

In computer science, a linked list is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next.

In C++, an object can point to another using a pointer or a reference.

A reference is possible but inconvenient, since it must be set at object creation and cannot be modified.

Any other structure in which you can navigate from one object to another without following a pointer would not be a linked list. A typical example is an array: you can navigate to the next element by computing its address relative to the current one.

Now how you allocate nodes is entirely up to you - you can allocate them in dynamic memory, in automatic storage (on the stack), statically, in shared memory, etc. If they are linked using a pointer or a reference, you'll still have a linked list structure.




回答3:


Another reason I can think of is that it would be more convenient to traverse the linked list. For example:

Node* current;
while (current != nullptr) {
    // Do something
    current = current->next;
}

If current is not a pointer, you probably will make copies of each node in the link list, which is really a bad idea.




回答4:


If you have a linked list in shared memory or reflective memory, then the world of pointers is non existent. You are back to the 1970s, using array indices instead of pointers.

It is a very different world and a total culture shock for those who are used to pointers. There is no heap and you have to manage your own garbage collection.




回答5:


Why can't you create a linked lists without creating nodes as pointers?

You can but you most definitly need some sort of indirection.

Why are a pointers commonly used to implement linked Lists? As @6502 already pointed out in his answer you can achieve the same thing by arrays or any other container which you keep as page/memory of the list. But for the sake of the argument lets compare a List implemented by pointers and one with keeps the node as member. The main answer is: It is easier to implement the functionality of a list. The benefit of Lists is you can easily push/pop front and back and delete items very fast. How is deleting in list done implementation wise? You simply change the pointer which is pointing to the "to be deleted object" to the next node (or in the array case you assign another index). But if your "to be deleted objected" conatins the other Object as real Object and not via pointer you will have to move it out of there before deleting it otherwise the it and the rest of the list would be gone.

Also you can only implement a single-linked list, because only one Node can hold the other. If Node A has NodeB it is not possible that NodeBcan also have NodeA. But with pointers NodeB can point to NodeA without problems and vice versa.

TL;DR If you use pointers or arrays your list actions like deleting, pushing and so on are simnply switching pointers or indices. If your nodes contain the data you need to up date these by moving/copying. And this is cumbersome to implement and much harder to grasp than the pointer/reference indirection.



来源:https://stackoverflow.com/questions/61097899/why-cant-you-create-a-linked-lists-without-creating-nodes-as-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!