#include<iostream>
#include<exception>
using namespace std;
class OutOfRange:public exception {
public:
const char* what()const throw() {
return "ERROR! OUT OF RANGE.\n";
}
};
class BadSize:public exception {
public:
const char* what()const throw() {
return "ERROR! BAD SIZE.\n";
}
};
template<class T>
class priorityQueue {
virtual bool isEmpty()const = 0;
virtual int size()const = 0;
virtual void clear() = 0;
virtual void enQueue(const T& x) = 0;
virtual void deQueue() = 0;
virtual T getHead()const = 0;
virtual ~priorityQueue() = 0;
};
template<class T>
class seqPriorityQueue:public priorityQueue<T> {
private:
T* data;
int maxSize;
int front, rear;
void resize();
public:
seqPriorityQueue(int initSize = 100);
~seqPriorityQueue() { delete[] data; }
bool isEmpty()const { return front == rear; }
int size()const { return (rear - front + maxSize) % maxSize; }
void clear() { front = rear = -1; }
void enQueue(const T& x) = 0;
void deQueue() = 0;
T getHead()const = 0;
};
template<class T>
seqPriorityQueue<T>::seqPriorityQueue(int initSize) {
if (initSize <= 0) throw BadSize();
maxSize = initSize;
data = new T[maxSize];
front = rear = -1;
}
template<class T>
void seqPriorityQueue<T>::enQueue(const T& x) {
if ((rear + 1) % maxSize == front) resize();
rear = (rear + 1) % maxSize;
data[rear] = x;
int i = rear;
while (int j = (i - 1 + maxSize) % maxSize, j != front && data[i] > data[j]) {
swap(data[i], data[j]);
i = j;
}
}
template<class T>
void seqPriorityQueue<T>::deQueue() {
if (isEmpty()) throw OutOfRange();
front = (front + 1) % front;
}
template<class T>
T seqPriorityQueue<T>::getHead()const {
if (isEmpty()) throw OutOfRange();
return data[front];
}
template<class T>
void seqPriorityQueue<T>::resize() {
T* p = data;
data = new T[maxSize * 2];
for (int i = 1; i <= maxSize; ++i) {
data[i] = p[(front + i) % maxSize];
}
maxSize *= 2;
front = 0;
rear = size();
delete[] p;
}
template<class T>
class heapPriorityQueue:public priorityQueue<T> {
private:
T* data;
int maxSize;
int back;
void resize();
void swim(int N, int k);
void sink(int N, int k);
public:
seqPriorityQueue(int initSize = 100);
~seqPriorityQueue() { delete[] data; }
bool isEmpty()const { return back == 0; }
int size()const { return back; }
void clear() { back = 0; }
void enQueue(const T& x) = 0;
void deQueue() = 0;
T getHead()const = 0;
};
template<class T>
heapPriorityQueue<T>::heapPriorityQueue(int initSize) {
if (initSize <= 0) throw BadSize();
maxSize = initSize;
data = new T[maxSize + 1];
back = 0;
}
template<class T>
void heapPriorityQueue<T>::enQueue(const T& x) {
if (back == maxSize) resize();
data[++back] = x;
swim(back,back);
}
template<class T>
void heapPriorityQueue<T>::deQueue() {
if (isEmpty()) throw OutOfRange();
swap(data[1], data[back]);
sink(--back,1);
}
template<class T>
T heapPriorityQueue<T>::getHead()const {
if (isEmpty()) throw OutOfRange();
return data[1];
}
template<class T>
void heapPriorityQueue<T>::resize() {
T* p = data;
data = new T[maxSize * 2];
for (int i = 1; i <= maxSize; ++i) {
data[i] = p[i];
}
maxSize *= 2;
delete[] p;
}
template<class T>
void heapPriorityQueue<T>::swim(int N, int k) {
while (k > 1 && data[k] > data[k/2]) {
swap(data[k], data[k/2]);
k /= 2;
}
}
template<class T>
void heapPriorityQueue<T>::sink(int N, int k) {
while (k * 2 <= N) {
int j = k * 2;
while (j + 1 <= N && data[j] < data[j + 1]) ++j;
if (data[k] >= data[j]) break;
swap(data[k], data[j]);
k = j;
}
}
【数据结构】优先队列
最新推荐文章于 2021-11-14 16:35:14 发布