Complete the code to define the node structure for a singly linked list.
typedef struct Node {
int data;
struct Node* [1];
} Node;The pointer to the next node in a singly linked list is commonly named next.
Complete the code to create a new node dynamically.
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof([1]));
if (newNode == NULL) return NULL;
newNode->data = value;
newNode->next = NULL;
return newNode;
}We allocate memory for the Node structure using sizeof(Node).
Fix the error in the function to append a node at the end of the list.
void appendNode(Node** head, int value) {
Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while (temp->[1] != NULL) {
temp = temp->next;
}
temp->next = newNode;
}To traverse the list, we follow the next pointer until it is NULL.
Fill both blanks to insert a node at the beginning of the list.
void insertAtHead(Node** head, int value) {
Node* newNode = createNode(value);
newNode->[1] = *head;
*head = [2];
}The new node's next points to the current head, then head is updated to the new node.
Fill all three blanks to print all elements of the singly linked list.
void printList(Node* head) {
Node* current = head;
while (current != [1]) {
printf("%d -> ", current->[2]);
current = current->[3];
}
printf("NULL\n");
}We loop until current is NULL, print current's data, then move to current->next.
