AODV链表

来源:互联网 发布:科技感 设计 知乎 编辑:程序博客网 时间:2024/06/13 21:37

原文地址:http://blog.csdn.net/ise_gaoyue1990/article/details/7625709

 

ns2中的链表定义在ns\lib\bsd-list.h下面,下面看一下里面的代码:

[cpp] view plaincopyprint?
  1. #define LIST_HEAD(name, type)                       \  
  2. struct name {                               \  
  3.     type *lh_first; /* first element */         \  
  4. }  

定义了一个结构体name和一个type类型的*lh_first指针,指向结构体的头。name是链表的名字,元素类型必须是type类型。

[cpp] view plaincopyprint?
  1. #define LIST_ENTRY(type)                        \  
  2. struct {                                \  
  3.     type *le_next;  /* next element */          \  
  4.     type **le_prev; /* address of previous next element */  \  
  5. }  

定义了两个type类型的指针,*le_next表示链表的下一个元素,**le_prev表示之前下一个元素的地址。这个结构体入口处只给了元素是type类型,并未给出结构体的名字,这在之后会介绍到。

下面是关于链表的一些具体操作

[cpp] view plaincopyprint?
  1. #define LIST_INIT(head) {   \// 这个操作是将链表初始为空。  
  2. #define LIST_INSERT_AFTER(listelm, elm, field) {\  
  3. //这个操作实际是将elm插入到listelm后。   
  4. #define LIST_INSERT_BEFORE(listelm, elm, field)         \  
  5. //功能时将elm插入到listelm前面   
  6.   
  7. #define LIST_INSERT_HEAD(head, elm, field)  
  8. //功能时将elm插入到表头   
  9. #define LIST_REMOVE(elm, field)  
  10. //功能时将elm除去  

下面来看看AODV前缀列表这个链表:

[cpp] view plaincopyprint?
  1. class AODV_Precursor {  
  2.         friend class AODV;  
  3.         friend class aodv_rt_entry;  
  4.  public:  
  5.         AODV_Precursor(u_int32_t a) { pc_addr = a; }  
  6.   
  7.  protected:  
  8.         LIST_ENTRY(AODV_Precursor) pc_link;  
  9.         nsaddr_t        pc_addr;    // precursor address  
  10. };  

[cpp] view plaincopyprint?
  1. LIST_HEAD(aodv_precursors, AODV_Precursor);  

LIST_HEAD中,aodv_precursors表示结构体的名字,AODV_Precursor表示结构体中的指针变量。在LIST_ENTRY中,AODV_Precursor表示指针变量,pc_link表示指针结构体的名字,因为在LIST_ENTRY中没有定义结构体变量的名字,上面的field域就是指的pc_link。接下来看一下前缀链表的操作。


[cpp] view plaincopyprint?
  1. AODV_Precursor*  
  2. aodv_rt_entry::pc_lookup(nsaddr_t id)  
  3. {  
  4. AODV_Precursor *pc = rt_pclist.lh_first;  
  5.  for(; pc; pc = pc->pc_link.le_next) {  
  6.    if(pc->pc_addr == id)  
  7.     return pc;  
  8.  }  
  9.  return NULL;  
  10. }  

这个函数就是遍历链表,若找到满足条件则返回真;否则返回假。

[cpp] view plaincopyprint?
  1. void  
  2. aodv_rt_entry::pc_delete(nsaddr_t id) {  
  3. AODV_Precursor *pc = rt_pclist.lh_first;  
  4.  for(; pc; pc = pc->pc_link.le_next) {  
  5.    if(pc->pc_addr == id) {  
  6.      LIST_REMOVE(pc,pc_link);  
  7.      delete pc;  
  8.      break;  
  9.    }  
  10.  }  
  11. }  

这个表示删除链表中的一个元素,遍历后,调用LIST_REMOVE(elm, field)



原创粉丝点击