-
Notifications
You must be signed in to change notification settings - Fork 1
/
8_priority_queue.c
95 lines (91 loc) · 1.81 KB
/
8_priority_queue.c
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
#include<stdio.h>
struct element{
int priority;
char name[1000];
};
void swap(struct element *x,struct element *y)
{
struct element t=*x;
*x=*y;
*y=t;
}
void max_heapify(struct element a[],int i,int n)
{
int l=2*i,r=2*i+1,largest=i;
if(l<=n && a[l].priority>a[largest].priority)
largest=l;
if(r<=n && a[r].priority>a[largest].priority)
largest=r;
if(largest!=i)
{
swap(a+i,a+largest);
max_heapify(a,largest,n);
}
}
void build_max_heap(struct element a[],int n)
{
int i;
for(i=n/2;i>=1;i--)
max_heapify(a,i,n);
}
void insert_with_priority(struct element a[],int n,struct element temp)
{
int i=n;
a[n]=temp;
while(i>1&&a[i/2].priority<a[i].priority)
{
swap(a+i/2,a+i);
i=i/2;
}
}
int main()
{
int i,n;
struct element a[1010];
printf(" Enter initial Number of elements of queue : ");
scanf("%d",&n);
printf(" Enter n elements ( priority followed by name ) : \n");
for(i=1;i<=n;i++)
{
scanf("%d %s",&(a[i].priority),a[i].name);
}
build_max_heap(a,n);
printf(" Constructed Priority Queue : \n");
for(i=1;i<=n;i++)
{
printf("%d %s \n",a[i].priority,a[i].name);
}
int option=0;
while(1){
printf(" Options :\n\t1. InsertWithPriority\n\t2. RemoveTop\n\t3. Display Queue\n\t4. Exit\n Enter Option : ");
scanf("%d",&option);
if(option==1)
{
n++;
printf(" Enter priority and name : ");
struct element temp;
scanf("%d %s",&(temp.priority),temp.name);
insert_with_priority(a,n,temp);
//build_max_heap(a,n);
printf(" Inserted to queue \n");
}
else if(option==2)
{
printf(" Removed element : %d %s\n",a[1].priority,a[1].name);
a[1].priority=-1;
swap(a+1,a+n);
n--;
max_heapify(a,1,n);
}
else if(option==3)
{
printf(" Current Priority Queue : \n");
for(i=1;i<=n;i++)
{
printf("%d %s \n",a[i].priority,a[i].name);
}
}
else
break;
}
}