CQueue封闭队列模板类

// Queue.h: interface for the CQueue class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_QUEUE_H__9DB216C0_AF3C_4C99_B776_FD19EAEBFBDB__INCLUDED_) #define AFX_QUEUE_H__9DB216C0_AF3C_4C99_B776_FD19EAEBFBDB__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #define QUEUE_SIZE 20 template<class TYPE> class CQueue { private: TYPE* m_pQueue; //存放队列元素的指针(数组) int m_nSize; //队列大小(容量) int m_nFront; //队首位置 int m_nRear; //队尾位置(最后一个元素的下一位置) int m_nCount; //队列中元素的个数 public: //构造函数 CQueue(int nSize=QUEUE_SIZE) { m_nFront = 0; m_nRear = 0; m_nCount = 0; m_nSize = 0; if (nSize < 1) nSize = QUEUE_SIZE; m_pQueue = new TYPE[nSize]; if (m_pQueue != NULL) { m_nSize = nSize; memset(m_pQueue, 0, sizeof(TYPE)*nSize); } } //析构函数 virtual ~CQueue() { if (m_pQueue) delete [] m_pQueue; m_nFront = 0; m_nRear = 0; m_nCount = 0; m_nSize = 0; } //判断队列是否为空 BOOL IsEmpty() { return m_nCount == 0; } //判断队列是否已满 BOOL IsFull() { return m_nCount == m_nSize; } //队尾插入(追加)元素 BOOL Insert(const TYPE &item) { if (m_nCount == m_nSize) { return FALSE; } m_nCount ++; m_pQueue[m_nRear] = item; m_nRear = (m_nRear + 1) % m_nSize; //rear始终指向最后一个元素的下一个位置 return TRUE; } //队首删除元素 BOOL Delete(TYPE &data) { if (m_nCount <= 0) { return FALSE; } data = m_pQueue[m_nFront]; m_nCount --; m_nFront = (m_nFront + 1) % m_nSize; //front移向下一位置 return TRUE; } //读取队首元素 BOOL Front(TYPE &data) { if (m_nCount <=0) { return FALSE; } data = m_pQueue[m_nFront]; return TRUE; } //清空队列 void ClearQueue() { m_nFront = 0; m_nRear = 0; m_nCount = 0; } //队列长度 int GetCount() { return m_nCount; } //获取队首位置 int GetFrontPos() { return m_nFront; } //获取队首位置 int GetRearPos() { return m_nRear; } typedef BOOL (CALLBACK *SearchCallBack)(TYPE& data, void* pParam); TYPE* SearchQueueFront(SearchCallBack fSearchCallBack, void* pParam = NULL); TYPE* SearchQueueBack(SearchCallBack fSearchCallBack, void* pParam = NULL); }; //从队尾 template<class TYPE> TYPE* CQueue<TYPE>::SearchQueueFront(SearchCallBack fSearchCallBack, void* pParam) { int nFindPos = m_nFront; int nFindEndPos = m_nFront; if ( (fSearchCallBack == NULL) || ( m_nCount <=0 ) ) { return NULL; } if (m_nRear > m_nFront) { nFindEndPos = m_nRear; for(nFindPos=m_nFront; nFindPos<=nFindEndPos; nFindPos++) { if ( fSearchCallBack(&m_pQueue[nFindPos],pParam) ) { return &m_pQueue[nFindPos],pParam; } } } else { nFindEndPos = m_nSize; for(nFindPos=m_nFront; nFindPos<=nFindEndPos; nFindPos++) { if ( fSearchCallBack(&m_pQueue[nFindPos],pParam) ) { return &m_pQueue[nFindPos],pParam; } } for(nFindPos=0; nFindPos<=m_nRear; nFindPos++) { if ( fSearchCallBack(&m_pQueue[nFindPos],pParam) ) { return &m_pQueue[nFindPos],pParam; } } } return NULL; } template<class TYPE> TYPE* CQueue<TYPE>::SearchQueueBack(SearchCallBack fSearchCallBack, void* pParam) { int nFindPos = m_nRear; int nFindEndPos = m_nRear; if ( (fSearchCallBack == NULL) || ( m_nCount <=0 ) ) { return NULL; } if (m_nRear > m_nFront) {//队尾位置大于队首位置 nFindEndPos = m_nFront; for(nFindPos=m_nRear; nFindPos>=nFindEndPos; nFindPos--) { if ( fSearchCallBack(&m_pQueue[nFindPos],pParam) ) { return &m_pQueue[nFindPos]; } } } else {//队尾位置小于队首位置 for(nFindPos=m_nRear; nFindPos>=0; nFindPos--) { if ( fSearchCallBack(&m_pQueue[nFindPos],pParam) ) { return &m_pQueue[nFindPos]; } } nFindEndPos = m_nFront; for(nFindPos=m_nSize; nFindPos>=nFindEndPos; nFindPos--) { if ( fSearchCallBack(&m_pQueue[nFindPos],pParam) ) { return &m_pQueue[nFindPos]; } } } return NULL; } #endif // !defined(AFX_QUEUE_H__9DB216C0_AF3C_4C99_B776_FD19EAEBFBDB__INCLUDED_)

你可能感兴趣的:(db2,null,delete,insert,callback,interface)