|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#define MAX 100 // Maximum number of elements in the priority queue |
| 5 | + |
| 6 | +typedef struct { |
| 7 | + int data[MAX]; |
| 8 | + int size; |
| 9 | +} PriorityQueue; |
| 10 | + |
| 11 | +// Function to swap elements in the heap |
| 12 | +void swap(int *a, int *b) { |
| 13 | + int temp = *a; |
| 14 | + *a = *b; |
| 15 | + *b = temp; |
| 16 | +} |
| 17 | + |
| 18 | +// Heapify function for maintaining the min-heap property |
| 19 | +void minHeapify(PriorityQueue *pq, int index) { |
| 20 | + int smallest = index; |
| 21 | + int left = 2 * index + 1; |
| 22 | + int right = 2 * index + 2; |
| 23 | + |
| 24 | + if (left < pq->size && pq->data[left] < pq->data[smallest]) |
| 25 | + smallest = left; |
| 26 | + |
| 27 | + if (right < pq->size && pq->data[right] < pq->data[smallest]) |
| 28 | + smallest = right; |
| 29 | + |
| 30 | + if (smallest != index) { |
| 31 | + swap(&pq->data[index], &pq->data[smallest]); |
| 32 | + minHeapify(pq, smallest); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Insert element into the priority queue |
| 37 | +void enqueue(PriorityQueue *pq, int value) { |
| 38 | + if (pq->size >= MAX) { |
| 39 | + printf("Priority queue overflow\n"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Insert the new element at the end |
| 44 | + int i = pq->size++; |
| 45 | + pq->data[i] = value; |
| 46 | + |
| 47 | + // Fix the min-heap property if it's violated |
| 48 | + while (i != 0 && pq->data[(i - 1) / 2] > pq->data[i]) { |
| 49 | + swap(&pq->data[i], &pq->data[(i - 1) / 2]); |
| 50 | + i = (i - 1) / 2; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Remove element with highest priority (smallest element in min-heap) |
| 55 | +int dequeue(PriorityQueue *pq) { |
| 56 | + if (pq->size <= 0) { |
| 57 | + printf("Priority queue underflow\n"); |
| 58 | + return -1; |
| 59 | + } |
| 60 | + if (pq->size == 1) { |
| 61 | + return pq->data[--pq->size]; |
| 62 | + } |
| 63 | + |
| 64 | + // Store the minimum value, remove it, and fix the heap |
| 65 | + int root = pq->data[0]; |
| 66 | + pq->data[0] = pq->data[--pq->size]; |
| 67 | + minHeapify(pq, 0); |
| 68 | + |
| 69 | + return root; |
| 70 | +} |
| 71 | + |
| 72 | +// Peek at the highest-priority element |
| 73 | +int peek(PriorityQueue *pq) { |
| 74 | + if (pq->size <= 0) { |
| 75 | + printf("Priority queue is empty\n"); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + return pq->data[0]; |
| 79 | +} |
| 80 | + |
| 81 | +// Main function to demonstrate priority queue |
| 82 | +int main() { |
| 83 | + PriorityQueue pq; |
| 84 | + pq.size = 0; |
| 85 | + |
| 86 | + enqueue(&pq, 10); |
| 87 | + enqueue(&pq, 20); |
| 88 | + enqueue(&pq, 5); |
| 89 | + enqueue(&pq, 7); |
| 90 | + |
| 91 | + printf("Highest priority element: %d\n", peek(&pq)); |
| 92 | + printf("Dequeued element: %d\n", dequeue(&pq)); |
| 93 | + printf("Highest priority element after dequeue: %d\n", peek(&pq)); |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments