一个通用的链表管理类

/*
**    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);
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值