Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the list is empty before deletion.
DSA C
if (head == [1]) { printf("List is empty\n"); return; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking head->next instead of head.
Using 0 instead of NULL for pointer comparison.
✗ Incorrect
We check if head is NULL to know if the list is empty.
2fill in blank
mediumComplete the code to move the head pointer to the next node after deletion.
DSA C
struct Node* temp = head;
head = head->[1];
free(temp); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move head.
Not freeing the old head node.
✗ Incorrect
After deleting the first node, head should point to the next node.
3fill in blank
hardFix the error in setting the previous pointer of the new head node to NULL.
DSA C
if (head != NULL) { head->[1] = NULL; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'next' to NULL instead of 'prev'.
Not checking if head is NULL before setting pointer.
✗ Incorrect
The previous pointer of the new head should be set to NULL.
4fill in blank
hardFill both blanks to complete the function that deletes the first node of the doubly linked list.
DSA C
void deleteBeginning(struct Node** head_ref) {
if (*head_ref == [1]) {
return;
}
struct Node* temp = *head_ref;
*head_ref = (*head_ref)->[2];
if (*head_ref != NULL) {
(*head_ref)->prev = NULL;
}
free(temp);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move head.
Not checking if *head_ref is NULL before deletion.
✗ Incorrect
We check if *head_ref is NULL to see if list is empty, then move head to next node.
5fill in blank
hardFill all three blanks to complete the main function that creates a list and deletes the first node.
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* prev; struct Node* next; }; void deleteBeginning(struct Node** head_ref) { if (*head_ref == NULL) { return; } struct Node* temp = *head_ref; *head_ref = (*head_ref)->next; if (*head_ref != NULL) { (*head_ref)->prev = NULL; } free(temp); } int main() { struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; head = (struct Node*)malloc(sizeof(struct Node)); second = (struct Node*)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); head->data = 1; head->prev = NULL; head->next = second; second->data = 2; second->prev = head; second->next = third; third->data = 3; third->prev = second; third->next = NULL; deleteBeginning(&head); printf("List after deletion: "); struct Node* current = head; while (current != NULL) { printf("%d ", current->[1]); current = current->[2]; } printf("\n"); free(second); free(third); return [3]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing 'next' or 'prev' instead of 'data'.
Returning 1 instead of 0 from main.
Using wrong pointer to traverse the list.
✗ Incorrect
Print node data, move to next node, and return 0 from main.
