Chinaunix首页 | 论坛 | 博客
  • 博客访问: 94793
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-24 22:04
文章分类

全部博文(31)

文章存档

2014年(31)

我的朋友

分类: C/C++

2014-05-26 10:40:45




SIMPLEQ队列图例:




点击(此处)折叠或打开

  1. /*
  2.  * Simple queue definitions.
  3.  */
  4. #define SIMPLEQ_HEAD(name, type)                    \
  5. struct name {                                \
  6.     struct type *sqh_first;    /* first element */            \
  7.     struct type **sqh_last;    /* addr of last next element */        \
  8. }
展开SIMPLEQ_HEAD(event_list,event)
struct event_list{
    struct event *sqh_first;
    struct event **sqh_last;
}
这样就声明了一个SIMPLEQ的一个头节点event_list,包括一个指向event的指针和一个指向event指针的指针(存放的是指向event指针的地址)
sqh_first指向SIMPLEQ的第一个元素,而sqh_last指向SIMPLEQ的最后一个元素




点击(此处)折叠或打开

  1. #define SIMPLEQ_HEAD_INITIALIZER(head)                    \    //这里的head是一个链表头节点,而不是指向头节点的指针
  2.     { NULL, &(head).sqh_first }
展开SIMPLEQ_HEAD_INITIALIZER(&pEventBase->eventqueue) {  NULL, &(pEventBase->eventqueue).sqh_first }
把头节点eventqueue的sqh_first置为NULL,让sqh_last指向sqh_first


点击(此处)折叠或打开

  1. #define SIMPLEQ_ENTRY(type)                        \
  2. struct {                                \
  3.     struct type *sqe_next;    /* next element */            \
  4. }
展开SIMPLEQ_ENTRY(event)
struct {
    struct event *sqe_next;
}
该结构具有一个指向下一个event结构的指针,可以把该结构放入一个struct中,利用ENTRY把各个struct连接起来



点击(此处)折叠或打开

  1. /*
  2.  * Simple queue access methods.
  3.  */
  4. #define    SIMPLEQ_FIRST(head)     ((head)->sqh_first)
展开SIMPLE_FIRST(&pEventBase->eventqueue)  ((&pEventBase->eventqueue)->sqh_first)   //访问SIMPLEQ的第一个元素


点击(此处)折叠或打开

  1. #define    SIMPLEQ_END(head)     NULL
返回SIMPLEQ的末尾,总是为NULL



点击(此处)折叠或打开

  1. #define    SIMPLEQ_EMPTY(head)     (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
展开SIMPLE_EMPTY(&pEventBase->eventqueue)  (   ((&pEventBase->eventqueue)->sqh_first)  == NULL  )


点击(此处)折叠或打开

  1. #define    SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
展开SIMPLEQ_NEXT(pEvent, event_next)  ((pEvent)->event_next.sqe_next)
访问下一个event节点



点击(此处)折叠或打开

  1. #define SIMPLEQ_FOREACH(var, head, field)                \
  2.     for((var) = SIMPLEQ_FIRST(head);                \
  3.      (var) != SIMPLEQ_END(head);                    \
  4.      (var) = SIMPLEQ_NEXT(var, field))
展开SIMPLEQ_FOREACH(var, head, field)               
    for((var) = SIMPLEQ_FIRST(head);                \
     (var) != SIMPLEQ_END(head);                    
    (var) = SIMPLEQ_NEXT(var, field))
SIMPLEQ_FOREACH(pTmp,&pEventBase->eventquue,event_next)
    for((pTmp) = SIMPLEQ_FIRST(&pEventBase->eventqueue);
    (pTmp) != NULL;
    (pTmp) = SIMPLE_NEXT(pTmp,event_next))
继续展开
    for((pTmp) = ((&pEventBase->eventqueue)->sqh_first);
    (pTmp) != NULL;
    (pTmp) = ((pTmp)->event_next.sqe_next)  )




点击(此处)折叠或打开

  1. /*
  2.  * Simple queue functions.
  3.  */
  4. #define    SIMPLEQ_INIT(head) do {                        \
  5.     (head)->sqh_first = NULL;                    \
  6.     (head)->sqh_last = &(head)->sqh_first;                \
  7. } while (0)
展开SIMPLEQ_INIT(&pEventBase->eventqueue) 
do{
    (&pEventBase->eventqueue)->sqh_first = NULL;    //让头节点的first指针指向NULL
    (&pEventBase->eventqueue)->sqh_last = &(&pEventBase->eventqueue)->sqh_first;    //让头节点的last指针指向头节点的first指针
}


点击(此处)折叠或打开

  1. #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {            \ //在head后插入一个元素
  2.     if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)    \ //head的first指针赋给ele的next指针
  3.         (head)->sqh_last = &(elm)->field.sqe_next;        \ //
  4.     (head)->sqh_first = (elm);                    \
  5. } while (0)
初始状态:

执行后:


图中的1,2,3分别对应:
1. ((elm)->field.sqe_next = (head)->sqh_first)     //  让elm的next指针指向head的first指针所指元素
2. (head)->sqh_last = &(elm)->field.sqe_next;     // 让head的last指针指向elm的next指针
3.  (head)->sqh_first = (elm);                    // 让head的fist指针指向elm所指元素


点击(此处)折叠或打开

  1. #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {            \在SIMPLEQ尾部插入一个元素
  2.     (elm)->field.sqe_next = NULL;                    \
  3.     *(head)->sqh_last = (elm);                    \
  4.     (head)->sqh_last = &(elm)->field.sqe_next;            \
  5. } while (0)
初始状态:


执行后:




图中的1,2,3分别对应:
  1. (elm)->field.sqe_next = NULL;                    //让elm的next指针指向NULL
  2. *(head)->sqh_last = (elm);                       //让最后一个元素的next指针指向elm所指元素, *(head)->sqh_last可以访问到最后一个元素的next指针,(head)->sqh_last可以得到最后一个元素的next指针的地址,*                                                                 //(head)->sqh_last则获得最后一个元素的next指针
  3. (head)->sqh_last = &(elm)->field.sqe_next;       //让头节点的last指针指向elm元素的next指针



点击(此处)折叠或打开

  1. #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {        \    //在listelm后插入elm
  2.     if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  3.         (head)->sqh_last = &(elm)->field.sqe_next;        \
  4.     (listelm)->field.sqe_next = (elm);                \
  5. } while (0)
初始状态:


执行后:



图中的1,2,3分别对应:
1. ((elm)->field.sqe_next = (listelm)->field.sqe_next)    //让elm的next指针指向listelm的next指针
2. (head)->sqh_last = &(elm)->field.sqe_next;           //让head的last指针指向elm的next指针
3. (listelm)->field.sqe_next = (elm);                   //让listelm的next指针指向elm所指元素


点击(此处)折叠或打开

  1. #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do {            \ //移除head后的第一个元素
  2.     if (((head)->sqh_first = (elm)->field.sqe_next) == NULL)    \
  3.         (head)->sqh_last = &(head)->sqh_first;            \
  4. } while (0)
初始状态:


执行后:



1. ((head)->sqh_first = (elm)->field.sqe_next)    //让head的first指针指向elm的next指针
2. (head)->sqh_last = &(head)->sqh_first;       //让head指针的last指针指向head的next指针








阅读(1662) | 评论(0) | 转发(0) |
0

上一篇:queue.h(List)

下一篇:queue.h(Tail queue)

给主人留下些什么吧!~~