Programming Problem in C: Your task for this assignment is to implement a priority queue. You must build your PQ using a

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Programming Problem in C: Your task for this assignment is to implement a priority queue. You must build your PQ using a

Post by answerhappygod »

Programming Problem in C:
Your task for this assignment is to implement a priority
queue. You must build your PQ using a binary heap.
The interface for the PQ you'll implement (i.e. the structures
and the prototypes of functions a user of the PQ interacts with) is
already defined for you in the file pq.h. Your job is to implement
definitions for the functions that comprise this interface in
pq.c.
Note that you may not modify the interface definition with which
you are provided. Specifically, do not modify any of the
already-defined PQ function prototypes.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//pq.c
/*
* In this file, you will write the structures and
functions needed to
* implement a priority queue. Feel free to implement
any helper functions
* you need in this file to implement a priority queue.
Make sure to add your
* name and @oregonstate.edu email address below:
*
* Name:
* Email:
*/
#include <stdlib.h>
#include "pq.h"
#include "dynarray.h"
/* This is the structure that represents a priority queue.
You must define
* this struct to contain the data needed to implement a
priority queue.
*/
struct pq;
/* This function should allocate and initialize an empty
priority queue and
* return a pointer to it.
*/
struct pq* pq_create() {
// FIXME:
return NULL;
}
/*
* This function should free the memory allocated to a
given priority queue.
* Note that this function SHOULD NOT free the individual
elements stored in
* the priority queue. That is the responsibility of
the caller.
*
* Params: pq - the priority queue to be destroyed.
May not be NULL.
*/
void pq_free(struct pq* pq) {
// FIXME:
return;
}
/*
* This function should return 1 if the specified priority
queue is empty and
* 0 otherwise.
*
* Params: pq - the priority queue whose emptiness is
to be checked. May not be
* NULL.
*
* Return: Should return 1 if pq is empty and 0
otherwise.
*/
int pq_isempty(struct pq* pq) {
// FIXME:
return 1;
}
/* This function should insert a given element into a
priority queue with a
* specified priority value. Note that in this
implementation, LOWER priority
* values are assigned to elements with HIGHER priority.
In other words, the
* element in the priority queue with the LOWEST priority
value should be the
* FIRST one returned.
* Params: pq - the priority queue into which to
insert an element. May not be
* NULL.
* value - the value to be inserted into pq.
* priority - the priority value to be assigned to
the newly-inserted
* element. Note that in this
implementation, LOWER priority values
* should correspond to elements with HIGHER
priority. In other words,
* the element in the priority queue with the
LOWEST priority value should
* be the FIRST one returned.
*/
void pq_insert(struct pq* pq, void* value, int priority) {
// FIXME:
return;
}
/*
* This function should return the value of the first item
in a priority
* queue, i.e. the item with LOWEST priority value.
*
* Params:
* pq - the priority queue from which to fetch a
value. May not be NULL or empty.
*
* Return: Should return the value of the first item
in pq, i.e. the item with
* LOWEST priority value.
*/
void* pq_first(struct pq* pq) {
// FIXME:
return NULL;
}
/*
* This function should return the priority value of the
first item in a
* priority queue, i.e. the item with LOWEST priority
value.
*
* Params:
* pq - the priority queue from which to fetch a
priority value. May not be
* NULL or empty.
* Return: Should return the priority value of the
first item in pq, i.e. the item
* with LOWEST priority value.
*/
int pq_first_priority(struct pq* pq) {
// FIX:
return 0;
}
/* This function should return the value of the first item
in a priority
* queue, i.e. the item with LOWEST priority value, and
then remove that item
* from the queue.
* Params: pq - the priority queue from which to
remove a value. May not be NULL or empty.
* Return: Should return the value of the first item
in pq, i.e. the item with
* LOWEST priority value.
*/
void* pq_remove_first(struct pq* pq) {
// FIXME:
return NULL;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply