优先级队列,代码参考范例
生活随笔
收集整理的這篇文章主要介紹了
优先级队列,代码参考范例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個看起來比較規范的代碼:
1、版本信息
2、預處理信息
3、庫函數引用
4、泛型編程
5、宏定義
6、復制構造函數
7、內斂函數
8、變量命名規范
9、代碼的時間空間效率
10、錯誤恢復能力
11、規范的注釋和縮進
代碼范例:
?
/***************************************** Project:PriorityQueue** File:priqueue.h** Edition:v1.0.0 Demo** Coder:KingsamChen [MDSA Group]** Last Modify:2011-9-1****************************************/#if _MSC_VER > 1000 #pragma once #endif#ifndef _PRIQUEUE_05232021_A052_400f_ADD6_8FA21FDCBF7E #define _PRIQUEUE_05232021_A052_400f_ADD6_8FA21FDCBF7E#include <cassert> #include <cstdio>#define PARENT(x) (((x) - 1) >> 1) #define LEFTCHILD(x) (((x) << 1) + 1) #define RIGHTCHILD(x) (((x) + 1) << 1)template<typename T> class CPriQueue {public:typedef int pos;public:CPriQueue();CPriQueue(const CPriQueue& q);~CPriQueue();public:CPriQueue& operator =(const CPriQueue& q);void BuildHeap(const T ary[], int count);void Insert(const T& ele);T ExtractMin();inline T Min() const;inline int GetCount() const;inline bool IsEmpty() const;inline bool IsFull() const;pos Find(const T& ele) const;void DecreaseKey(pos p, unsigned int det);void IncreaseKey(pos p, unsigned int det);void Delete(pos p);// diagnostic interface#if _DEBUGvoid DgPrint();#endifprivate:void PercolateUp(int i, const T& ele);void PercolateDown(int i, const T& ele);private:enum{INI_CAPCITY = 50, NOT_FOUND = -1};T* m_pHeap;int m_capcity;int m_count; };template<typename T> CPriQueue<T>::CPriQueue() : m_count(0) {m_pHeap = new T[INI_CAPCITY];assert(m_pHeap != NULL);m_capcity = INI_CAPCITY; }template<typename T> CPriQueue<T>::CPriQueue(const CPriQueue& q) : m_capcity(q.m_capcity),m_count(q.m_count) {m_pHeap = new T[m_capcity];assert(m_pHeap != NULL); // the element may have internal handle pointing to the extra data outside// assume that the object already overloaded operator =for (int i = 0; i < m_count; ++i){m_pHeap[i] = q.m_pHeap[i];} }template<typename T> CPriQueue<T>::~CPriQueue() {if (m_pHeap != NULL){delete [] m_pHeap;m_pHeap = NULL;m_capcity = 0;m_count = 0;} }template<typename T> CPriQueue<T>& CPriQueue<T>::operator =(const CPriQueue& q) {if (m_capcity < q.m_count){// need to expandassert(false);}m_count = q.m_countfor (int i = 0; i < m_count; ++i){m_pHeap[i] = q.m_pHeap[i];} return *this; }template<typename T> void CPriQueue<T>::Insert(const T& ele) {if (IsFull()){// Logs error or expands capcity of the heapassert(false);}// new element may violate heap propertyPercolateUp(m_count, ele);++m_count; }/*Description:Adjusts the specific element which may violate the heap propertyupward.Parameters:i[in] - the position in the heap of the specific element. ele[in] - a copy of the element. It's used to make the function moreefficient. Do not have this parameter refered to the element directly.It may possible change the value of the ele while adjusting.Return Value:none */ template<typename T> void CPriQueue<T>::PercolateUp(int i, const T& ele) {for (int p = PARENT(i); ele < m_pHeap[p]; p = PARENT(p)){// reaches the rootif (0 == i){break;}m_pHeap[i] = m_pHeap[p];i = p;}m_pHeap[i] = ele; }template<typename T> T CPriQueue<T>::ExtractMin() {assert(!IsEmpty());T ret(m_pHeap[0]);// new root violates the heap propertyPercolateDown(0, m_pHeap[--m_count]);return ret; }/*Description:It is Similar to the function PercolateUp but downward.Parameters:i[in] - the position in the heap of the specific element. ele[in] - the same as in PercolateUpReturn Value:none */ template<typename T> void CPriQueue<T>::PercolateDown(int i, const T& ele) {for (; LEFTCHILD(i) < m_count;){// the node may have only left childint iL = LEFTCHILD(i);int iR = RIGHTCHILD(i);int iMin = iR < m_count ? (m_pHeap[iL] < m_pHeap[iR] ? iL : iR) : iL; if (m_pHeap[iMin] < ele){m_pHeap[i] = m_pHeap[iMin];i = iMin;} else{break;}}m_pHeap[i] = ele; }template<typename T> inline T CPriQueue<T>::Min() const {assert(!IsEmpty());return m_pHeap[0]; }template<typename T> inline int CPriQueue<T>::GetCount() const {return m_count; }template<typename T> inline bool CPriQueue<T>::IsEmpty() const {return 0 == m_count ? true : false; }template<typename T> inline bool CPriQueue<T>::IsFull() const {return m_capcity == m_count ? true : false; }/*Description:Returns the position of the specific element to be found. The functiontakes O(N) timeParameters:ele[in] - the element we search forReturn Value:The function returns NOT_FOUND if the specific element is not foundotherwise the return value indicates the position of the element */ template<typename T> typename CPriQueue<T>::pos CPriQueue<T>::Find(const T& ele) const {pos index = NOT_FOUND;for (int i = 0; i < m_count; ++i){if (m_pHeap[i] == ele){index = i;break;}}return index; }template<typename T> void CPriQueue<T>::DecreaseKey(pos p, unsigned int det) {assert(p >= 0);m_pHeap[p] -= det;T newEle(m_pHeap[p]);// adjusts the order propertyPercolateUp(p, newEle); }template<typename T> void CPriQueue<T>::IncreaseKey(pos p, unsigned int det) {assert(p >= 0);m_pHeap[p] += det;T newEle(m_pHeap[p]);PercolateDown(p, newEle); }template<typename T> void CPriQueue<T>::Delete(pos p) {assert(p >= 0);int det = m_pHeap[p] - m_pHeap[0] + 1;DecreaseKey(p, det);ExtractMin(); }/*Description:Builds up the heap from an arrayParameters:ary[in] - the array contains elementscount[in] - indicates the counts of the elements in arrayReturn Value:none */ template<typename T> void CPriQueue<T>::BuildHeap(const T ary[], int count) {assert(m_capcity >= count);for (int i = 0; i < count; ++i){m_pHeap[i] = ary[i];}m_count = count;for (int i = PARENT(count - 1); i >= 0; --i){T eleMov(m_pHeap[i]);PercolateDown(i, eleMov);} }#if _DEBUG template<typename T> void CPriQueue<T>::DgPrint() {for (int i = 0; i < m_count; ++i){wprintf_s(L"%d\t", m_pHeap[i]);}wprintf_s(L"\n"); } #endif#endif
轉載于:https://www.cnblogs.com/fistao/archive/2013/05/29/3106154.html
總結
以上是生活随笔為你收集整理的优先级队列,代码参考范例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “李逵遇李鬼”!快手把块手直播告了:获赔
- 下一篇: VS2003,VS2005,VS2008