Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given data.
DSA C
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or next pointer to data field.
Using an undefined variable instead of data.
✗ Incorrect
We assign the given data value to the new node's data field.
2fill in blank
mediumComplete the code to link the new node to the current head of the list.
DSA C
newNode->next = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to NULL which breaks the list.
Setting next to newNode itself causing a cycle.
✗ Incorrect
The new node's next pointer should point to the current head node.
3fill in blank
hardFix the error in updating the head pointer to the new node.
DSA C
*head_ref = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning data or NULL to head_ref instead of newNode.
Assigning head which is old pointer, not new node.
✗ Incorrect
The head pointer should be updated to point to the new node after insertion.
4fill in blank
hardFill both blanks to complete the push function that inserts a new node at the beginning.
DSA C
void push(struct Node** head_ref, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
newNode->next = [2];
*head_ref = newNode;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using head_ref instead of *head_ref for next pointer.
Assigning NULL to next which loses the rest of the list.
✗ Incorrect
Assign data to newNode->data and link newNode->next to the current head *head_ref.
5fill in blank
hardFill all three blanks to complete the push function with memory allocation, data assignment, and linking.
DSA C
void push(struct Node** head_ref, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof([1]));
newNode->data = [2];
newNode->next = [3];
*head_ref = newNode;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Node' without struct keyword in sizeof.
Assigning wrong variable to data or next pointers.
✗ Incorrect
Allocate memory for struct Node, assign data to newNode->data, and link newNode->next to *head_ref.
