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->prev = NULL;
newNode->next = NULL; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL instead of data to newNode->data.
Using head instead of data for newNode->data.
✗ Incorrect
The new node's data field should be set to the given data value.
2fill in blank
mediumComplete the code to link the new node's next pointer to the current head.
DSA C
newNode->next = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode->next to NULL instead of head.
Setting newNode->next to newNode itself.
✗ Incorrect
The new node's next pointer should point to the current head node.
3fill in blank
hardFix the error in updating the previous pointer of the old head node.
DSA C
if (head != NULL) { head->[1] = newNode; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Updating the next pointer instead of prev.
Trying to assign to head instead of head->prev.
✗ Incorrect
The old head's previous pointer should point back to the new node.
4fill in blank
hardFill both blanks to update the head pointer and complete the insertion.
DSA C
head = [1]; return [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the updated head.
Assigning head to newNode->next instead of newNode.
✗ Incorrect
The head pointer should be updated to the new node, and the updated head returned.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning function.
DSA C
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
newNode->prev = NULL;
newNode->next = [2];
if (head != NULL) {
head->prev = [3];
}
head = newNode;
return head;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up pointers for next and prev.
Not updating head->prev when head is not NULL.
✗ Incorrect
Assign data to newNode->data, link newNode->next to head, and update head->prev to newNode.
