When creating a new node in a linked list, is it legal to use designated initializers to initialize the members of the node as mentioned below? Is there any repercussion in doing so and what would be a better way to achieve the same result? (lang : C++)
Node *temp = new Node{.data = value, .next = NULL};
struct Node
{
int data;
Node *next;
};
I think you can use function as a constructor.
Node* newFunction(int data) {
Node* newNode = malloc(sizeof(Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
And after that, you can use in the main part like that;
Node* newNode = newFunction(5);