pqueue.h

This interface defines a queue abstraction that dequeues elements in priority order. In keeping with traditional English usage, lower priority numbers have higher priority, so that priority 1 items appear before priority 2 items.
Type
PriorityQueue This type defines the abstract type for a queue.
Functions
newPriorityQueue() Allocates and returns an empty priority queue.
freePriorityQueue(pq) Frees the storage associated with the specified priority queue.
enqueue(pq, value, priority) Adds a value to the queue in the order specified by priority.
dequeue(pq) Removes the data value at the head of the queue and returns it to the client.
peek(pq) Returns the data value at the head of the queue without removing it.
peekPriority(pq) Returns the priority of the first entry in the queue, without removing it.
isEmpty(pq) Tests whether the queue is empty.
size(pq) Returns the number of values in the queue.
clear(pq) Removes all values from the queue.
clone(pq) Creates a copy of the priority queue.

Type detail


typedef struct PriorityQueueCDT *PriorityQueue;
This type defines the abstract type for a queue.

Function detail


PriorityQueue newPriorityQueue(void);
Allocates and returns an empty priority queue.

Usage:

pq = newPriorityQueue();

void freePriorityQueue(PriorityQueue pq);
Frees the storage associated with the specified priority queue.

Usage:

freePriorityQueue(pq);

void enqueue(PriorityQueue pq, void *value, double priority);
Adds a value to the queue in the order specified by priority. In C, the priority argument must have type double, which means that constants used to express priorities in the call to the generic enqueue function must include a decimal point.

Usage:

enqueue(pq, value, priority);

void *dequeue(PriorityQueue pq);
Removes the data value at the head of the queue and returns it to the client. If the queue is empty, dequeue calls error with an appropriate message.

Usage:

value = dequeue(pq);

void *peek(PriorityQueue pq);
Returns the data value at the head of the queue without removing it. If the queue is empty, peek calls error with an appropriate message.

Usage:

value = peek(pq);

double peekPriority(PriorityQueue pq);
Returns the priority of the first entry in the queue, without removing it.

Usage:

priority = peekPriority(pq);

bool isEmpty(PriorityQueue pq);
Tests whether the queue is empty.

Usage:

if (isEmpty(pq)) . . .

int size(PriorityQueue pq);
Returns the number of values in the queue.

Usage:

n = size(pq);

void clear(PriorityQueue pq);
Removes all values from the queue.

Usage:

clear(pq);

PriorityQueue clone(PriorityQueue pq);
Creates a copy of the priority queue. The clone function copies only the first level of the structure and does not copy the individual elements.

Usage:

newpq = clone(pq);