如何初始化此数据结构 [英] How to initialize this data structure
问题描述
这是一个双向链接列表.除了下一个和上一个指针之外,每个元素还具有一个子指针,该子指针可以指向也可以不指向单独的双向链接列表.这些子列表可以自己拥有子列表.
像这样的结构:
It is a doubly linked list. In addition to next and previous pointers, each element has a child pointer, which may or may not point to a separate doubly linked list. These child lists could have their child lists on their own.
Structure like this:
struct nodeT
{
struct nodeT *prev;
struct nodeT *next;
struct nodeT *child;
int value;
}
谁能给我一个提示,我如何初始化这样的结构?有了给定的数据.
谢谢大家〜
好吧,我可以想到的一种方法就是输入此结构的扁平化版本,我可以从扁平化版本转换为目标版本,但是我正在寻找更好的解决方案.
我自己实现了一些代码,不知道它是否有效.
希望你们能给我一些帮助!
Could anyone give me a clue how may I initialize one structure like this? With given data.
Thank you guys~
Well, one way I can think is to just input a flatten version of this structure, I can transform from flatten version to target one, but I am looking for a better solution.
I implement some code myself, no idea it works or not.
I hope you guys can give me some help!
typedef struct nodeT{
struct nodeT *next;
struct nodeT *prev;
struct nodeT *child;
int value;
}node;
//flatten fn:
/*void Flatten(node *head,node **tail)
{
node *currentNode=head;
while(currentNode)
{
if(node->child)
{
Append(currentNode->child,**tail)
}
node=node->next;
}
}
void Append(node *child,**tail)
{
child->prev=*tail;
(*tail)->next=child;
node *curNode=child;
while(curNode)
{
*tail=curNode;
curNode=curNode->next;
}
}
*/
void Initialize(node **currentNode)
{
int data,tag_1,tag_2;
printf("Input data of current node:\n");scanf("%d",&data);
(*currentNode)->value=data;
node **temp_1;node **temp_2;
printf("Does current node have a child?(1 for yes, 0 for no)\n");scanf("%d",&tag_1);
printf("Does current node have next node?(1 for yes, 0 for no)\n");scanf("%d",&tag_2);
if(tag_1==1)
{
(*currentNode)->child=(node *)malloc(sizeof(node));
temp_1=&((*currentNode)->child);
Initialize(temp_1);
}
if(tag_2==1)
{
(*currentNode)->next=(node *)malloc(sizeof(node));
temp_2=&((*currentNode)->next);
(*currentNode)=(*temp_1)->prev;
Initialize(temp_2);
}
}
推荐答案
请在对问题的评论中看到我的问题.
如果可以,将其设置为C ++,而不是C,然后使用一组构造函数对其进行初始化.另外,请遵循封装原则( http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 [ ^ ]),将这些成员隐藏在访问修饰符private
下(类默认为private
,但struct
默认为public
),将其替换为仅显示所需内容的函数.
它为您提供线索吗?
—SA
Please see my question in my comment to the question.
If you can, make it C++, not C and initialize it using a set of constructors. Also, follow the encapsulation principles (http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29[^]), hide these members under access modifierprivate
(private
is default for classes, but default forstruct
ispublic
), replace it with functions which expose only what is needed.
Does it provide you a clue?
—SA
我首先将节点中的每个属性都清除为0.然后,当您导航插入您的节点时,您将始终有一个基准来进行测试下一个节点,您已经找到了树的底部.
您可以简化函数中的几件事,然后可能会更加清楚如何初始化节点.当前编写函数Initialize的方式,您必须像这样调用它:
I would start by clearing out each property in your node to 0. Then you will always have a baseline to test against when you navigate to insert your next node and you have found the bottom of your tree.
You can simplify a few things in your function, then it might be a bit clearer how to initialize your node. The way you currently have the function Initialize written, you have to call it like this:
Node elt;
Initialize(&(&elt));
或
or
Node elt;
Node *pElt = &elt;
Initialize(&pElt);
从要初始化的结构开始.您没有为实际节点分配任何新内存,因此可以删除*之一,并使用单个指针而不是指向指针的指针:
Start with the struct that you will pass into be initialized. You are not allocating any new memory for the actual node, so you can remove one of the *, and use a single pointer rather than a pointer to a pointer:
void Initialize(node *currentNode)
然后,您可以像这样调用该函数:
You can then call the function like this:
Node elt;
Initialize(&elt);
另外,您不必将函数中的每次访问都重新引用到currentNode两次;
第一次:(* currentNode)
第二次:->
您可以将所有
Also, you don''t have to dereference every access in your function to currentNode twice;
first time: (*currentNode)
second time: ->
You can replace all of the
(*currentNode)->
替换为
currentNode->
除此之外,我不确定您将如何初始化这些项目.我可以告诉你,如果用户对第一个问题的回答为否",那么是否有一个孩子,但对下一个节点为是",则不会初始化temp_1,并且您将尝试访问未初始化的指针.
另外,我看不到任何初始化您的上一个数据成员的代码.
最后一个技巧,就像对Initialize(¤tNode)的调用一样,如果将temp_1和temp_2的定义更改为使用单个指针而不是像这样的指针,则可以使用指针简化调用: br/>
Beyond that, I am not sure exactly how you need the items to be initialized. I can tell you that if the user answers no for the first question of is there a child, but yes for a next node, temp_1 will not be initialized, and you will attempt to access an uninitialized pointer.
Also, I do not see any code that initializes your prev data member.
One final tip, just like with the call to Initialize(¤tNode), you can simplify your calls with the pointers, if you change the definitions for temp_1 and temp_2 to use a single pointer rather than a pointer to a pointer like this:
node *temp_2;
...
currentNode->next = (node *)malloc(sizeof(node));
temp_2 = currentNode->next;
currentNode = temp_1->prev;
Initialize(temp_2);
这篇关于如何初始化此数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!