To create a node in a singly linked list in C, you would typically define a structure representing the node, like this:
c
struct Node {
int data;
struct Node* next;
};
Then, you can create a function to allocate memory for a new node and initialize its data and next pointer:
c
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
You can then call this function to create a new node with the desired data value. For example:
c
struct Node* newNode = createNode(5);
Make sure to handle memory allocation failure gracefully in your code.