Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to pop the top node from the linked list stack.
DSA C
struct Node* pop(struct Node** top) {
if (*top == NULL) {
return NULL;
}
struct Node* temp = *top;
*top = [1];
return temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using (*top)->prev instead of (*top)->next.
Using top->next which is incorrect because top is a double pointer.
Not updating the top pointer at all.
✗ Incorrect
To pop the top node, we move the top pointer to the next node in the list, which is (*top)->next.
2fill in blank
mediumComplete the code to free the popped node after removing it from the stack.
DSA C
void pop_and_free(struct Node** top) {
if (*top == NULL) {
return;
}
struct Node* temp = *top;
*top = (*top)->next;
[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Freeing *top instead of temp.
Freeing temp->next which is still part of the stack.
Freeing the double pointer top which is incorrect.
✗ Incorrect
After updating the top pointer, we free the memory of the popped node stored in temp.
3fill in blank
hardFix the error in the pop function to correctly handle empty stack and return the popped node.
DSA C
struct Node* pop(struct Node** top) {
if ([1]) {
return NULL;
}
struct Node* temp = *top;
*top = (*top)->next;
return temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking top == NULL which checks the pointer to pointer, not the node.
Using != NULL instead of == NULL.
Not checking for empty stack at all.
✗ Incorrect
We check if the stack is empty by testing if *top is NULL before popping.
4fill in blank
hardFill both blanks to update the top pointer and detach the popped node from the list.
DSA C
struct Node* pop(struct Node** top) {
if (*top == NULL) {
return NULL;
}
struct Node* temp = *top;
*top = [1];
[2] = NULL;
return temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting temp->next to NULL instead of temp->prev.
Updating top to temp->next instead of (*top)->next.
Not detaching the popped node properly.
✗ Incorrect
We move top to the next node and set the popped node's prev pointer to NULL to detach it.
5fill in blank
hardFill all three blanks to pop the top node, update the top pointer, and free the popped node safely.
DSA C
void pop_and_free(struct Node** top) {
if (*top == NULL) {
return;
}
struct Node* temp = *top;
*top = [1];
if (*top != NULL) {
[2] = NULL;
}
[3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using (*top)->next instead of temp->next to update top.
Not checking if *top is NULL before setting prev.
Freeing *top instead of temp.
✗ Incorrect
We update top to temp->next, set the new top's prev to NULL, and free the popped node temp.
