• contiki list 链表


    1 相关宏和数据结构

    1.1 LIST_CONCAT

    #define LIST_CONCAT2(s1, s2) s1##s2
    #define LIST_CONCAT(s1, s2) LIST_CONCAT2(s1, s2)

    该宏的作用非常简单,将s1,s2拼接在一起。其中,##起连接作用,编译器在预处理时负责将s1,s2连接在一起。比如hello_##world经过编译器预处理后就变成了hello_world了。

    1.2 LIST

    typedef void ** list_t;
    
    #define LIST(name) 
             static void *LIST_CONCAT(name,_list) = NULL; 
             static list_t name = (list_t)&LIST_CONCAT(name,_list)

    经过宏替换后,变为

    #define LIST(name) 
             static void *name_list = NULL; 
             static list_t name = (list_t)&name_list

    该宏用于申明一个链表。可以将name_list理解为链表头(且该表头元素是一个指针),将name理解为一个指向链表头的指针。需要注意的是,name_list所指向的类型必须是一个结构体。

    1.3 LIST_STRUCT

    #define LIST_STRUCT(name) 
             void *LIST_CONCAT(name,_list); 
             list_t name

    1.4 LIST_STRUCT_INIT

    #define LIST_STRUCT_INIT(struct_ptr, name)                              
        do {                                                                
           (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list));   
           (struct_ptr)->LIST_CONCAT(name,_list) = NULL;                    
           list_init((struct_ptr)->name);                                   
        } while(0)

    1.5 struct list

    struct list {
      struct list *next;
    };

    struct list只有一个指针成员next,它指向的类型依然是struct list。在Contiki中,该结构体用于用于遍历链表,我们将在后面的链表函数中看到如何使用该结构体。

    2 各链表函数

    2.1 list_init

    void
    list_init(list_t list)
    {
      *list = NULL;
    }

    作用:初始化链表
    解释

    2.2 list_head

    void *
    list_head(list_t list)
    {
      return *list;
    }

    作用:获取链表头
    解释

    2.3 list_copy

    void
    list_copy(list_t dest, list_t src)
    {
      *dest = *src;
    }

    作用:“拷贝”链表
    解释

    2.4 list_tail

    void *
    list_tail(list_t list)
    {
      struct list *l;
    
      if(*list == NULL) {
        return NULL;
      }
    
      for(l = *list; l->next != NULL; l = l->next);
    
      return l;
    }

    作用:返回链表尾部节点
    解释

    2.5 list_add

    void
    list_add(list_t list, void *item)
    {
      struct list *l;
    
      /* Make sure not to add the same element twice */
      list_remove(list, item);
    
      ((struct list *)item)->next = NULL;
    
      l = list_tail(list);
    
      if(l == NULL) {
        *list = item;
      } else {
        l->next = item;
      }
    }

    作用:在链表尾部加入节点
    解释

    2.6 list_push

    void
    list_push(list_t list, void *item)
    {
      /*  struct list *l;*/
    
      /* Make sure not to add the same element twice */
      list_remove(list, item);
    
      ((struct list *)item)->next = *list;
      *list = item;
    }

    作用:在链表头部压入节点
    解释

    2.7 list_chop

    void *
    list_chop(list_t list)
    {
      struct list *l, *r;
    
      if(*list == NULL) {
        return NULL;
      }
      if(((struct list *)*list)->next == NULL) {
        l = *list;
        *list = NULL;
        return l;
      }
    
      for(l = *list; l->next->next != NULL; l = l->next);
    
      r = l->next;
      l->next = NULL;
    
      return r;
    }

    作用:砍掉链表尾部节点,并返回该尾部节点
    解释

    2.8 list_pop

    void *
    list_pop(list_t list)
    {
      struct list *l;
      l = *list;
      if(*list != NULL) {
        *list = ((struct list *)*list)->next;
      }
    
      return l;
    }

    作用弹出链表头结点
    解释

    2.9 list_remove

    void
    list_remove(list_t list, void *item)
    {
      struct list *l, *r;
    
      if(*list == NULL) {
        return;
      }
    
      r = NULL;
      for(l = *list; l != NULL; l = l->next) {
        if(l == item) {
          if(r == NULL) {
        /* First on list */
        *list = l->next;
          } else {
        /* Not first on list */
        r->next = l->next;
          }
          l->next = NULL;
          return;
        }
        r = l;
      }
    }

    作用:删除item所指向的节点

    解释

    2.10 list_length

    int
    list_length(list_t list)
    {
      struct list *l;
      int n = 0;
    
      for(l = *list; l != NULL; l = l->next) {
        ++n;
      }
    
      return n;
    }

    作用:返回链表长度
    解释

    2.11 list_insert

    void
    list_insert(list_t list, void *previtem, void *newitem)
    {
      if(previtem == NULL) {
        list_push(list, newitem);
      } else {
    
        ((struct list *)newitem)->next = ((struct list *)previtem)->next;
        ((struct list *)previtem)->next = newitem;
      }
    }

    作用:插入链表节点
    解释

    2.12 list_item_next

    void *
    list_item_next(void *item)
    {
      return item == NULL? NULL: ((struct list *)item)->next;
    }

    作用:返回链表中item的下一个节点

  • 相关阅读:
    利用Regsvr32绕过Applocker的限制策略
    nmap 端口扫描工具
    meterpreter命令大全
    metaspolit 命令大全
    ms12_004漏洞进行渗透
    msf web_delivery模块攻击
    kail linux 系统下利用metaspolit工具渗透win7电脑
    Homebrew简介和基本使用
    android中NavigationView(Design Support)的使用
    android中Snackbar(Design Support)的使用
  • 原文地址:https://www.cnblogs.com/zym0805/p/5642419.html
Copyright © 2020-2023  润新知