NS2自身的LIST链表

 

NS2 LIST 链表

本人在看NS2源代码的时候发现有LIST_HEAD,LIST_ENTRY,LIST_INIT,LIST_REMOVE等等,经过查看源代码和上网翻阅资料,发现NS2有其自身定义的链表结构,定义在bsd-list.h中,于是写了个简短的说明,方便他人使用.由于是自己看源代码后自己理解的,如有错误,请告知

 

本文档分三部分

 

1.

2.

3.应用

 

 

1.解释如下:

 

LIST_HEAD(name, type) 是定义一个结构体name, 其中包含一个type类型的指针*lh_first,用于指向表头

LIST_ENTRY(type)用于定义两个指针, *le_next指向链表的下一个元素, **le_prev指向”address of previous next element”,即该元素在该队列中的前一个元素的le_next的地址

LIST_INIT(head) 初始化(head)->lh_first为空

LIST_INSERT_AFTER(listelm, elm, field)elm插在listelm后面

LIST_INSERT_BEFORE(listelm, elm, field)elm插在listelm前面

LIST_INSERT_HEAD(head, elm, field)elm插在表头,对于elm, le_prev指向其自身的地址”(elm)->field.le_prev = &(head)->lh_first”

其中field指的是LIST_ENTRY中定义的结构体实例

LIST_REMOVE(elm, field) 从链表中移出elm

 

2.源码如下:

 

#define LIST_HEAD(name, type)                                                    /

struct name {                                                                    /

         type *lh_first;  /* first element */                      /

}

 

#define LIST_ENTRY(type)                                                      /

struct {                                                                      /

         type *le_next; /* next element */                     /

         type **le_prev;        /* address of previous next element */  /

}

 

/*

 * List functions.

 */

#define     LIST_INIT(head) {                                                    /

         (head)->lh_first = NULL;                                       /

}

 

#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;               /

}

 

#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;               /

}

 

#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;                            /

}

 

#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;                     /

}

 

 

3.如何使用:

 

//此例创建了一个链表,名为rt_path_list, 该链表中的元素类型为AODV_path

class AODV_Path {

       

 public:

      

 

 protected:

        LIST_ENTRY(AODV_Path) path_link;// path_link相当与field

       

};

 

LIST_HEAD(aodv_paths, AODV_Path);//定义结构体

 

aodv_paths      rt_path_list;// rt_path_list相当与源代码中的head

 

//初始化

LIST_INIT(&rt_path_list);

 

//p元素后插入path元素

LIST_INSERT_AFTER(p, path, path_link);

 

//在表头插入path

LIST_INSERT_HEAD(&rt_path_list, path, path_link);

 

//path从链表中删除

LIST_REMOVE(path, path_link);

 

//获取链表表头元素

AODV_Path *path = rt_path_list.lh_first;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值