Chinaunix首页 | 论坛 | 博客
  • 博客访问: 692219
  • 博文数量: 192
  • 博客积分: 1875
  • 博客等级: 上尉
  • 技术积分: 2177
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 23:21
个人简介

有时候,就是想窥视一下不知道的东东,因为好奇!

文章分类

全部博文(192)

文章存档

2024年(8)

2023年(3)

2020年(1)

2019年(1)

2018年(1)

2017年(2)

2016年(69)

2015年(53)

2014年(14)

2013年(1)

2012年(5)

2011年(25)

2010年(9)

分类: LINUX

2016-01-03 21:35:14

linux自带的三种 queue接口: Lists/Tail queues/Circular queues的使用. 
每一个接口宏(非函数)都用了至少一次.

0. 环境
   Distributor ID: Ubuntu
    Description: Ubuntu 14.04.1 LTS
    Release: 14.04
    Codename: trusty
1. 代码及运行
    a. list接口代码list.c
    

点击(此处)折叠或打开

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <sys/queue.h>

  4. LIST_HEAD(listhead, entry) head;
  5. struct listhead *headp; /* List head. */
  6. struct entry {
  7.     int value;
  8.     LIST_ENTRY(entry) entries; /* List. */
  9. } *n1, *n2, *np;

  10. int main(int argc, char *argv[])
  11. {
  12.     int i = 0;
  13.     LIST_INIT(&head); /* Initialize the list. */

  14.     n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  15.     n1->value = 10000;
  16.     LIST_INSERT_HEAD(&head, n1, entries);

  17.     for(i = 0; i < 10; i++)
  18.     {
  19.         n2 = malloc(sizeof(struct entry)); /* Insert after. */
  20.         n2->value = i;
  21.         LIST_INSERT_AFTER(n1, n2, entries);
  22.     }
  23.     /* Forward traversal. */
  24.     for (np = head.lh_first; np != NULL; np = np->entries.le_next)
  25.     printf("value is %d\n", np->value);

  26.     while (head.lh_first != NULL) /* Delete. */
  27.     {
  28.         LIST_REMOVE(head.lh_first, entries);
  29.     }
  30.     return 0;
  31. }

编译/运行后结果如下:

代码简短.不解释
-------------------------------------------------
 b. TailQueue接口代码TailQueue.c

点击(此处)折叠或打开

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <sys/queue.h>

  4. TAILQ_HEAD(tailhead, entry) head;
  5. struct tailhead *headp; /* Tail queue head. */
  6. struct entry {
  7.     int value;
  8.     TAILQ_ENTRY(entry) entries; /* Tail queue. */
  9. } *n1, *n2, *np;

  10. int main(int argc, char *argv[])
  11. {
  12.     int i;
  13.     TAILQ_INIT(&head); /* Initialize the queue. */

  14.     n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  15.     n1->value = 10000;
  16.     TAILQ_INSERT_HEAD(&head, n1, entries);

  17.     for(i = 0; i < 10; i++)
  18.     {
  19.         n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
  20.         n1->value = i;
  21.         TAILQ_INSERT_TAIL(&head, n1, entries);
  22.     }

  23.     for(i = 10; i < 20; i++)
  24.     {
  25.         n2 = malloc(sizeof(struct entry)); /* Insert after. */
  26.         n2->value = i;
  27.         TAILQ_INSERT_AFTER(&head, n1, n2, entries);
  28.     }
  29.     /* Forward traversal. */
  30.     for(np = head.tqh_first; np != NULL; np = np->entries.tqe_next)
  31.     {
  32.         printf("value is %d\n", np->value);
  33.     }
  34.     /* Delete. */
  35.     while (head.tqh_first != NULL)
  36.     {
  37.         TAILQ_REMOVE(&head, head.tqh_first, entries);
  38.     }

  39.     return 0;
  40. }

编译/运行后结果如下:

代码简短.不解释
-------------------------------------------------
c. CircularQueues接口代码CircularQueues.c

点击(此处)折叠或打开

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <sys/queue.h>

  4. CIRCLEQ_HEAD(circleq, entry) head;
  5. struct circleq *headp; /* Circular queue head. */
  6. struct entry {
  7.     int value;
  8.     CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
  9. } *n1, *n2, *np;

  10. int main(int argc, char *argv[])
  11. {
  12.     int i;
  13.     CIRCLEQ_INIT(&head); /* Initialize the circular queue. */

  14.     n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  15.     n1->value = 10000;
  16.     CIRCLEQ_INSERT_HEAD(&head, n1, entries);

  17.     for(i = 0; i < 10; i++)
  18.     {
  19.         n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
  20.         n1->value = i;
  21.         CIRCLEQ_INSERT_TAIL(&head, n1, entries);
  22.     }

  23.     //for(i = 10; i < 20; i++)
  24.     for(i = 100; i < 101; i++)
  25.     {
  26.         n2 = malloc(sizeof(struct entry)); /* Insert after. */
  27.         n2->value = i;
  28.         CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
  29.     }

  30.     //for(i = 20; i < 30; i++)
  31.     for(i = 200; i < 201; i++)
  32.     {
  33.         n2 = malloc(sizeof(struct entry)); /* Insert before. */
  34.         n2->value = i;
  35.         CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
  36.     }

  37.     /* Forward traversal. */
  38.     for (np = head.cqh_first; np != (void *)&head; np = np->entries.cqe_next)
  39.     {
  40.         printf("value is %d\n", np->value);
  41.     }
  42.     printf("\n----------------------------\n");
  43.     /* Reverse traversal. */
  44.     for (np = head.cqh_last; np != (void *)&head; np = np->entries.cqe_prev)
  45.     {
  46.         printf("value is %d\n", np->value);
  47.     }
  48.     /* Delete. */
  49.     while (head.cqh_first != (void *)&head)
  50.     {
  51.         CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
  52.     }

  53.     return 0;
  54. }

编译/运行后结果如下:



代码简短.不解释
----------------------------------------------
2. 更多东东
    找"man queue"

阅读(754) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~