-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queue.c
66 lines (66 loc) · 1.26 KB
/
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
#include <stdio.h>
#include <stdlib.h>
typedef struct item
{
int value;
int priority;
} item;
int size = -1;
void enqueue(item *p, int value, int priority)
{
size++;
p[size].value = value;
p[size].priority = priority;
}
void sort(item *p)
{
int i, j, temp;
item c;
for (i = 1; i < size; i++)
{
temp = p[i].priority;
c = p[i];
j = i - 1;
while (j >= 0 && p[j].priority > temp)
{
p[j + 1] = p[j];
j--;
}
p[j + 1] = c;
}
}
int dequeue(item *p)
{
int temp = p[0].value;
sort(p);
for (int i = 1; i < size; i++)
{
p[i] = p[i + 1];
}
size--;
return temp;
}
void disp(item *p)
{
sort(p);
for (int i = 0; i <= size; i++)
{
printf("Element %d value is %d priority %d \n", i + 1, p[i].value, p[i].priority);
}
}
void main()
{
item *p;
int i, a, b, n;
printf("Enter the size of the queue\n");
scanf("%d", &n);
p = (item *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++)
{
printf("Enter the element and its priority\n");
scanf("%d%d", &a, &b);
enqueue(p, a, b);
}
printf("\n");
disp(p);
}