Skip to content

Commit 71d1e24

Browse files
authored
Merge pull request #1551 from sarthaxtic/main
Added Priority Queue
2 parents 8a58aae + fc49ec5 commit 71d1e24

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Queue/Priority Queue/Priority Queue.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

Queue/Priority Queue/Readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition
2+
3+
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).
4+
5+
A priority queue can be implemented in various ways, but one of the most efficient implementations is using a binary heap:
6+
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.
7+
2) Min-Heap: In a min-heap, the element with the lowest priority (minimum value) is at the root.
8+
9+
# Operations in a Priority Queue
10+
1) Insert (Enqueue): Insert an element into the priority queue while maintaining the heap property.
11+
2) Remove (Dequeue): Remove the element with the highest priority (root element in a max-heap or min-heap).
12+
3) Peek: Access the highest-priority element without removing it.
13+
14+
15+
16+
# Explanation of Code
17+
1) enqueue: Inserts a new element at the end of the heap array and then "bubbles up" to restore the min-heap property.
18+
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.
19+
3) peek: Returns the highest-priority element without removing it.
20+
21+
# Complexity Analysis
22+
1) Insertion (enqueue): O(logn) because it may require "bubbling up."
23+
2) Deletion (dequeue): O(logn) due to "bubbling down."
24+
3) Peek: O(1) since the root is always the highest-priority element.
25+
26+
This implementation offers a simple, efficient way to manage a priority queue in C using a min-heap.

0 commit comments

Comments
 (0)