CList封装模板类

/* *list.h * *Simple doubly linked list implementation. */ #ifndef __LIST_H #define __LIST_H #include "memory.h" #include "stdio.h" template<class TYPE, class ARG_TYPE> class CList { public: typedef struct CNode { CNode *pNext; CNode *pPrev; TYPE data; }NODE, *PNODE; public: CList(int nBlockSize = 10); virtual ~CList(); //add PNODE AddHead(ARG_TYPE data); PNODE AddTail(ARG_TYPE data); //Delete void RemoveHead(); void RemoveTail(); void RemoveAt(PNODE pos); void RemoveAll(); //Insertion PNODE InsertBefor(ARG_TYPE data, PNODE pBefor); PNODE InsertAfter(ARG_TYPE data, PNODE pAfter); //Iteration PNODE GetHeadPosition(); PNODE GetTailPosition(); ARG_TYPE GetNext(PNODE pos); ARG_TYPE GetPrev(PNODE pos); ARG_TYPE GetAt(PNODE pos); //Modification void SetAt(PNODE pos, ARG_TYPE data); typedef BOOL (CALLBACK *FindCallBack)(ARG_TYPE data, void* pParam); //Searching PNODE FindHeadToTail(FindCallBack fFindCallBack, void* pParam = NULL, PNODE pPos = 0); PNODE FindTailToHead(FindCallBack fFindCallBack, void* pParam = NULL, PNODE pPos = 0); //printf virtual void PrintfNode(ARG_TYPE data); //Status bool IsEmpty(); int GetCount(); protected: bool CompareElements(ARG_TYPE data1, ARG_TYPE data2); PNODE NewNode(PNODE pBefor, PNODE pAfter); void FreeNode(PNODE pNode); protected: PNODE m_pHead; PNODE m_pTail; PNODE m_pFree; private: int m_nSize; int m_nCount; int m_nBlockSize; }; ///////////////////////////////////////////////////////////////////////////// // CList<TYPE, ARG_TYPE> inline functions template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::CList(int nBlockSize) { m_pHead = 0; m_pTail = 0; m_pFree = 0; m_nBlockSize = nBlockSize; m_nSize = 0; m_nCount = 0; } template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::~CList() { RemoveAll(); } // Remove node from list to the Free list template<class TYPE, class ARG_TYPE> void CList<TYPE, ARG_TYPE>::FreeNode(PNODE pNode) { memset(pNode, 0 ,sizeof(NODE)); pNode->pNext = m_pFree; pNode->pPrev = NULL; m_pFree = pNode; m_nCount--; //ASSERT(m_nCount >= 0); // make sure we don't underflow } //use new to alloc memory when the Free list is used up, //other remove one node from Free list to list. template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::NewNode(PNODE pBefor, PNODE pAfter) { int i = 0, nRealNew = 0; // add another block if (m_pFree == 0) { PNODE pNode = new NODE; memset(pNode,0,sizeof(NODE)); if (pNode == 0) return pNode; printf("%d/n", pNode); nRealNew++; m_pFree = pNode; PNODE pFreeTail = m_pFree; for (i = 0; i < m_nBlockSize-1; i++) { pNode = 0; pNode = new NODE; if(pNode != 0) { memset(pNode,0,sizeof(NODE)); pNode->pPrev = pFreeTail; pFreeTail->pNext = pNode; pFreeTail = pNode; nRealNew++; printf("%d/n", pNode); }//if (pNewNode != 0) } printf("NewNode %d./n", nRealNew); m_nSize += nRealNew; }//if (m_pFree == 0) //ASSERT(m_pFree != 0); PNODE pNewNode = m_pFree; m_pFree = m_pFree->pNext; if (m_pFree != NULL) { m_pFree->pPrev = NULL; } pNewNode->pPrev = pBefor; pNewNode->pNext = pAfter; m_nCount++; //ASSERT(m_nCount > 0); // make sure we don't overflow return pNewNode; } //add template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::AddHead(ARG_TYPE data) { PNODE pNode = NewNode(0,m_pHead); pNode->data = data; if (m_pHead != 0) m_pHead->pPrev = pNode; else m_pTail = pNode; m_pHead = pNode; return pNode; } template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::AddTail(ARG_TYPE data) { PNODE pNode = NewNode(m_pTail,0); pNode->data = data; if (m_pTail != 0) m_pTail->pNext = pNode; else m_pHead = pNode; m_pTail = pNode; return pNode; } //Delete template<class TYPE, class ARG_TYPE> void CList<TYPE, ARG_TYPE>::RemoveHead() { PNODE pNode = 0; //ASSERT(m_pHead != 0); pNode = m_pHead; m_pHead = m_pHead->pNext; if (m_pHead != 0) m_pHead->pPrev = 0; else m_pTail = 0; FreeNode(pNode); } template<class TYPE, class ARG_TYPE> void CList<TYPE, ARG_TYPE>::RemoveTail() { PNODE pNode = 0; //ASSERT(m_pTail != 0); pNode = m_pTail; m_pTail = m_pTail->pPrev; if (m_pTail != 0) m_pTail->pNext = 0; else m_pHead = 0; FreeNode(pNode); } template<class TYPE, class ARG_TYPE> void CList<TYPE, ARG_TYPE>::RemoveAt(PNODE pos) { PNODE pNode = (PNODE)pos; //ASSERT(pos != 0); if (pNode == m_pHead) { m_pHead = pNode->pNext; } else { pNode->pPrev->pNext = pNode->pNext; } if (pNode == m_pTail) { m_pTail = pNode->pPrev; } else { pNode->pNext->pPrev = pNode->pPrev; } FreeNode(pNode); } template<class TYPE, class ARG_TYPE> void CList<TYPE, ARG_TYPE>::RemoveAll() { PNODE pNode = 0; PNODE pFreeNode = 0; //free the MCB use node list pNode = m_pHead; while(pNode != 0) { pFreeNode = pNode; pNode = pNode->pNext; delete pFreeNode; printf("free use:%d/n", pFreeNode); pFreeNode = 0; } //free the MCB free node list pNode = m_pFree; while(pNode != 0) { pFreeNode = pNode; pNode = pNode->pNext; delete pFreeNode; printf("free unuse:%d/n", pFreeNode); pFreeNode = 0; } m_pHead = m_pTail = m_pFree = 0; m_nCount = 0; m_nSize = 0; } //Insertion template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::InsertBefor(ARG_TYPE data, PNODE pBefor) { if (pBefor == 0) return AddHead(data); PNODE pBeforNode = (PNODE)pBefor; PNODE pNode = NewNode(pBeforNode->pPrev, pBeforNode); pNode->data = data; if (pBeforNode->pPrev != 0) pBeforNode->pPrev->pNext = pNode; else { //ASSERT(pBeforNode == m_pHead); m_pHead = pNode; } pBeforNode->pPrev = pNode; return pNode; } template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::InsertAfter(ARG_TYPE data, PNODE pAfter) { if (pAfter == 0) return AddTail(data); PNODE pAfterNode = (PNODE)pAfter; PNODE pNode = NewNode(pAfterNode, (pAfterNode->pNext)); pNode->data = data; if (pAfterNode->pNext != 0) pAfterNode->pNext->pPrev = pNode; else { //ASSERT(pBeforNode == m_pTail); m_pTail = pNode; } pAfterNode->pNext = pNode; return pNode; } //Iteration template<class TYPE, class ARG_TYPE> inline CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::GetHeadPosition() { return m_pHead; } template<class TYPE, class ARG_TYPE> inline ARG_TYPE CList<TYPE, ARG_TYPE>::GetNext(PNODE pos) { PNODE pNode = (PNODE)pos; pos = pNode->pNext; return pNode->data; } template<class TYPE, class ARG_TYPE> inline CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::GetTailPosition() { return m_pTail; } template<class TYPE, class ARG_TYPE> inline ARG_TYPE CList<TYPE, ARG_TYPE>::GetPrev(PNODE pos) { PNODE pNode = (PNODE)pos; pos = pNode->pPrev; return &(pNode->data); } template<class TYPE, class ARG_TYPE> inline ARG_TYPE CList<TYPE, ARG_TYPE>::GetAt(PNODE pos) { PNODE pNode = pos; return pNode->data; } //Modification template<class TYPE, class ARG_TYPE> inline void CList<TYPE, ARG_TYPE>::SetAt(PNODE pos, ARG_TYPE data) { pos->data = data; } //Searching from head to tail template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::FindHeadToTail(FindCallBack fFindCallBack, void* pParam, PNODE pPos) { PNODE pFindNode = pPos; if (pFindNode == 0) { pFindNode = m_pHead; } for (; pFindNode != 0; pFindNode = pFindNode->pNext) { if( fFindCallBack(pFindNode->data, pParam) ) { return pFindNode; } } return 0; } //Searching from tail to head template<class TYPE, class ARG_TYPE> CList<TYPE, ARG_TYPE>::PNODE CList<TYPE, ARG_TYPE>::FindTailToHead(FindCallBack fFindCallBack, void* pParam, PNODE pPos) { PNODE pFindNode = pPos; if (pFindNode == 0) { pFindNode = m_pTail; } if (fFindCallBack == NULL) {//如果回调函数为空 return NULL; } for (; pFindNode != 0; pFindNode = pFindNode->pPrev) { if( fFindCallBack(pFindNode->data, pParam) ) { return pFindNode; } } return 0; } //Status template<class TYPE, class ARG_TYPE> inline bool CList<TYPE, ARG_TYPE>::IsEmpty() { return (m_nCount == 0); } template<class TYPE, class ARG_TYPE> inline int CList<TYPE, ARG_TYPE>::GetCount() { return m_nCount; } //compare template<class TYPE, class ARG_TYPE> bool CList<TYPE, ARG_TYPE>::CompareElements(ARG_TYPE data1, ARG_TYPE data2) { return (memcmp(&data1 , &data2, sizeof(TYPE)) == 0 ); } //printf template<class TYPE, class ARG_TYPE> void CList<TYPE, ARG_TYPE>::PrintfNode(ARG_TYPE data) { } #endif//__MCB_LIST_H

你可能感兴趣的:(struct,list,null,Class,UP,callback)