Bird
0
0
DSA Cprogramming~10 mins

Priority Queue Introduction and Concept in DSA C - Interactive Practice

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

Complete 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'
Apriority
Bvalue
Cindex
Dweight
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic name like 'value' instead of 'priority'.
Confusing priority with data.
2fill in blank
medium

Complete 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'
AnewNode->priority
Bdata
CnewNode->data
Dpriority
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 'data' instead of 'priority'.
Using 'priority' instead of 'newNode->priority' in the condition.
3fill in blank
hard

Fix 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'
A*head->next
Btemp->next
Chead->next
D(*head)->next
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.
4fill in blank
hard

Fill 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'
Adata
Bnext
Cpriority
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' or 'value' instead of 'data' or 'priority'.
Mixing up the order of fields.
5fill in blank
hard

Fill 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'
Ahead
CNULL
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable like 'temp'.
Comparing to 0 instead of NULL.