Bird
0
0
DSA Cprogramming~10 mins

Delete from Beginning of Doubly Linked List in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Ahead->next
BNULL
Chead
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking head->next instead of head.
Using 0 instead of NULL for pointer comparison.
2fill in blank
medium

Complete 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'
Aprev
Bdata
Cnext
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move head.
Not freeing the old head node.
3fill in blank
hard

Fix 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'
Aprev
Bnext
Cdata
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'next' to NULL instead of 'prev'.
Not checking if head is NULL before setting pointer.
4fill in blank
hard

Fill 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'
ANULL
Bhead
Cnext
Dprev
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move head.
Not checking if *head_ref is NULL before deletion.
5fill in blank
hard

Fill 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'
Adata
Bnext
C0
D1
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.