Bird
0
0
DSA Cprogramming~10 mins

Creating a Singly Linked List from Scratch in DSA C - Code Step by Step

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the node structure for a singly linked list.

DSA C
typedef struct Node {
    int data;
    struct Node* [1];
} Node;
Drag options to blanks, or click blank then click option'
Apointer
Bprev
Clink
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using generic names like 'pointer' or 'link' which are uncommon.
2fill in blank
medium

Complete the code to create a new node dynamically.

DSA C
Node* createNode(int value) {
    Node* newNode = (Node*)malloc(sizeof([1]));
    if (newNode == NULL) return NULL;
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}
Drag options to blanks, or click blank then click option'
ANode
Bnode
Cstruct Node
DNode*
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'node' which is undefined.
Using 'struct Node' inside sizeof without typedef.
3fill in blank
hard

Fix the error in the function to append a node at the end of the list.

DSA C
void appendNode(Node** head, int value) {
    Node* newNode = createNode(value);
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->[1] != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
}
Drag options to blanks, or click blank then click option'
Anext
Bprev
Clink
Dpointer
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using undefined pointer names like 'link' or 'pointer'.
4fill in blank
hard

Fill both blanks to insert a node at the beginning of the list.

DSA C
void insertAtHead(Node** head, int value) {
    Node* newNode = createNode(value);
    newNode->[1] = *head;
    *head = [2];
}
Drag options to blanks, or click blank then click option'
Anext
Bhead
CnewNode
Dprev
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not used in singly linked lists.
Assigning head to newNode->next instead of the other way.
5fill in blank
hard

Fill all three blanks to print all elements of the singly linked list.

DSA C
void printList(Node* head) {
    Node* current = head;
    while (current != [1]) {
        printf("%d -> ", current->[2]);
        current = current->[3];
    }
    printf("NULL\n");
}
Drag options to blanks, or click blank then click option'
ANULL
Bdata
Cnext
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of NULL in the loop condition.
Printing 'next' instead of 'data'.
Moving current to 'data' instead of 'next'.