I was trying to make a tree in c++ and came across this code which is really confusing me.
struct node
{
int data;
struct node* left;
struct node* right;
};
// The code bellow this is the part i dont understand
struct node* newNode(int idata)
{
node* node = new struct node;
node->data = idata;
node->left = NULL;
node->right = NULL;
return node;
}
what is struct node* ? Some kind of a structure, but a pointer? Also shouldn't structs have ; at the end? For example, node has ; at the end of the body but not node* ?
struct node*is the return type of functionnewNode()and not a definition in itself. The line you are referring to is the signature of the functionnewNode. It is followed by the function definition between the two curly braces {}. A function definition does not need a;at the end.