-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue.c
104 lines (85 loc) · 2.21 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
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
#include "priority_queue.h"
#include "pacman.h"
void heap_init(struct heap* h)
{
h->count = 0;
h->size = initial_size;
h->heaparr = (node_t **) malloc(sizeof(node_t*) * initial_size);
for(int i = 0; i < initial_size; i++)
h->heaparr[i]=NULL;
if(!h->heaparr) {
printf("Error allocatinga memory...\n");
exit(-1);
}
}
void max_heapify(node_t** data, int loc, int count) {
int left, right, largest;
node_t* temp;
left = 2*(loc) + 1;
right = left + 1;
largest = loc;
if (left <= count && data[left]->priority > data[largest]->priority) {
largest = left;
}
if (right <= count && data[right]->priority > data[largest]->priority) {
largest = right;
}
if(largest != loc) {
temp = data[loc];
data[loc] = data[largest];
data[largest] = temp;
max_heapify(data, largest, count);
}
}
void heap_push(struct heap* h, node_t* value)
{
int index, parent;
// Resize the heap if it is too small to hold all the data
if (h->count == h->size)
{
h->size += 1;
h->heaparr = realloc(h->heaparr, sizeof(node_t) * h->size);
if (!h->heaparr) exit(-1); // Exit if the memory allocation fails
}
index = h->count++; // First insert at last of array
// Find out where to put the element and put it
for(;index; index = parent)
{
parent = (index - 1) / 2;
if (h->heaparr[parent]->priority >= value->priority) break;
h->heaparr[index] = h->heaparr[parent];
}
h->heaparr[index] = value;
}
void heap_display(struct heap* h) {
int i;
for(i=0; i<h->count; ++i) {
node_t* n = h->heaparr[i];
printf("priority = %d", n->priority);
printf("\n");
DrawWindowState( n->state );
}
}
node_t* heap_delete(struct heap* h)
{
node_t* removed;
node_t* temp = h->heaparr[--h->count];
if ((h->count <= (h->size + 2)) && (h->size > initial_size))
{
h->size -= 1;
h->heaparr = realloc(h->heaparr, sizeof(node_t) * h->size);
if (!h->heaparr) exit(-1); // Exit if the memory allocation fails
}
removed = h->heaparr[0];
h->heaparr[0] = temp;
if(temp == removed) h->heaparr[0] = NULL;
max_heapify(h->heaparr, 0, h->count);
return removed;
}
void emptyPQ(struct heap* pq) {
while(pq->count != 0) {
node_t* n = heap_delete(pq);
free(n);
//printf("<<%d", heap_delete(pq));
}
}