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 = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
newNode->next = NULL; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or head instead of data to newNode->data.
✗ Incorrect
We assign the given data value to the new node's data field.
2fill in blank
mediumComplete the code to handle the case when the list is empty.
DSA C
if (head == NULL) { newNode->next = newNode; head = [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to NULL or data instead of newNode.
✗ Incorrect
When the list is empty, head should point to the new node.
3fill in blank
hardFix the error in updating the last node's next pointer to the new node.
DSA C
struct Node* temp = head; while (temp->next != head) { temp = temp->[1]; } temp->next = newNode;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using temp->prev or temp->data which are invalid or incorrect here.
✗ Incorrect
To traverse the list, move to the next node using temp->next.
4fill in blank
hardFill both blanks to insert the new node at the beginning and update head.
DSA C
newNode->next = [1]; head = [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order or assigning wrong pointers.
✗ Incorrect
The new node points to the old head, and head is updated to newNode.
5fill in blank
hardFill all three blanks to complete the insert at beginning function.
DSA C
void insertAtBeginning(struct Node** head_ref, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
if (*head_ref == NULL) {
newNode->next = newNode;
*head_ref = [2];
return;
}
struct Node* temp = *head_ref;
while (temp->next != *head_ref) {
temp = temp->[3];
}
temp->next = newNode;
newNode->next = *head_ref;
*head_ref = newNode;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pointer names or forgetting to update head_ref.
✗ Incorrect
Assign data to newNode->data, set *head_ref to newNode when empty, and traverse using temp->next.
