Outline
Definition: A priority queue is an ADT where each object placed into the priority queue has a priority value. The objects can be retrieved via top or pop but the object with the highest priority is retrieved first.
push places a new object in an appropriate location based on its priority
Examples of priority types:
Numeric priority based on $x$ (lower value has higher prioirty)
Example Lexicographic Priority: Given a key pair such as $(a,b)$, we say that the $(a,b)$ has higher priority than $(c,d)$ if
Processes requesting hardware components have prioirty
Linux nice command: -20 highest priority and 19 means lowest prioirty
$ nice -20 ./a.out
#include "myqueue.h"
template<typename Type, int M>
class MultiQueue {
private:
Queue<Type> queue_array[M];
int queue_size;
public:
MultiQueue();
bool isempty() const;
Type top() const;
void push(const Type &, int);
Type pop();
};
template<typename Type, int M>
MultiQueue<Type>::MultiQueue() : queue_size( 0 ) {
// empty constructor
}
template<typename Type, int M>
bool MultiQueue<Type>::isempty() {
return (queue_size == 0);
}
template<typename Type, int M>
Type MultiQueue<Type>::top() {
for (int pri = 0; pri < M; ++pri) {
if (!queue_array[pri].isempty()) {
return queue_array[pri].front();
}
}
throw underflow;
}
template<typename Type, int M>
Type MultiQueue<Type>::pop() {
for (int pri = 0; pri < M; ++pri) {
if (!queue_array[pri].isempty()) {
-- queue_size;
return queue_array[pri].pop();
}
}
throw underflow;
}
template<typename Type, int M>
void MultiQueue<Type>::push(const Type & obj, int pririty) {
if (priority < 0 || priority >= M) {
throw illegal_argument;
}
queue_array[priority].push(obj);
++queue_size;
}
template<typename KeyType, typename RecType>
class AVL_tree;
template<typename Type>
class AVL_priority_queue() {
private:
AVL_tree<int, Type> tree; // keytype=int
public:
bool isempty() const;
Type top() const;
Type pop();
void push(const Type &, int);
}
bool AVL_priority_queue<Type>::isempty() const {
return tree.isempty();
}
Type AVL_priority_queue<Type>::top() const {
return tree.retrieve( tree.front() );
}
void AVL_priority_queue<Type>::push(const Type &obj, int k) {
tree.insert(k, obj);
}
Type AVL_priority_queue<Type>::pop() {
Type obj = tree.front();
tree.erase(obj);
return obj;
}