Question: Consider the structure struct pq{ struct dynarray* heap; } struct pq_elem{ int priority; void* item; }; and assuming you have the following function, void dynarray_free(struct
Consider the structure
struct pq{
struct dynarray* heap;
}
struct pq_elem{
int priority;
void* item;
};
and assuming you have the following function,
void dynarray_free(struct dynarray* da) ,
int dynarray_size(struct dynarray* da),
void* dynarray_get(struct dynarray* da, int idx),
void dynarray_set(struct dynarray* da, int idx, void* val),
void _dynarray_resize(struct dynarray* da, int new_capacity),
void dynarray_insert(struct dynarray* da, int idx, void* val),
void dynarray_remove(struct dynarray* da, int idx)
struct pq* pq_create() This function allocates and initializes a new priority queue.
void pq_free(struct pq* pq) This function frees the memory associated with a priority queue.
int pq_isempty(struct pq* pq) This function returns 1 if the given priority queue is empty or 0 otherwise
Complete the function,
void pq_insert(struct pq* pq, void* item, int priority) {
| assert(pq); | |
| /* | |
| * Allocate space for the new element to be placed into the priority queue | |
| * and initialize it with the provided values. | |
| */ | |
| struct pq_elem* new_elem = malloc(sizeof(struct pq_elem)); | |
| new_elem->item = item; | |
| new_elem->priority = priority; | |
| /* | |
| * Figure out where in the heap array to insert the new element represented | |
| * by new_elem and insert it. | |
| */ | |
| /* | |
| * Restore the heap so that it has the property that every node's priority | |
| * value is less than the priority values of its children. This can be | |
| * done by "percolating" the newly added element up in the heap array. To | |
| * perform the percolation, you will have to compare the priority values of | |
| * the struct pq_elems in the heap array (i.e. by comparing the | |
| * elem->priority values). | |
| */ } |
void* pq_first(struct pq* pq) {
| assert(pq); | |
| assert(dynarray_size(pq->heap) > 0); | |
| struct pq_elem* first_elem = NULL; | |
| /* | |
| * Determine what index in the heap array corresponds to the highest-priority | |
| * element (i.e. the one with the lowest priority value), and store the | |
| * value there in first_elem. | |
| */ | |
| /* | |
| * Return the extracted item, if the element taken out of the priority | |
| * queue is not NULL. | |
| */ | |
| if (first_elem != NULL) { | |
| return first_elem->item; | |
| } else { | |
| return NULL; | |
| } | |
| } |
void* pq_remove_first(struct pq* pq) {
| assert(pq); | |
| assert(dynarray_size(pq->heap) > 0); | |
| struct pq_elem* first_elem = NULL; | |
| /* | |
| * Determine what index in the heap array corresponds to the highest-priority | |
| * element (i.e. the one with the lowest priority value), and store the | |
| * value there in first_elem. | |
| */ | |
| /* | |
| * Replace the highest-priority element with the appropriate one from within | |
| * the heap array. Remove that replacement element from the array after | |
| * copying its value to the location of the old highest-priority element.. | |
| */ | |
| /* | |
| * Restore the heap so that it has the property that every node's priority | |
| * value is less than the priority values of its children. This can be | |
| * done by "percolating" the element that replaced the highest-priority | |
| * element down in the heap array. To perform the percolation, you will | |
| * have to compare the priority values of the struct pq_elems in the heap | |
| * array (i.e. by comparing the elem->priority values). It may be helpful | |
| * to write a helper function to accomplish this percolation down. | |
| */ | |
| /* | |
| * Return the extracted item, if the element taken out of the priority | |
| * queue is not NULL. | |
| */ | |
| if (first_elem != NULL) { | |
| void* item = first_elem->item; | |
| free(first_elem); | |
| return item; | |
| } else { | |
| return NULL; | |
| } | |
| } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
