简体   繁体   English

指向结构的指针成员以在C中构造自己

[英]Pointing member of struct to struct itself in C

Here are two structs I'm using. 这是我正在使用的两个结构。 I'm not sure how to initialize so that member of the struct points to struct itself. 我不确定如何初始化,以便该结构的成员指向结构本身。 I'm not sure if this is possible or not. 我不确定这是否可行。

typedef struct __node 
{
   int value;
   struct __node* left;
   struct __node* right;
}setNode;

typedef struct __set {
   setNode* root;
   //mutex for this set (To be implemented)
}set;

I want to initialize members of set but not sure how should I do to get result->root to point to same address as that of set result . 我想初始化set的成员,但不确定如何使result->root指向与set result相同的地址。

set* new_set() 
{ 
  set* result = malloc(sizeof(set));
  result -> root = NULL; // Not sure???
}

I want to get the pointer to root. 我想获得指向根的指针。

void someOther(set* s)
{
   setNode* temp = s -> root;
   //....
}

Sorry if the question is too vague. 抱歉,这个问题太模糊了。

Additional Info

I want two structs. 我想要两个结构。 One which is contains nodes of a tree[setNode] and second struct which contains pointer to root of the tree and some other members(like mutex for that tree)[set]. 其中一个包含树的节点[setNode],第二个结构包含指向树的根和其他成员(例如该树的互斥体)[set]的指针。 That is not a problem. 那不是问题。

Problem: In a function, I have setNode* temp = someSet -> root; 问题:在一个函数中,我有setNode* temp = someSet -> root; such that I should be able to traverse over the tree ie temp should point to root of someSet. 这样我应该能够遍历树,即temp应该指向someSet的根。 So what should I assign to result -> root in new_set function? 那么我应该在new_set函数中为result -> root分配什么呢?

An empty set doesn't contain any elements. 空集不包含任何元素。 If a set is represented by a binary search tree, a NULL root pointer is appropriate for that. 如果集合由二进制搜索树表示,则NULL根指针适用于此。

set* new_set() 
{ 
   set* result = malloc(sizeof(set));
   result -> root = NULL; // Yes, absolutely positively 100% sure
   return s; // Don't forget!
}

If you want to get the pointer to root, get it. 如果要获取指向根的指针,请获取它。

void someOther(set* s)
{
    setNode* temp = s -> root;
    //!!!!
}

Nothing whatsoever is wrong with temp being NULL. temp为NULL没什么不对。 Your function must be able to handle that. 您的函数必须能够处理该问题。 If you want a recursive traversal function, write one that gets a setNode* argument: 如果需要递归遍历函数,请编写一个具有setNode*参数的函数:

void someOther(set* s)
{
    setNode* temp = s -> root;
    someOtherDoThisActuallyIMeanItNow (temp);
}

void someOtherDoThisActuallyIMeanItNow (setNode* current)
{
    ...
    if (current) { // <-- here we check that it's not NULL
       ...
       someOtherDoThisActuallyIMeanItNow (current->left);
       ...
       someOtherDoThisActuallyIMeanItNow (current->right);
    } else {
       // do whatever is appropriate for an empty tree/set
    }
}

If you need a function that recursively traverses and modifies a tree, make one that accepts a setNode** argument. 如果您需要一个递归遍历和修改树的函数,请使之接受setNode**参数。

void someOtherWithMutation(set* s)
{
    setNode* temp = s -> root;
    someOtherWithMutationNow (&temp); // <---- here
}

void someOtherWithMutationNow (setNode** current) // <---- here
{
    ...
    if (*current) { // <-- here we check that it's not NULL
       ...
       someOtherDoThisActuallyIMeanItNow ((*current)->left);
       ...
       someOtherDoThisActuallyIMeanItNow ((*current)->right);
       ...
       if (...) {
           free (*current);
           *current = NULL;
       }           
    } else {
       // do whatever is appropriate for an empty tree/set
       ...
       *current = malloc(sizeof(setNode));
       ...          
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM