add code for createNode function (#22472)

This commit is contained in:
Varun kumar
2018-11-24 15:18:21 +05:30
committed by nik
parent 9db07d9b50
commit e68084401d

View File

@ -102,6 +102,20 @@ struct node
}; };
``` ```
### Code for node creation
createNode() returns a new node with the given data and NULL left and right pointers.
``` c++
struct node* newNode(int element)
{
struct node* temp = (node*)malloc(sizeof(node)); //Allocate memeory for temp node
temp->data = element; // Assign element to temp
temp->left = NULL; // Initialize left child as NULL
temp->right = NULL; // Initialize right child as NULL
return temp;
}
```
#### More Information: #### More Information:
* [CMU lesson notes](http://www.cs.cmu.edu/~clo/www/CMU/DataStructures/Lessons/lesson4_1.htm) * [CMU lesson notes](http://www.cs.cmu.edu/~clo/www/CMU/DataStructures/Lessons/lesson4_1.htm)