双向链表

 
双向循环链表在内存分配上更容易管理,因为它可以重复利用已分配的内存.
而双向链表一般是不用的结点直接就释放了.


如果你要把数据集合看成一个环,可以顺时针走,也可以逆时针走,那你就可以用双向循环链表来描述
如果你要把数据集合看成一条链,可以双向遍历,那你就可以用双向链表来描述
如果你要把数据集合看成一个层次结构,那你可以用树来描述
如果你要把数据集合看成……,比方说,你完全想不到谁跟谁会有关系,那你可以用图来描述

struct LocoIpSocketEt_t
{
 
  int                 index;                  /* socketDesc & index of the array */
  U32                 ctrlSessionId;          /* session id used by control plane, transparent to dataplane */
  PROCESS             masterPid;              /* the control plane process which use this socket */

  U8                  addrFamily;             /* address family */
  U16                 udpPort;                /* local udp port in network format */
  U32                 ipAddr;                 /* local ip address in network format */
  U16                 remoteUdpPort;          /* remote udp port in network format */
  U32                 remoteIpAddr;           /* remote ip address in network format */



  /* only for CIV to double-linked the sockets */
  LocoIpSocketEt_Socket     *prev;
  LocoIpSocketEt_Socket     *next;
};


typedef struct LocoIpSocketEt_t LocoIpSocketEt_Socket;


/**************************************************************************************
   Func Name    : 

   Description  : add the socket in the busyList for sending traffic

   Parameters   : headerSocketPPtr - busyList header, may be NULL
                  newSocketPtr     - new socket data pointer

   Return Value : NONE

   Applicable   : 
 ***************************************************************************************/
static void AddSocketForTraffic(LocoIpSocketEt_Socket **headerSocketPPtr, 
                                                       LocoIpSocketEt_Socket *newSocketPtr)
{
 

  /*always insert the new socket at last, that is to say, prevous of header*/
  if (*headerSocketPPtr != NULL)
  {
    /*add this socket before the header, that's the new tailer*/
    newSocketPtr->prev = (*headerSocketPPtr)->prev;
    newSocketPtr->next = *headerSocketPPtr;
    (*headerSocketPPtr)->prev->next = newSocketPtr;
    (*headerSocketPPtr)->prev = newSocketPtr;
  }
  else
  {
    /*this is the first socket in this sub-simulation dlink */
    *headerSocketPPtr  = newSocketPtr;
    newSocketPtr->next = newSocketPtr;
    newSocketPtr->prev = newSocketPtr;
  }


}

/**************************************************************************************
   Func Name    : cRemoveSocketFromTraffic

   Description  : remove the socket in the busyList for sending traffic
                  but no change for the socket state

   Parameters   : headerSocketPPtr - busyList header, may be NULL
                  newSocketPtr     - new socket data pointer

   Return Value : NONE

   Applicable   : 
 ***************************************************************************************/
static void RemoveSocketFromTraffic(LocoIpSocketEt_Socket **headerSocketPPtr, 
                                                      LocoIpSocketEt_Socket *newSocketPtr)
{
 

  /*if this socket is the only one in the busyList*/
  if (newSocketPtr->next == newSocketPtr)
  {
    *headerSocketPPtr = NULL;
    newSocketPtr->next = NULL;
    newSocketPtr->prev = NULL;
  }
  else
  {
    /*if the removed socket is the first one in the busyList*/
    if (newSocketPtr == *headerSocketPPtr)
    {
      *headerSocketPPtr = newSocketPtr->next;
    }

    newSocketPtr->prev->next = newSocketPtr->next;
    newSocketPtr->next->prev = newSocketPtr->prev;
    newSocketPtr->prev = NULL;
    newSocketPtr->next = NULL;
  }

 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值