代碼按照適配器模式實(shí)現(xiàn),若理解了堆的內(nèi)部怎么實(shí)現(xiàn)的,那優(yōu)先級(jí)的隊(duì)列實(shí)現(xiàn)則是非常簡單的了,堆的設(shè)計(jì)大家不明白的話,可以查看我的博客http://10740184.blog.51cto.com/10730184/1767076。

建立PriorityQueue.hpp:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<assert.h>
#include<vector>
template<class T>
struct Less
{
bool operator() (const T& l, const T& r)
{
return l < r;
}
};
template<class T>
struct Greater
{
bool operator() (const T& l, const T& r)
{
return l>r;
}
};
template<class T,template<class> class Compare=Less>
class Heap
{
public:
Heap()
:_a(NULL)
{}
Heap(const T* a,size_t size)
{
for (int i = 0; i < size; i++)
{
_a.push_back(a[i]);
}
for (int i = (size - 2) / 2; i >= 0; i--)
{
_adjustDown(i);
}
}
void _adjustDown(size_t parent)
{
Compare<T> com;
size_t child = 2 * parent + 1;
size_t size = _a.size();
while (child<size)
{
if (child + 1<size && com(_a[child + 1],_a[child]))
{
++child;
}
if (com(_a[child],_a[parent]))
{
swap(_a[child], _a[parent]);
parent = child;
child = 2 * parent + 1;
}
else
{
break;
}
}
}
void Push(const T& x)
{
_a.push_back(x);
_adjustUp(_a.size()-1);
}
void _adjustUp(size_t child)
{
int parent = (child - 1) / 2;
Compare<T> com;
while (child>0)
{
if (com(_a[child],_a[parent]))
{
swap(_a[parent], _a[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
size_t Size()
{
size_t size = _a.size();
return size;
}
bool Empty()
{
assert(Size() >= 0);
return Size() == 0;
}
void Pop()
{
assert(_a.Size() > 0);
swap(_a[0], _a[Size - 1]);
_a.pop_back();
_adjustDown(0);
}
T& GetTop()
{
return _a[0];
}
void Print()
{
cout << "大堆序列:" << endl;
for (int i = 0; i < _a.size(); i++)
{
cout << _a[i] << " ";
}
cout << endl;
}
public:
vector<T> _a;
};
template<class T, template<class> class Compare = Less>
class PriorityQueue
{
public:
void Push(const T& x)
{
_hp.Push(x);
}
void Pop()
{
_hp.Pop();
}
size_t Size()
{
return _hp.Size();
}
bool Empty()
{
_hp.Empty();
}
T& Top()
{
return _hp.GetTop();
}
void Print()
{
_hp.Print();
}
private:
Heap<T,Compare> _hp;
};建立PriorityQueue.cpp文件:
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include "PriorityQueue.hpp"
void Test()
{
int a[] = { 10, 11, 13, 12, 16, 18, 15, 17, 14, 19 };
PriorityQueue<int,Greater> pq;
pq.Push(10);
pq.Push(11);
pq.Push(13);
pq.Push(12);
pq.Push(16);
pq.Push(18);
pq.Push(15);
pq.Push(17);
pq.Push(14);
pq.Push(19);
pq.Print();
}
int main()
{
Test();
system("pause");
return 0;
}另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:【數(shù)據(jù)結(jié)構(gòu)】優(yōu)先級(jí)隊(duì)列的實(shí)現(xiàn)(適配器模式)-創(chuàng)新互聯(lián)
路徑分享:http://www.chinadenli.net/article20/dospco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化、品牌網(wǎng)站制作、網(wǎng)站收錄、Google、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容