Bird
0
0
DSA Cprogramming~20 mins

Create and Initialize Doubly Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Doubly Linked List Initialization
What is the printed state of the doubly linked list after running the following code?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

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

int main() {
    Node* head = createNode(10);
    Node* second = createNode(20);
    Node* third = createNode(30);

    head->next = second;
    second->prev = head;
    second->next = third;
    third->prev = second;

    printList(head);
    return 0;
}
A30 <-> 20 <-> 10 -> NULL
B10 -> 20 -> 30 -> NULL
C10 <-> 20 -> 30 -> NULL
D10 <-> 20 <-> 30 -> NULL
Attempts:
2 left
💡 Hint
Check how the next and prev pointers are linked between nodes.
Predict Output
intermediate
2:00remaining
State of Doubly Linked List After Adding a Node at Head
What is the printed state of the doubly linked list after adding a new node with value 5 at the head?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

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

int main() {
    Node* head = createNode(10);
    Node* second = createNode(20);
    head->next = second;
    second->prev = head;

    Node* newHead = createNode(5);
    newHead->next = head;
    head->prev = newHead;
    head = newHead;

    printList(head);
    return 0;
}
A5 <-> 10 <-> 20 -> NULL
B10 <-> 20 <-> 5 -> NULL
C5 -> 10 -> 20 -> NULL
D20 <-> 10 <-> 5 -> NULL
Attempts:
2 left
💡 Hint
Remember to update both prev and next pointers when adding at head.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Doubly Linked List Node Creation
What error will occur when running this code to create a doubly linked list node?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

Node* createNode(int data) {
    Node* newNode = malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

int main() {
    Node* node = createNode(10);
    printf("%d\n", node->data);
    free(node);
    return 0;
}
ANo error, prints 10
BCompilation error: missing cast in malloc
CRuntime error: segmentation fault
DCompilation error: missing #include <stdlib.h>
Attempts:
2 left
💡 Hint
Casting malloc is optional in C, and stdlib.h is included.
Predict Output
advanced
2:00remaining
Output After Removing a Node from Doubly Linked List
What is the printed state of the doubly linked list after removing the middle node (20) from the list?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

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

int main() {
    Node* head = createNode(10);
    Node* second = createNode(20);
    Node* third = createNode(30);

    head->next = second;
    second->prev = head;
    second->next = third;
    third->prev = second;

    // Remove second node
    head->next = third;
    third->prev = head;
    free(second);

    printList(head);
    return 0;
}
A20 <-> 30 -> NULL
B10 -> 30 -> NULL
C10 <-> 30 -> NULL
D10 <-> 20 <-> 30 -> NULL
Attempts:
2 left
💡 Hint
Check how the prev and next pointers are updated after removal.
🧠 Conceptual
expert
2:00remaining
Memory Management in Doubly Linked List Initialization
Which statement about memory management when creating and initializing a doubly linked list in C is TRUE?
AUsing malloc for nodes is optional because the compiler manages memory automatically.
BEach node must be allocated with malloc and freed when no longer needed to avoid memory leaks.
CFreeing the head node automatically frees all other nodes linked to it.
DNodes can be created as local variables inside functions without malloc to persist after function returns.
Attempts:
2 left
💡 Hint
Think about how dynamic memory works in C and what happens if you don't free allocated memory.