Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given data.
DSA C
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = [1];
node->next = NULL;
return node;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the input data.
Using the variable name 'node' instead of 'data'.
✗ Incorrect
The new node's data field should be set to the input parameter 'data'.
2fill in blank
mediumComplete the code to traverse the list until the position before insertion.
DSA C
struct Node* temp = head; int i = 1; while (i < [1] - 1 && temp != NULL) { temp = temp->next; i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' or 'head' instead of the position variable.
Incorrect loop condition causing infinite loop.
✗ Incorrect
We traverse until the node before the desired position, so we use 'pos'.
3fill in blank
hardFix the error in linking the new node after the temp node.
DSA C
new_node->next = [1]->next; [1]->next = new_node;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'new_node' instead of 'temp' causes wrong linking.
Assigning NULL breaks the list.
✗ Incorrect
We link the new node after 'temp', so 'temp' is used to access the next node.
4fill in blank
hardFill both blanks to check if position is valid and handle insertion at head.
DSA C
if (pos == 1) { new_node->next = [1]; [2] = new_node; } else { // insertion in middle or end }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'temp' or 'new_node' instead of 'head' causes wrong head update.
Not updating head pointer leads to lost list.
✗ Incorrect
When inserting at position 1, new_node points to current head, and head is updated to new_node.
5fill in blank
hardFill all three blanks to complete the insertAtPosition function.
DSA C
void insertAtPosition(struct Node** head_ref, int data, int pos) {
struct Node* new_node = newNode(data);
if (pos == 1) {
new_node->next = [1];
[2] = new_node;
return;
}
struct Node* temp = *head_ref;
int i = 1;
while (i < pos - 1 && temp != NULL) {
temp = temp->next;
i++;
}
if (temp == NULL) {
printf("Position out of range\n");
free(new_node);
return;
}
new_node->next = [3]->next;
[3]->next = new_node;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not dereferencing head_ref correctly.
Using wrong pointer for linking new_node.
✗ Incorrect
For position 1, new_node->next points to *head_ref and *head_ref is updated. For middle insertion, new_node->next and temp->next are linked.
