终于看懂了难懂的NS2链表

ns2 里最常用的宏链表定义在文件 ns-2.31/lib/bsd-list.h 中,其功能很是大 下边是详细代码分析

// 头节点宏

// 链表单指针头节点结构体宏

#define LIST_HEAD(name, type)

struct name {

type *lh_first;

}


// 链表实体用于连接的指针部分、 可在某个类的定义中声明该结构题的实体,就可以用此链表连接此类的实体了

#define LIST_ENTRY(type)

struct {

type *le_next;

type **le_prev;

// le_next 指向下一个节点, le_prev 指向前面节点的 “ next element” 的地址,“ next elemnet “ 为 lh_first 或者 le_next 这样用发就同一了接口,实在是高

}

// 初始化连表 struct name * head

#define LIST_INIT(head) {

(head)->lh_first = NULL;

}

 

// 尾插法 elm 插入到 listelm 后 使用的指针在 field 

#define LIST_INSERT_AFTER(listelm, elm, field) {

if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)

(listelm)->field.le_next->field.le_prev =

&(elm)->field.le_next;

(listelm)->field.le_next = (elm);

(elm)->field.le_prev = &(listelm)->field.le_next;

}

// 前插法 , elm 插入到 listelm 

#define LIST_INSERT_BEFORE(listelm, elm, field) {

(elm)->field.le_prev = (listelm)->field.le_prev;

(elm)->field.le_next = (listelm);

*(listelm)->field.le_prev = (elm);

(listelm)->field.le_prev = &(elm)->field.le_next;

}

// 头插法 elm 插入到 head 后边

#define LIST_INSERT_HEAD(head, elm, field) {

if (((elm)->field.le_next = (head)->lh_first) != NULL)

(head)->lh_first->field.le_prev = &(elm)->field.le_next;

(head)->lh_first = (elm);

(elm)->field.le_prev = &(head)->lh_first; // 指向指针的指针

}

// 删除 elm ,由于是双向链表,参数只有两个就可以了

#define LIST_REMOVE(elm, field) {

if ((elm)->field.le_next != NULL)

(elm)->field.le_next->field.le_prev =

(elm)->field.le_prev;

*(elm)->field.le_prev = (elm)->field.le_next;

}


转自CSDN:http://blog.csdn.net/piyajee/article/details/5903382

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值