AODV链表

原文地址:http://blog.youkuaiyun.com/ise_gaoyue1990/article/details/7625709

 

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

  1. #define LIST_HEAD(name, type)                       \   
  2. struct name {                               \  
  3.     type *lh_first; /* first element */         \  
  4. }  
#define LIST_HEAD(name, type)						\
struct name {								\
	type *lh_first;	/* first element */			\
}

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

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

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

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

  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除去  
#define	LIST_INIT(head) {	\//	这个操作是将链表初始为空。
#define LIST_INSERT_AFTER(listelm, elm, field) {\
//这个操作实际是将elm插入到listelm后。
#define LIST_INSERT_BEFORE(listelm, elm, field) 		\
//功能时将elm插入到listelm前面

#define LIST_INSERT_HEAD(head, elm, field)
//功能时将elm插入到表头
#define LIST_REMOVE(elm, field)
//功能时将elm除去

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

  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. };  
class AODV_Precursor {
        friend class AODV;
        friend class aodv_rt_entry;
 public:
        AODV_Precursor(u_int32_t a) { pc_addr = a; }

 protected:
        LIST_ENTRY(AODV_Precursor) pc_link;
        nsaddr_t        pc_addr;	// precursor address
};

  1. LIST_HEAD(aodv_precursors, AODV_Precursor);  
LIST_HEAD(aodv_precursors, AODV_Precursor);

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


  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. }  
AODV_Precursor*
aodv_rt_entry::pc_lookup(nsaddr_t id)
{
AODV_Precursor *pc = rt_pclist.lh_first;
 for(; pc; pc = pc->pc_link.le_next) {
   if(pc->pc_addr == id)
   	return pc;
 }
 return NULL;
}

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

  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. }  
void
aodv_rt_entry::pc_delete(nsaddr_t id) {
AODV_Precursor *pc = rt_pclist.lh_first;
 for(; pc; pc = pc->pc_link.le_next) {
   if(pc->pc_addr == id) {
     LIST_REMOVE(pc,pc_link);
     delete pc;
     break;
   }
 }
}

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值