EECS678: Project 2: Scheduler
libpriqueue.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "libpriqueue.h"

Functions

void priqueue_print (priqueue_t *q, char *str)
 
void priqueue_init (priqueue_t *q, int(*comparer)(const void *, const void *))
 Initializes the priqueue_t data structure. More...
 
unsigned int priqueue_offer (priqueue_t *q, void *ptr)
 Insert the specified element into this priority queue. More...
 
void * priqueue_peek (priqueue_t *q)
 Retrieves, but does not remove, the head of this queue, returning NULL if this queue is empty. More...
 
void * priqueue_poll (priqueue_t *q)
 Retrieves and removes the head of this queue, or NULL if this queue is empty. More...
 
void * priqueue_at (priqueue_t *q, unsigned int index)
 Returns the element at the specified position in this list, or NULL if the queue does not contain an index'th element. More...
 
unsigned int priqueue_remove (priqueue_t *q, void *ptr)
 Removes all instances of ptr from the queue. More...
 
void * priqueue_remove_at (priqueue_t *q, unsigned int index)
 Removes the specified index from the queue, moving later elements up a spot in the queue to fill the gap. More...
 
unsigned int priqueue_size (priqueue_t *q)
 Return the number of elements in the queue. More...
 
void priqueue_destroy (priqueue_t *q)
 Destroys and frees all the memory associated with q. More...
 

Function Documentation

◆ priqueue_at()

void* priqueue_at ( priqueue_t q,
unsigned int  index 
)

Returns the element at the specified position in this list, or NULL if the queue does not contain an index'th element.

Parameters
qa pointer to an instance of the priqueue_t data structure
indexposition of retrieved element
Returns
the index'th element in the queue
NULL if the queue does not contain the index'th element

◆ priqueue_destroy()

void priqueue_destroy ( priqueue_t q)

Destroys and frees all the memory associated with q.

Parameters
qa pointer to an instance of the priqueue_t data structure

◆ priqueue_init()

void priqueue_init ( priqueue_t q,
int(*)(const void *, const void *)  comparer 
)

Initializes the priqueue_t data structure.

Assumptions

  • You may assume this function will only be called once per instance of priqueue_t
  • You may assume this function will be the first function called using an instance of priqueue_t.
Parameters
qa pointer to an instance of the priqueue_t data structure
comparera function pointer that compares two elements. See also Compare Function

◆ priqueue_offer()

unsigned int priqueue_offer ( priqueue_t q,
void *  ptr 
)

Insert the specified element into this priority queue.

Parameters
qa pointer to an instance of the priqueue_t data structure
ptra pointer to the data to be inserted into the priority queue
Returns
The zero-based index where ptr is stored in the priority queue, where 0 indicates that ptr was stored at the front of the priority queue.

◆ priqueue_peek()

void* priqueue_peek ( priqueue_t q)

Retrieves, but does not remove, the head of this queue, returning NULL if this queue is empty.

Parameters
qa pointer to an instance of the priqueue_t data structure
Returns
pointer to element at the head of the queue
NULL if the queue is empty

◆ priqueue_poll()

void* priqueue_poll ( priqueue_t q)

Retrieves and removes the head of this queue, or NULL if this queue is empty.

Parameters
qa pointer to an instance of the priqueue_t data structure
Returns
the head of this queue
NULL if this queue is empty

◆ priqueue_remove()

unsigned int priqueue_remove ( priqueue_t q,
void *  ptr 
)

Removes all instances of ptr from the queue.

This function should not use the comparer function, but check if the data contained in each element of the queue is equal (==) to ptr.

Parameters
qa pointer to an instance of the priqueue_t data structure
ptraddress of element to be removed
Returns
the number of entries removed

◆ priqueue_remove_at()

void* priqueue_remove_at ( priqueue_t q,
unsigned int  index 
)

Removes the specified index from the queue, moving later elements up a spot in the queue to fill the gap.

Parameters
qa pointer to an instance of the priqueue_t data structure
indexposition of element to be removed
Returns
the element removed from the queue
NULL if the specified index does not exist

◆ priqueue_size()

unsigned int priqueue_size ( priqueue_t q)

Return the number of elements in the queue.

Parameters
qa pointer to an instance of the priqueue_t data structure
Returns
the number of elements in the queue