双向循环链表在内存分配上更容易管理,因为它可以重复利用已分配的内存.
而双向链表一般是不用的结点直接就释放了.
如果你要把数据集合看成一个环,可以顺时针走,也可以逆时针走,那你就可以用双向循环链表来描述
如果你要把数据集合看成一条链,可以双向遍历,那你就可以用双向链表来描述
如果你要把数据集合看成一个层次结构,那你可以用树来描述
如果你要把数据集合看成……,比方说,你完全想不到谁跟谁会有关系,那你可以用图来描述
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;
}
}
双向链表
最新推荐文章于 2023-12-20 13:35:53 发布