一个通用的链表管理类

/*
**    CManageList为链表管理类,可以对不同类型的内存链表
**    进行插入,查询(特定了结点字段)、出列、卸载等动作
*/

class CManageList 
{
public:
 CManageList()
 {
  //InitializeCriticalSection(&m_ListIoCriSection);
 };

 virtual ~CManageList()
 {
  //DeleteCriticalSection(&m_ListIoCriSection);
 };
 
 template<typename T>
  BOOL InsertToList(CRITICAL_SECTION ListCriSection,T & lpList,T & lpNode,int iSize = 0,bool bIfInit=true,bool bTail=false)//把一个结点插入到链表头部,需要结点具有指向下一个结点的元素pNext
 {
  EnterCriticalSection(&ListCriSection);
  
  if (NULL != lpNode)
  {
   if (bIfInit) ZeroMemory(lpNode,iSize);
   
   if (NULL == lpList)
   {
    lpList = lpNode;
    lpList->pNext = NULL;
   }
   else
   {
    bool bExist = false;
    
    T lpPreIoNode = lpList;
    T lpTmpIoNode = lpList;
    
    while (NULL != lpTmpIoNode)//遍历链表,存在相同的结点内存不插入
    {
     if (lpNode == lpTmpIoNode)
     {
      bExist = true;
      break;
     }
     else
     {
      lpPreIoNode = lpTmpIoNode;
      lpTmpIoNode = lpTmpIoNode->pNext;
     }
    }
    
    if (!bExist)
    {
     if (bTail)//需要插入到链表尾部
     {
      lpPreIoNode->pNext = lpNode;
      lpNode->pNext = NULL;
     }
     else//需要插入到链表的头部
     {
      lpNode->pNext = lpList;
      lpList = lpNode;
     }
    }
    
   }
  }
  
  LeaveCriticalSection(&ListCriSection);
  
  return TRUE;
 }

 template<typename T>
  T GetFromList(int iSize,CRITICAL_SECTION ListCriSection,T & lpList)//从链表头部获取一个结点,需要结点具有指向下一个结点的元素pNext
 {
  EnterCriticalSection(&ListCriSection);
  T lpReturnNode = NULL;
  
  if (NULL == lpList)
  {
   lpReturnNode = (T)HeapAlloc(
    GetProcessHeap(),
    HEAP_ZERO_MEMORY,
    iSize);

   TRACE("---------Create memory (0x%x),size(%d)------/r/n",lpReturnNode,iSize);
  }
  else
  {
   lpReturnNode = lpList;
   lpList = lpList->pNext;
  }

  lpReturnNode->pNext = NULL;

  LeaveCriticalSection(&ListCriSection);
  
  return lpReturnNode;
 }

 template<typename T>
  T MoveNodeFromList(CRITICAL_SECTION ListCriSection,T & lpList)//从链表头部移走一个结点,需要结点具有指向下一个结点的元素pNext
 {
  EnterCriticalSection(&ListCriSection);
  T lpReturnNode = NULL;
  
  if (NULL != lpList)
  {
   lpReturnNode = lpList;
   lpList = lpList->pNext;
   lpReturnNode->pNext = NULL;
  }

  LeaveCriticalSection(&ListCriSection);
  
  return lpReturnNode;
 }

 template<typename T>//从链表接连获取一个结点,需要结点具有指向下一个结点的元素pNext
  T GetNextNodeFromList(CRITICAL_SECTION ListCriSection,T lpList,int iPos = 1)
 {//int iPos = 0表示取的表头指针,1表示取下一个
  EnterCriticalSection(&ListCriSection);
  static T lpNowNode;
  T lpReturnNode = NULL;
  
  if (!iPos)//表示取表头
  {
   lpNowNode = lpList;
  }

  lpReturnNode = lpNowNode;
  if (NULL != lpNowNode)lpNowNode = lpNowNode->pNext;

  LeaveCriticalSection(&ListCriSection);
  
  return lpReturnNode;
 }

template<typename T>/*从链表卸载一个关键索引的结点。需要结点具有指向下一个结点的元素pNext。*/
  BOOL ReleaseNodeFromList(CRITICAL_SECTION ListCriSection,T & lpList,T & lpIdleList)
 {
  EnterCriticalSection(&ListCriSection);

  BOOL bReturn = TRUE;

  T lpNowNode = lpList;
  T lpTmpNode = lpList;
  
  while (NULL != lpNowNode)
  {
   if (lpIdleList == lpNowNode)
   {
    if (lpNowNode != lpTmpNode)//不是链表头
    {
     lpTmpNode->pNext = lpNowNode->pNext;
    }
    else
    {
     lpList = lpList->pNext;
    }
    
    lpNowNode->pNext = NULL;
    lpIdleList->pNext = NULL;

    break;
   }

   lpTmpNode = lpNowNode;
   lpNowNode = lpNowNode->pNext;
  }

  LeaveCriticalSection(&ListCriSection);
  
  return bReturn;
 }

 template<typename T>
  void ReleaseList(CRITICAL_SECTION ListCriSection,T & lpList)//释放链表所有结点内存,需要结点具有指向下一个结点的元素pNext
 {
  EnterCriticalSection(&ListCriSection);
  
  T lpNode = NULL;
  
  while(NULL != lpList)
  {
   lpNode = lpList;
   lpList = lpList->pNext;
   HeapFree(GetProcessHeap(), 0, lpNode);
  }

  LeaveCriticalSection(&ListCriSection);
 }

你可能感兴趣的:(null,Class)