如果没有列表本身就可以执行链接列表 [英] It is possible and how to do a linked list without the list itself

查看:83
本文介绍了如果没有列表本身就可以执行链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

所以我的问题是这样的:

有可能以及如何在没有列表的情况下进行链接列表?



我的意思是我在网上找到的每个链表(我可能错过了很多)都是由两部分组成的,Node类(或struct)和一个列表类包含链接节点的列表,管理所有prev和next对象。所以我的问题是我是否只能从Node类中获得链表。所以Node类应该有* prev和* next,并且应该管理* prev和* next对象的分配和解除定位。这甚至是可管理的并且是个好主意吗?

Hi everyone,
So my question is this:
It is possible and how to do a linked list without the list itself?

What I mean by this is on every linked list I can find on the net (I may have missed many) is constructed from two parts, the Node class (or struct) and a list class which containts the list of the linked nodes, managing all of the prev and next objects. So my question is can I have a "linked list" only from the Node class. So the Node class should have *prev and *next and should manage the allocation and dellocation of the *prev and *next objects. Is that even managable and a good idea?

推荐答案

是的,在纯粹的C ++中非常简单 - 你只需使用指针。

如何使用C / C ++创建链接列表 [ ^ ]
Yes, in "pure" C++ that's pretty simple - you just use pointers.
How to create Linked list using C/C++[^]


虽然我感谢你以不同的方式思考,但我会说没有'list'的链接'list'并不是一个好主意。链表基本上是链接在一起的节点列表。如果没有列表类,您打算在哪里保留对链接列表的头部和/或结尾的引用?您计划在哪里实现链接列表特定的功能,如遍历,插入,删除等?最终没有'List',你最终会用C ++实现一个面向过程的链接列表程序。



'OriginalGriff'共享的链接显示了a的实现纯C中的链表。虽然理解链表的基础是一个很好的开始,但它不是用C ++实现链表的最佳方式。



While I appreciate you thinking in a different way, I would say that is not a good idea to have a linked 'list' without 'list'. A linked list is basically a list of Nodes linked together. When there is no 'List' class where do you plan to keep the reference to the Head and/or End of the linked list? And where do you plan to implement the Linked List specific functionalities such as traversal, insertion, deletion etc.? Without a 'List' eventually you would end up in implementing a procedural oriented linked list program in C++.

The link that 'OriginalGriff' has shared shows the implementation of a linked list in "pure C". While it is a good start to understand the basics of linked list, it is not the best way to implement a linked list in C++.

引用:

Node类应该有* prev和* next,并且应该管理* prev和* next对象的分配和解除分配。

the Node class should have *prev and *next and should manage the allocation and de-allocation of the *prev and *next objects.





当然,prev和next指针应该是Node类的一部分。但它不是Node类为它们分配内存的责任。实际上* prev和* next分别指向将节点链接到其上一个节点和下一个节点的指针。如果Node本身负责分配和取消分配其prev和next指针,那么它就像一个Node创建其上一个和下一个节点。在这种情况下,最基本的问题是谁负责创建第一个节点?



Of course the prev and next pointers should be a part of the Node class. But its not the duty of the Node class to allocate memory for them. Actually *prev and *next are pointers to link a Node to its previous and next Nodes respectively. If a Node itself is responsible for allocating and de-allocating its prev and next pointers, then it would be like a Node creating its previous and next Nodes. In that case the most basic question would be 'Who is responsible for creating the first Node?'


这篇关于如果没有列表本身就可以执行链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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