linux__链表__操作

#include <stdio.h>
#include <stdlib.h>

 CSocketList:: CSocketList()
 {
        lSocket=0;  ///当前节点的socket
        lUdata=NULL; ///节点的 user data
        lHead=NULL;  
        lPrev=NULL;  
        lNext=NULL;  
        lTail=NULL;  
 }
 CSocketList:: ~CSocketList()
 {
 
 }
int CSocketList::clean_socketlist()
{
   CycleList *pList1,*pList2;
   if(lHead==NULL)
   {
      return 0;  ///链表为空,直接返回	
   }
   
   pList1 = lHead;
   while(pList1 != NULL)
   {   
	   pList2 = pList1->lNext;
	   free(pList1);
	   pList1=pList2;	
   }	
	 return 0; ///	
}

int CSocketList::addlist_bysocket(int socket,void *udata)
{
	CycleList *newlist;

     if(socket==0)
    {
      return -1;	
    }

    newlist=lHead;
      while(newlist!=NULL)
     {
	  if(newlist->lSocket==socket)///如果已经存在,则不增加 直接返回
                  {
	             printf("socket always exist! not execute;return\n");
	             return 0;
	  }
	  newlist=newlist->lNext;
     }

   newlist=(CycleList* )malloc(sizeof(CycleList));
   if(newlist==NULL)
   {
      printf("list malloc failed!\n");
      return -1;	
   }

   if(lHead==NULL)///如果是刚开始
   {
       lHead=newlist;
       lHead->lSocket=socket;
       lHead->lUdata=udata;
       lHead->lNext=NULL; 
       lHead->lPrev=NULL;
       lTail=NULL;
   }
   else if(lTail==NULL)///第二次
   {
	 
      newlist->lSocket=socket;
      newlist->lUdata=udata;  

      newlist->lPrev=lHead;
      newlist->lNext=NULL;
      lHead->lNext=newlist;  
	  lTail=newlist;
   }	
   else
   {	
      newlist->lSocket=socket;
      newlist->lUdata=udata; 
	  
      newlist->lPrev=lTail;
      newlist->lNext=NULL;
      
      lTail->lNext=newlist;
      lTail=newlist;  ///追加到尾部
   }
   return 0;
}  
int CSocketList::dellist_bysocket(int socket) ///
{

你可能感兴趣的:(linux__链表__操作)