Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a doubly linked list in data structures?
easy
A. To allow traversal in both forward and backward directions
B. To store data in a fixed-size array
C. To sort data automatically
D. To store data only in one direction

Solution

  1. Step 1: Understand doubly linked list structure

    A doubly linked list has nodes with pointers to both next and previous nodes.
  2. Step 2: Identify traversal capability

    This structure allows moving forward and backward through the list easily.
  3. Final Answer:

    To allow traversal in both forward and backward directions -> Option A
  4. Quick Check:

    Doubly linked list = bidirectional traversal [OK]
Hint: Remember: doubly means two directions [OK]
Common Mistakes:
  • Confusing doubly linked list with singly linked list
  • Thinking it sorts data automatically
  • Assuming it uses arrays internally
2. Which of the following is the correct way to declare a node structure for a doubly linked list in C?
easy
A. struct Node { int data; struct Node* next; };
B. struct Node { int data; struct Node* prev; struct Node* next; };
C. struct Node { int data; int next; int prev; };
D. struct Node { int data; struct Node next; struct Node prev; };

Solution

  1. Step 1: Check node structure for doubly linked list

    Each node must have data, a pointer to next node, and a pointer to previous node.
  2. Step 2: Identify correct syntax

    struct Node { int data; struct Node* prev; struct Node* next; }; correctly declares pointers to next and prev nodes using struct Node*.
  3. Final Answer:

    struct Node { int data; struct Node* prev; struct Node* next; }; -> Option B
  4. Quick Check:

    Node has data + prev pointer + next pointer [OK]
Hint: Pointers to prev and next must be struct Node* [OK]
Common Mistakes:
  • Using int instead of pointer for next/prev
  • Missing prev pointer in node
  • Declaring next/prev as struct Node instead of pointer
3. What will be the output of the following C code snippet that creates a doubly linked list with two nodes and prints their data?
typedef struct Node {
  int data;
  struct Node* prev;
  struct Node* next;
} Node;

int main() {
  Node* head = (Node*)malloc(sizeof(Node));
  Node* second = (Node*)malloc(sizeof(Node));

  head->data = 10;
  head->prev = NULL;
  head->next = second;

  second->data = 20;
  second->prev = head;
  second->next = NULL;

  printf("%d -> %d -> NULL", head->data, second->data);
  return 0;
}
medium
A. Compilation error
B. 20 -> 10 -> NULL
C. 10 -> 20 -> NULL
D. 10 -> NULL

Solution

  1. Step 1: Analyze node creation and linking

    Two nodes are created: head with data 10, second with data 20. head->next points to second, second->prev points to head.
  2. Step 2: Understand print statement

    Prints head->data (10), then second->data (20), then "-> NULL".
  3. Final Answer:

    10 -> 20 -> NULL -> Option C
  4. Quick Check:

    Prints data in order: 10 then 20 [OK]
Hint: Follow pointers to print nodes in order [OK]
Common Mistakes:
  • Confusing order of nodes
  • Forgetting to set next or prev pointers
  • Assuming print shows backward traversal
4. Identify the error in the following code snippet that attempts to initialize a doubly linked list node:
struct Node {
  int data;
  struct Node* prev;
  struct Node* next;
};

int main() {
  struct Node* node = malloc(sizeof(struct Node));
  node->data = 5;
  node->prev = NULL;
  node->next = NULL;
  free(node);
  printf("%d", node->data);
  return 0;
}
medium
A. Accessing freed memory after free(node)
B. Missing #include
C. Incorrect struct declaration
D. No error, code runs fine

Solution

  1. Step 1: Review memory allocation and freeing

    Memory is allocated for node, initialized, then freed with free(node).
  2. Step 2: Identify invalid access

    After freeing, code tries to print node->data, which accesses freed memory causing undefined behavior.
  3. Final Answer:

    Accessing freed memory after free(node) -> Option A
  4. Quick Check:

    Using pointer after free = error [OK]
Hint: Never use pointer after free() [OK]
Common Mistakes:
  • Ignoring use-after-free errors
  • Forgetting to include stdlib.h (but not main error here)
  • Assuming free deletes pointer variable
5. You want to create a doubly linked list with nodes containing values 1, 2, and 3 in that order. Which code snippet correctly initializes this list with proper prev and next pointers?
hard
A. Node* n1 = malloc(sizeof(Node)); n1->data=1; n1->prev=NULL; n1->next=NULL; Node* n2 = malloc(sizeof(Node)); n2->data=2; n2->prev=NULL; n2->next=NULL; n1->next=n2; Node* n3 = malloc(sizeof(Node)); n3->data=3; n3->prev=NULL; n3->next=NULL; n2->next=n3;
B. Node* n1 = malloc(sizeof(Node)); n1->data=1; n1->prev=NULL; n1->next=NULL; Node* n2 = malloc(sizeof(Node)); n2->data=2; n2->prev=n1; n2->next=NULL; n1->next=n2; Node* n3 = malloc(sizeof(Node)); n3->data=3; n3->prev=n2; n3->next=n1; n2->next=n3;
C. Node* n1 = malloc(sizeof(Node)); n1->data=1; n1->prev=NULL; n1->next=NULL; Node* n2 = malloc(sizeof(Node)); n2->data=2; n2->prev=n1; n2->next=NULL; n1->next=n2; Node* n3 = malloc(sizeof(Node)); n3->data=3; n3->prev=NULL; n3->next=NULL; n2->next=n3;
D. Node* n1 = malloc(sizeof(Node)); n1->data=1; n1->prev=NULL; n1->next=NULL; Node* n2 = malloc(sizeof(Node)); n2->data=2; n2->prev=n1; n2->next=NULL; n1->next=n2; Node* n3 = malloc(sizeof(Node)); n3->data=3; n3->prev=n2; n3->next=NULL; n2->next=n3;

Solution

  1. Step 1: Check initialization of first node

    n1 has data=1, prev=NULL, next=NULL initially.
  2. Step 2: Link second node correctly

    n2 has data=2, prev=n1, next=NULL; n1->next points to n2.
  3. Step 3: Link third node correctly

    n3 has data=3, prev=n2, next=NULL; n2->next points to n3.
  4. Final Answer:

    Code snippet B correctly sets prev and next pointers for all nodes -> Option D
  5. Quick Check:

    Each node's prev and next link neighbors properly [OK]
Hint: Prev of first is NULL; next of last is NULL [OK]
Common Mistakes:
  • Not setting prev pointers for nodes after first
  • Setting next pointers incorrectly
  • Making circular links unintentionally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard