【数据结构】优先队列

#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;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值