Bird
0
0
DSA Cprogramming~10 mins

Insert at 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 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'
ANULL
Bdata
Chead
DnewNode
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL instead of data to newNode->data.
Using head instead of data for newNode->data.
2fill in blank
medium

Complete 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'
ANULL
BnewNode->prev
Chead
DnewNode
Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode->next to NULL instead of head.
Setting newNode->next to newNode itself.
3fill in blank
hard

Fix 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'
Adata
Bnext
Chead
Dprev
Attempts:
3 left
💡 Hint
Common Mistakes
Updating the next pointer instead of prev.
Trying to assign to head instead of head->prev.
4fill in blank
hard

Fill 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'
AnewNode
Bhead
CNULL
DnewNode->next
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the updated head.
Assigning head to newNode->next instead of newNode.
5fill in blank
hard

Fill 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'
Adata
Bhead
CnewNode
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up pointers for next and prev.
Not updating head->prev when head is not NULL.