#include "prioq.h" #include "list.h" #include "stdlib.h" struct prioq { int max_priority; list *table; int nb_elements; }; prioq prioq_create(int max_priority) { prioq q = malloc(sizeof(struct prioq)); int i; q -> max_priority = max_priority; q -> table = malloc(max_priority * sizeof(list)); q -> nb_elements = 0; for(i = 0; i < max_priority; i++) { q -> table[i] = NULL; } } void prioq_destroy(prioq q) { free(q -> table); free(q); } void prioq_insert(prioq q, void *element, int priority) { assert(priority > 0 && priority <= q -> max_priority); q -> table[priority - 1] = cons(element, q -> table[priority - 1]); } void * prioq_dequeue_max(prioq q) { int i; assert(q -> nb_elements > 0); for(i = q -> max_priority - 1; q -> table[i] == NULL; i--) ; { list ltemp = q -> table[i]; void *temp = car(ltemp); q -> table[i] = cdr(ltemp); free(ltemp); return temp; } } extern int prioq_empty(proiq q) { return q -> nb_elements == 0; }