-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_main.c
88 lines (73 loc) · 1.77 KB
/
heap_main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "src/ed/heap.h"
typedef struct
{
int x, y;
float g, h;
} Celula;
Celula *celula_create(int x, int y)
{
Celula *c = malloc(sizeof(Celula));
c->x = x;
c->y = y;
return c;
}
void celula_destroy(Celula *c)
{
free(c);
}
void hash_key_destroy(void *a) {
Celula *c = (Celula *)a;
celula_destroy(c);
}
int celula_hash(HashTable *h, void *key)
{
Celula *c = (Celula *)key;
// 83 e 97 sao primos e o operador "^" é o XOR bit a bit
return ((c->x * 83) ^ (c->y * 97)) % hash_table_size(h);
}
int celula_cmp(void *c1, void *c2)
{
HashTableItem *d1 = (HashTableItem *)c1;
Celula *a = (Celula *)d1->key;
Celula *b = (Celula *)c2;
if (a->x == b->x && a->y == b->y) {
return 0;
}
else
return 1;
}
int main()
{
int i, n, x, y, priority;
char cmd[10];
HashTable *h = hash_table_construct(19, celula_hash, celula_cmp, free, free);
Heap *heap = heap_construct(h);
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("\n%s", cmd);
if (!strcmp(cmd, "PUSH"))
{
scanf("%d %d %d", &x, &y, &priority);
Celula *cel = celula_create(x, y);
cel = heap_push(heap, cel, priority);
// se a celula ja existia, lembre-se liberar a memoria alocada para a nova celula
if (cel)
celula_destroy(cel);
}
else if (!strcmp(cmd, "POP"))
{
int priority = heap_min_priority(heap);
Celula *cel = heap_pop(heap);
printf("%d %d %d\n", cel->x, cel->y, priority);
celula_destroy(cel);
}
//printf("%d\n", i);
}
hash_table_destroy(h);
heap_destroy(heap);
return 0;
}