免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 758 | 回复: 0
打印 上一主题 下一主题

NS技巧13 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-04 15:58 |只看该作者 |倒序浏览

九.怎样使用ns自己的链表
1.头文件  #include
2.初始化
1). 在节点中添加,包括了指向前驱和后继的节点。
#define LIST_ENTRY(type)                                    
struct {                                                      
    type *le_next;      /* next element */                     
    type **le_prev;     /* address of previous next element */     
}
  2).定义一个链表,链表的类型为type,表头为lh_first
    #define LIST_HEAD(name, type)                                       
struct name {                                                        
       type *lh_first;      /* first element */        
}
3)初始化链表
#define     LIST_INIT(head) {                                 
    (head)->lh_first = NULL;                                
}
4)插入节点
#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;                     
}
5)删除节点
#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;                \
}
6)一个例子
  1)定义节点类型
class MFlood_RTEntry {
    friend class MFlood_RTable;
    friend class MFlood;

public:
    MFlood_RTEntry();
    MFlood_RTEntry(nsaddr_t src,u_int32_t seq);
    bool       isNewSeq(u_int32_t seq);    // old -> false, new->true
    void        addSeq(u_int32_t seq);       // add a seqno to seqno array(rt_seqnos)
   
protected:
    LIST_ENTRY(MFlood_RTEntry) rt_link;

    nsaddr_t src_;
//  u_int32_t seq_;

    u_int32_t              rt_seqnos[REM_SEQ_COUNT]; //seqno array
    u_int32_t       max_seqno;   //max seqno
    u_int32_t       min_seqno;   //max seqno
    u_int16_t              seq_it;    // seqno's iterator
};
2)建立一个链表节点类型为MFlood_RTEntry的链表 rthead
  LIST_HEAD(, MFlood_RTEntry) rthead;
3)初始化链表rheadNULL
LIST_INIT(&rthead)
(4) 怎样使用
MFlood_RTEntry*  
MFlood_Rtable::rt_lookup(nsaddr_t id) {
  Mflood_RTEntry *rt = rthead.lh_first;//获取链表表头
  for(; rt; rt = rt->rt_link.le_next) {
         if(rt->src_ == id)
                break;
  }
  return rt;
}
5)删除节点
void
MFlood_RTable::rt_delete(nsaddr_t id) {
  MFlood_RTEntry *rt = rt_lookup(id);
  if(rt) {
         LIST_REMOVE(rt, rt_link);
         delete rt;
  }
}
6)插入节点
rt = new MFlood_RTEntry(ih->saddr(), fh->seq_);
              LIST_INSERT_HEAD(&rtable_.rthead,rt,rt_link);      


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/47073/showart_1359564.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP