Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using generic names like 'pointer' or 'link' which are uncommon.
✗ Incorrect
The pointer to the next node in a singly linked list is commonly named next.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'node' which is undefined.
Using 'struct Node' inside sizeof without typedef.
✗ Incorrect
We allocate memory for the Node structure using sizeof(Node).
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using undefined pointer names like 'link' or 'pointer'.
✗ Incorrect
To traverse the list, we follow the next pointer until it is NULL.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The new node's next points to the current head, then head is updated to the new node.
5fill in blank
hardFill 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'
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'.
✗ Incorrect
We loop until current is NULL, print current's data, then move to current->next.
