-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.c
More file actions
116 lines (100 loc) · 2.4 KB
/
priority_queue.c
File metadata and controls
116 lines (100 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*Implement a Priority Queue using arrays with the operations:
a.Insert elements into the queue
b.Delete elements from the queue
c.Display the contents of the queue after each operation.*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int heap[MAX];
int size = 0;
// Swap function
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
// Heapify up (for insert)
void heapifyUp(int i) {
while (i > 1 && heap[i / 2] < heap[i]) {
swap(&heap[i], &heap[i / 2]);
i = i / 2;
}
}
// Heapify down (for delete)
void heapifyDown(int i) {
int largest = i;
int left = 2 * i;
int right = (2 * i) + 1;
if (left <= size && heap[left] > heap[largest])
largest = left;
if (right <= size && heap[right] > heap[largest])
largest = right;
if (largest != i) {
swap(&heap[i], &heap[largest]);
heapifyDown(largest);
}
}
// Insert element into priority queue
void insert(int value) {
if (size == MAX - 1) {
printf("Priority Queue is full!\n");
return;
}
size++;
heap[size] = value;
heapifyUp(size);
printf("Inserted %d into the queue.\n", value);
}
// Delete max (highest priority element)
void deleteMax() {
if (size == 0) {
printf("Priority Queue is empty!\n");
return;
}
printf("Deleted element: %d\n", heap[1]);
heap[1] = heap[size];
size--;
heapifyDown(1);
}
// Display contents of the queue
void display() {
if (size == 0) {
printf("Priority Queue is empty!\n");
return;
}
printf("Priority Queue: ");
for (int i = 1; i <= size; i++) {
printf("%d ", heap[i]);
}
printf("\n");
}
// Driver program
int main() {
int choice, value;
while (1) {
printf("\nPriority Queue Operations (Max Heap):\n");
printf("1. Insert\n2. Delete Max\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
display();
break;
case 2:
deleteMax();
display();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}