1

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;
};
trincot
  • 317,000
  • 35
  • 244
  • 286
Sameer Ahmed
  • 53
  • 1
  • 7

1 Answers1

1

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);
Ogün Birinci
  • 598
  • 3
  • 11