[英]Implementing General Tree using the Linked List data structure
I am trying to generate the following general tree:我正在尝试生成以下通用树:
root
|
----------------
| |
child child
|
----
|
child
|
-------------
| | |
child child child
I am planning to use two classes, one stores a parent node element (Tree class) and the other implements linked list for storing children (Single_linked_list).我打算使用两个类,一个存储父节点元素(树类),另一个实现用于存储子节点的链表(Single_linked_list)。
My class definition for the Tree class is:我对树 class 的 class 定义是:
template <class Object>
class Tree
{
private:
Object node_val; // this is stored in node of tree
Single_linked_list< Tree<Object> * > children;
public:
// accessors and mutators
...
}
I just wanted to confirm that the Single_linked_list< Tree<Object> * > children;
我只是想确认
Single_linked_list< Tree<Object> * > children;
should have the *
in it because in c++ that is the way of specifying that it is to be a pointer, pointing the start of the linked_list with all the children?应该有
*
因为在 c++ 中这是指定它是一个指针的方式,指向所有孩子的链接列表的开始?
Please let me know if my interpretation of that line of code is correct.请让我知道我对那行代码的解释是否正确。
If I understand your question, you're wondering what the Single_linked_list class template parameter means.如果我理解您的问题,您想知道 Single_linked_list class 模板参数的含义。
Your statement:您的声明:
in c++ [the
*
] is the way of specifying that it is to be a pointer, pointing the start of the linked_list with all the children在 c++ [
*
] 是指定它是一个指针的方式,指向所有孩子的链接列表的开始
is worth discussion.值得讨论。 The
Single_linked_list
template class manages instances of the type given as the template parameter. Single_linked_list
模板 class 管理作为模板参数给出的类型的实例。 The template parameter does not change how the list is used.模板参数不会改变列表的使用方式。 The managed type identified by the template parameter may be built-in - like a pointer - or could be a class.
由模板参数标识的托管类型可能是内置的(如指针),也可能是 class。 Whatever the template parameter, I would assume that access to the start of the linked list and functions to traverse the list will be accessed by calling methods on
children
eg无论模板参数是什么,我都会假设访问链表的开头和遍历列表的函数将通过调用
children
级的方法来访问,例如
Single_linked_list< SomeClassOrType > my_list;
putThingsOnList( &my_list );
my_list.goToFirst();
while( !my_list.hasNext() )
{
SomeClassOrType &o = children.getCurrent();
children.goToNext();
}
The first part of your statement quoted above is correct: the *
specifies a pointer type.上面引用的语句的第一部分是正确的:
*
指定指针类型。 The second part of your statement is where I disagree: the template parameter is not related to the idea of the start of the linked_list.您陈述的第二部分是我不同意的地方:模板参数与链接列表开始的想法无关。
I hope you find my answer valuable.我希望你觉得我的回答很有价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.