Priority queue: Difference between revisions
Content added Content deleted
m (Fixed indentation issues) |
|||
Line 1,631: | Line 1,631: | ||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include "pairheap.h" |
#include "pairheap.h" |
||
/* --------------------------------------------------------------------------- |
|||
* Pairing heap implementation |
|||
* --------------------------------------------------------------------------- */ |
|||
static heap_t add_child(heap_t h, heap_t g) { |
static heap_t add_child(heap_t h, heap_t g) { |
||
if (h->down != NULL) |
if (h->down != NULL) |
||
g->next = h->down; |
|||
h->down = g; |
h->down = g; |
||
} |
} |
||
Line 1,642: | Line 1,646: | ||
if (b == NULL) return a; |
if (b == NULL) return a; |
||
if (a->key < b->key) { |
if (a->key < b->key) { |
||
add_child(a, b); |
|||
return a; |
|||
} else { |
} else { |
||
add_child(b, a); |
|||
return b; |
|||
} |
} |
||
} |
} |
||
Line 1,655: | Line 1,659: | ||
heap_t two_pass_merge(heap_t h) { |
heap_t two_pass_merge(heap_t h) { |
||
if (h == NULL || h->next == NULL) |
if (h == NULL || h->next == NULL) |
||
return h; |
|||
else { |
else { |
||
pq_node_t |
|||
*a = h, |
|||
*b = h->next, |
|||
*rest = b->next; |
|||
a->next = b->next = NULL; |
|||
return heap_merge(heap_merge(a, b), two_pass_merge(rest)); |
|||
} |
} |
||
} |
} |
||
Line 1,702: | Line 1,706: | ||
while (heap != NULL) { |
while (heap != NULL) { |
||
struct task *top = (struct task *) heap; |
|||
printf("%s\n", top->task); |
|||
heap = heap_pop(heap); |
|||
free(top); |
|||
} |
} |
||
} |
} |