Welcome to the exciting world of C Programming! Today, we'll be diving into a very important and widely used data structure known as the Priority Queue.
A priority queue is a collection of elements, each with its own unique priority. It maintains the property that the highest priority element is always at the front of the line. In C programming, we implement priority queues using Heaps.
A heap is a complete binary tree where either the parent nodes are greater than (max-heap) or less than (min-heap) their children. A complete binary tree is a tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}š” Pro Tip: In C programming, we will use min-heaps where the parent nodes have a smaller value than their children.
To build a priority queue, we follow these steps:
void insert(Node** root, int data) {
Node* newNode = createNode(data);
if (*root == NULL) {
*root = newNode;
return;
}
Node* current = *root;
Node* parent;
while (current != NULL) {
parent = current;
if (data < parent->data) {
if (parent->left == current) {
parent->left = current->left;
current->left = parent;
current = parent;
} else {
parent->right = current->left;
current->left = parent;
current = parent;
}
} else {
break;
}
}
if (parent == NULL) {
*root = newNode;
} else if (parent->left == NULL) {
parent->left = newNode;
} else {
parent->right = newNode;
}
}
int extractMin(Node** root) {
if (*root == NULL) {
return INT_MIN;
}
int min = (*root)->data;
Node* last = *root;
*root = (*root)->left;
last->left = NULL;
Node* current = *root;
Node* parent;
while (current != NULL && current->left != NULL) {
parent = current;
current = current->left;
if (current->right != NULL && current->right->data < current->data) {
parent->left = current->right;
current->right->left = parent;
current = current->right;
} else {
break;
}
}
if (parent == NULL) {
*root = current;
} else if (parent->left == current) {
parent->left = current->left;
current->left = NULL;
} else {
parent->right = current->left;
current->left = NULL;
}
if (current != NULL) {
current->right = last->right;
if (last->right != NULL) {
last->right->left = current;
}
current->left = last->left;
if (last->left != NULL) {
last->left->right = current;
}
current->data = min;
}
return min;
}Now that we've learned how to build and manipulate a priority queue, let's put it to use in a real-world scenario. Imagine a task scheduler where tasks are prioritized based on their deadlines.
#include <stdio.h>
#include <stdlib.h>
typedef struct Task {
int deadline;
char* description;
} Task;
Task createTask(int deadline, char* description) {
Task newTask;
newTask.deadline = deadline;
newTask.description = malloc(sizeof(char) * (strlen(description) + 1));
strcpy(newTask.description, description);
return newTask;
}
int main() {
Node* priorityQueue = NULL;
Task tasks[] = {
createTask(2, "Prepare for interview"),
createTask(4, "Complete homework"),
createTask(1, "Meeting with boss")
};
for (int i = 0; i < sizeof(tasks) / sizeof(tasks[0]); ++i) {
insert(&priorityQueue, tasks[i].deadline);
}
printf("Prioritized Tasks:\n");
while (priorityQueue != NULL) {
printf("%d: %s\n", extractMin(&priorityQueue).deadline, extractMin(&priorityQueue).description);
}
return 0;
}What is the data structure used to implement a priority queue in C programming?
Now that you've understood the concept of priority queues and have seen how to implement them in C, it's time to practice and master this powerful data structure. Happy coding! š