Bird
0
0
DSA Cprogramming~20 mins

Insert at End Tail Insert in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Tail Insert in Linked List
What is the printed linked list after inserting 10, 20, and 30 at the end using tail insert?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

Node* tailInsert(Node* head, int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    if (head == NULL) {
        return newNode;
    }
    Node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    return head;
}

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main() {
    Node* head = NULL;
    head = tailInsert(head, 10);
    head = tailInsert(head, 20);
    head = tailInsert(head, 30);
    printList(head);
    return 0;
}
A10 -> 20 -> 30 -> NULL
B30 -> 20 -> 10 -> NULL
CNULL
D10 -> 20 -> NULL
Attempts:
2 left
💡 Hint
Remember that tail insert adds nodes at the end, preserving the order of insertion.
🧠 Conceptual
intermediate
1:00remaining
Number of Nodes After Tail Insertions
If you start with an empty linked list and perform tail insertions of 5 nodes, how many nodes will the list contain?
A0
B4
C6
D5
Attempts:
2 left
💡 Hint
Each tail insertion adds exactly one node to the list.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Tail Insert Function
What error will occur when running this tail insert function code snippet?
DSA C
Node* tailInsert(Node* head, int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    Node* temp = head;
    if (head == NULL) {
        head = newNode;
    }
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    return head;
}
AMemory leak due to missing free
BSegmentation fault due to dereferencing NULL pointer
CNo error, works correctly
DCompilation error due to missing return
Attempts:
2 left
💡 Hint
Check what happens when head is NULL and you try to access temp->next.
📝 Syntax
advanced
1:30remaining
Correct Syntax for Tail Insert Loop
Which option correctly iterates to the last node in a singly linked list for tail insertion?
Afor (; temp->next != NULL; temp = temp->next);
Bwhile (temp != NULL) { temp = temp->next; }
Cwhile (temp->next != NULL) { temp = temp->next; }
Dwhile (temp->next) { temp = temp->next; }
Attempts:
2 left
💡 Hint
You want to stop at the last node, not go past it.
🚀 Application
expert
3:00remaining
Final Linked List After Multiple Tail Inserts and One Deletion
Given an empty linked list, you perform tail insertions of values 1, 2, 3, 4, 5 in order. Then you delete the node with value 3. What is the printed linked list?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

Node* tailInsert(Node* head, int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    if (head == NULL) {
        return newNode;
    }
    Node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    return head;
}

Node* deleteNode(Node* head, int val) {
    if (head == NULL) return NULL;
    if (head->data == val) {
        Node* temp = head->next;
        free(head);
        return temp;
    }
    Node* curr = head;
    while (curr->next != NULL && curr->next->data != val) {
        curr = curr->next;
    }
    if (curr->next != NULL) {
        Node* temp = curr->next;
        curr->next = temp->next;
        free(temp);
    }
    return head;
}

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main() {
    Node* head = NULL;
    head = tailInsert(head, 1);
    head = tailInsert(head, 2);
    head = tailInsert(head, 3);
    head = tailInsert(head, 4);
    head = tailInsert(head, 5);
    head = deleteNode(head, 3);
    printList(head);
    return 0;
}
A1 -> 2 -> 4 -> 5 -> NULL
B1 -> 2 -> 3 -> 4 -> 5 -> NULL
C2 -> 4 -> 5 -> NULL
D1 -> 2 -> 4 -> NULL
Attempts:
2 left
💡 Hint
Deleting node with value 3 removes it from the list, others remain in order.