Bird
0
0
DSA Cprogramming~10 mins

Insert at Middle Specific Position 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(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = [1];
    node->next = NULL;
    return node;
}
Drag options to blanks, or click blank then click option'
Anode
BNULL
C0
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the input data.
Using the variable name 'node' instead of 'data'.
2fill in blank
medium

Complete the code to traverse the list until the position before insertion.

DSA C
struct Node* temp = head;
int i = 1;
while (i < [1] - 1 && temp != NULL) {
    temp = temp->next;
    i++;
}
Drag options to blanks, or click blank then click option'
Apos
Bdata
Chead
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' or 'head' instead of the position variable.
Incorrect loop condition causing infinite loop.
3fill in blank
hard

Fix the error in linking the new node after the temp node.

DSA C
new_node->next = [1]->next;
[1]->next = new_node;
Drag options to blanks, or click blank then click option'
Atemp
Bhead
Cnew_node
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'new_node' instead of 'temp' causes wrong linking.
Assigning NULL breaks the list.
4fill in blank
hard

Fill both blanks to check if position is valid and handle insertion at head.

DSA C
if (pos == 1) {
    new_node->next = [1];
    [2] = new_node;
} else {
    // insertion in middle or end
}
Drag options to blanks, or click blank then click option'
Ahead
Btemp
Cnew_node
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'temp' or 'new_node' instead of 'head' causes wrong head update.
Not updating head pointer leads to lost list.
5fill in blank
hard

Fill all three blanks to complete the insertAtPosition function.

DSA C
void insertAtPosition(struct Node** head_ref, int data, int pos) {
    struct Node* new_node = newNode(data);
    if (pos == 1) {
        new_node->next = [1];
        [2] = new_node;
        return;
    }
    struct Node* temp = *head_ref;
    int i = 1;
    while (i < pos - 1 && temp != NULL) {
        temp = temp->next;
        i++;
    }
    if (temp == NULL) {
        printf("Position out of range\n");
        free(new_node);
        return;
    }
    new_node->next = [3]->next;
    [3]->next = new_node;
}
Drag options to blanks, or click blank then click option'
A*head_ref
Ctemp
Dnew_node
Attempts:
3 left
💡 Hint
Common Mistakes
Not dereferencing head_ref correctly.
Using wrong pointer for linking new_node.