在下面的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/