-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheap_push.c
38 lines (33 loc) · 1.26 KB
/
heap_push.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heap_push.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/10 20:43:16 by mihykim #+# #+# */
/* Updated: 2020/04/10 21:56:01 by mihykim ### ########.fr */
/* */
/* ************************************************************************** */
#include "heap.h"
void swap(void **a, void **b)
{
void *tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int heap_push(t_heap *heap, void *data)
{
int i;
if (heap == NULL || heap->max_size <= heap->size)
return (FAIL);
i = ++heap->size;
heap->data[i] = data;
while (i/2 && heap->cmp(heap->data[i/2], heap->data[i]) > 0)
{
swap(&heap->data[i/2], &heap->data[i]);
i = i/2;
}
return (SUCCESS);
}