c - 节点*和*节点有什么区别?

标签 c pointers

在下面的c++二叉树实现中,使用指针node*和*node有什么区别。我对指针的实现很弱。

struct node  
{ 
    int data; 
    struct node *left; 
    struct node *right; 
}; 

struct node* newNode(int data)
{ 

  struct node* node = (struct node*)malloc(sizeof(struct node)); 

  node->data = data; 

  node->left = NULL; 
  node->right = NULL; 
  return(node); 
} 


int main() 
{ 
  struct node *root = newNode(1);   
  root->left        = newNode(2); 
  root->right       = newNode(3);

  root->left->left  = newNode(4); 

  getchar(); 
  return 0; 
}

最佳答案

使用相同的数据类型名称和对象名称是相当不好的做法。

struct structnode  
{ 
    int data; 
    struct structnode *left; 
    struct structnode *right; 
}; 

struct structnode* newNode(int data)
{ 

  struct structnode* node = (struct node*)malloc(sizeof(struct node)); 

  node->data = data; 

  node->left = NULL; 
  node->right = NULL; 
  return(node); 
} 


int main() 
{ 
  struct structnode *root = newNode(1);   
  root->left        = newNode(2); 
  root->right       = newNode(3);

  root->left->left  = newNode(4); 

  getchar(); 
  return 0; 
 }

现在困惑已经消失了。

关于c - 节点*和*节点有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55203198/

相关文章:

c - C中的链表实现错误

c - 迭代程序

c - 如何在 GCC 内联汇编中使用标签?

C# 编码(marshal) uint*& 参数

嵌套 For 循环后的计数值 - 谷歌撰写

c - OpenGL 中的纹理映射(使用 SOIL)

c - 如何在不收到警告 `strtod` char "Assigning to ' const char *' from ' 的情况下复制 *' discards qualifier"等的功能?

c - 双指针指针

c++ - 为什么 C++ 变量是指针时不需要正确定义?

c - 如何在 C 中取消引用 void*?