diff --git a/Queue/Priority Queue/Priority Queue.c b/Queue/Priority Queue/Priority Queue.c new file mode 100644 index 00000000..b8d4e19c --- /dev/null +++ b/Queue/Priority Queue/Priority Queue.c @@ -0,0 +1,96 @@ +#include +#include + +#define MAX 100 // Maximum number of elements in the priority queue + +typedef struct { + int data[MAX]; + int size; +} PriorityQueue; + +// Function to swap elements in the heap +void swap(int *a, int *b) { + int temp = *a; + *a = *b; + *b = temp; +} + +// Heapify function for maintaining the min-heap property +void minHeapify(PriorityQueue *pq, int index) { + int smallest = index; + int left = 2 * index + 1; + int right = 2 * index + 2; + + if (left < pq->size && pq->data[left] < pq->data[smallest]) + smallest = left; + + if (right < pq->size && pq->data[right] < pq->data[smallest]) + smallest = right; + + if (smallest != index) { + swap(&pq->data[index], &pq->data[smallest]); + minHeapify(pq, smallest); + } +} + +// Insert element into the priority queue +void enqueue(PriorityQueue *pq, int value) { + if (pq->size >= MAX) { + printf("Priority queue overflow\n"); + return; + } + + // Insert the new element at the end + int i = pq->size++; + pq->data[i] = value; + + // Fix the min-heap property if it's violated + while (i != 0 && pq->data[(i - 1) / 2] > pq->data[i]) { + swap(&pq->data[i], &pq->data[(i - 1) / 2]); + i = (i - 1) / 2; + } +} + +// Remove element with highest priority (smallest element in min-heap) +int dequeue(PriorityQueue *pq) { + if (pq->size <= 0) { + printf("Priority queue underflow\n"); + return -1; + } + if (pq->size == 1) { + return pq->data[--pq->size]; + } + + // Store the minimum value, remove it, and fix the heap + int root = pq->data[0]; + pq->data[0] = pq->data[--pq->size]; + minHeapify(pq, 0); + + return root; +} + +// Peek at the highest-priority element +int peek(PriorityQueue *pq) { + if (pq->size <= 0) { + printf("Priority queue is empty\n"); + return -1; + } + return pq->data[0]; +} + +// Main function to demonstrate priority queue +int main() { + PriorityQueue pq; + pq.size = 0; + + enqueue(&pq, 10); + enqueue(&pq, 20); + enqueue(&pq, 5); + enqueue(&pq, 7); + + printf("Highest priority element: %d\n", peek(&pq)); + printf("Dequeued element: %d\n", dequeue(&pq)); + printf("Highest priority element after dequeue: %d\n", peek(&pq)); + + return 0; +} diff --git a/Queue/Priority Queue/Readme.md b/Queue/Priority Queue/Readme.md new file mode 100644 index 00000000..8e3891d9 --- /dev/null +++ b/Queue/Priority Queue/Readme.md @@ -0,0 +1,26 @@ +# Definition + +In C, a priority queue is a special type of queue where each element has a priority associated with it. Elements with higher priority are served before elements with lower priority. If two elements have the same priority, they are served based on their order in the queue (depending on whether it's a min-heap or max-heap). + +A priority queue can be implemented in various ways, but one of the most efficient implementations is using a binary heap: +1) Max-Heap: In a max-heap, the element with the highest priority (maximum value) is at the root of the heap, making it easy to access and remove. +2) Min-Heap: In a min-heap, the element with the lowest priority (minimum value) is at the root. + +# Operations in a Priority Queue +1) Insert (Enqueue): Insert an element into the priority queue while maintaining the heap property. +2) Remove (Dequeue): Remove the element with the highest priority (root element in a max-heap or min-heap). +3) Peek: Access the highest-priority element without removing it. + + + +# Explanation of Code +1) enqueue: Inserts a new element at the end of the heap array and then "bubbles up" to restore the min-heap property. +2) dequeue: Removes the root (smallest) element and replaces it with the last element in the array, then "bubbles down" to maintain the min-heap. +3) peek: Returns the highest-priority element without removing it. + +# Complexity Analysis +1) Insertion (enqueue): O(logn) because it may require "bubbling up." +2) Deletion (dequeue): O(logn) due to "bubbling down." +3) Peek: O(1) since the root is always the highest-priority element. + +This implementation offers a simple, efficient way to manage a priority queue in C using a min-heap.