-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.main.c
38 lines (33 loc) · 1.05 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
#include "heap.h"
#include <stdio.h>
#define PRINT_ARRAY(array) print_array(array, sizeof(array) / sizeof((array)[0]))
void print_array(int* array, int count)
{
for(int i = 0; i < count; i++)
printf("%d ", array[i]);
if(count > 0)
puts("");
}
static int compare_ints(int* value, const int* compare_value)
{
if((*value) == (*compare_value))
return 0;
else if((*value) > *(compare_value))
return 1;
else
return -1;
}
int main(int argc, const char* argv[])
{
int array[12] = { -1, -3, 0, 2, 1, 7, 8, -9, 10, -11, 34, -56 };
heap_t* heap = heap_create(array, sizeof(int), 12, sizeof(array) / sizeof(int));
printf("Original: "); PRINT_ARRAY(array);
heap_build(heap, COMPARE_CALLBACK(compare_ints), NULL);
printf("After build heap: "); PRINT_ARRAY(array);
heap_sort(heap, COMPARE_CALLBACK(compare_ints), NULL);
printf("After heap sort: "); PRINT_ARRAY(array);
heap_remove(heap, &array[7], COMPARE_CALLBACK(compare_ints), NULL);
printf("After remove @[7]: "); PRINT_ARRAY(array);
heap_destroy(heap);
return 0;
}