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(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = [1];
temp->next = NULL;
return temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the data value.
Using the wrong variable name.
✗ Incorrect
The new node's data field should be set to the input parameter 'data'.
2fill in blank
mediumComplete the code to check if the queue is empty.
DSA C
int isEmpty(struct Queue* q) {
return (q->front == [1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing front to rear instead of NULL.
Using 0 instead of NULL in pointer comparison.
✗ Incorrect
The queue is empty if the front pointer is NULL.
3fill in blank
hardFix the error in the enqueue function to correctly update the rear pointer.
DSA C
void enqueue(struct Queue* q, int data) {
struct Node* temp = newNode(data);
if (q->rear == NULL) {
q->front = temp;
q->rear = [1];
return;
}
q->rear->next = temp;
q->rear = temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning q->front or NULL to q->rear.
Not updating q->rear at all.
✗ Incorrect
When the queue is empty, both front and rear should point to the new node 'temp'.
4fill in blank
hardFill both blanks to correctly dequeue an element and update the queue pointers.
DSA C
int dequeue(struct Queue* q) {
if (isEmpty(q))
return -1;
struct Node* temp = q->front;
int data = temp->data;
q->front = [1];
if (q->front == NULL)
q->rear = [2];
free(temp);
return data;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using q->front->next before updating front pointer.
Not setting rear to NULL when queue becomes empty.
✗ Incorrect
After removing the front node, front should point to the next node (temp->next). If front becomes NULL, rear should also be set to NULL.
5fill in blank
hardFill all three blanks to print all elements in the queue from front to rear.
DSA C
void displayQueue(struct Queue* q) {
struct Node* temp = [1];
while (temp != [2]) {
printf("%d -> ", [3]);
temp = temp->next;
}
printf("NULL\n");
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from rear instead of front.
Looping until temp equals rear instead of NULL.
Printing temp instead of temp->data.
✗ Incorrect
Start from the front node, loop until temp is NULL, and print temp->data at each step.
