Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a priority queue node structure with an integer priority.
DSA C
typedef struct Node {
int data;
int [1];
struct Node* next;
} Node; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic name like 'value' instead of 'priority'.
Confusing priority with data.
✗ Incorrect
The field 'priority' stores the priority value for each node in the priority queue.
2fill in blank
mediumComplete the code to insert a new node into the priority queue based on priority order (higher priority first).
DSA C
void insert(Node** head, int data, int priority) {
Node* newNode = malloc(sizeof(Node));
newNode->data = data;
newNode->priority = priority;
newNode->next = NULL;
if (*head == NULL || (*head)->priority < [1]) {
newNode->next = *head;
*head = newNode;
} else {
Node* temp = *head;
while (temp->next != NULL && temp->next->priority >= priority) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 'data' instead of 'priority'.
Using 'priority' instead of 'newNode->priority' in the condition.
✗ Incorrect
We compare the priority of the new node with the head node's priority to decide insertion at the front.
3fill in blank
hardFix the error in the dequeue function that removes the highest priority node from the queue.
DSA C
int dequeue(Node** head) {
if (*head == NULL) {
return -1; // Queue empty
}
Node* temp = *head;
int data = temp->data;
*head = [1];
free(temp);
return data;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head->next' which is invalid because head is a double pointer.
Using '*head->next' which is parsed incorrectly.
✗ Incorrect
To remove the head node, we set *head to the next node using (*head)->next.
4fill in blank
hardFill both blanks to complete the function that prints the priority queue nodes in order.
DSA C
void printQueue(Node* head) {
Node* current = head;
while (current != NULL) {
printf("Data: %d, Priority: %d\n", current->[1], current->[2]);
current = current->next;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' or 'value' instead of 'data' or 'priority'.
Mixing up the order of fields.
✗ Incorrect
We print the 'data' and 'priority' fields of each node.
5fill in blank
hardFill all three blanks to complete the function that checks if the priority queue is empty.
DSA C
int isEmpty(Node* [1]) { return [2] == [3]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable like 'temp'.
Comparing to 0 instead of NULL.
✗ Incorrect
We check if the pointer 'head' is equal to NULL to determine if the queue is empty.
