#include "prio.h" #include "list.h" #include #include struct prio { list *table; int max_prio; int nb_elements; }; prio prio_create(int max_prio) { prio p = malloc(sizeof(struct prio)); p -> table = calloc(max_prio, sizeof(list)); p -> max_prio = max_prio; p -> nb_elements = 0; return p; } void prio_destroy(prioq q) { free(q -> table); free(q); } int prio_empty(prio p) { return p -> nb_elements == 0; } void prio_insert(prio p, void *element, int priority) { assert(priority >= 0 && priority <= p -> max_prio); p -> table[priority - 1] = cons(element, p -> table[priority - 1]); } void * prio_dequeue_max(prio p) { int i; assert(!prio_empty(p)); for(i = p -> max_prio - 1; !(p -> table[i]); i--) ; { list ltemp = p -> table[i]; void *temp = car(ltemp); p -> table[i] = cdr(p -> table[i]); free(ltemp); return temp; } }