Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370687
  • 博文数量: 66
  • 博客积分: 3201
  • 博客等级: 中校
  • 技术积分: 695
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-04 11:17
文章分类

全部博文(66)

文章存档

2016年(1)

2014年(1)

2012年(1)

2011年(2)

2010年(18)

2009年(42)

2008年(1)

分类: 网络与安全

2014-07-11 14:31:53

APR的list用法感觉比linux内核的list_head难用,网上没搜索到相关说明,记录下

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "apr_ring.h"

  4. #define LEN 30
  5. int main( int argc, char **argv )
  6. {
  7.         struct mydat {
  8.                 char *name;
  9.                 int age;
  10.                 APR_RING_ENTRY(mydat) link;
  11.                 APR_RING_ENTRY(mydat) rlink; /* reverse order */
  12.         };
  13.         int i;
  14.         struct mydat *myd, *tmp, *cur, *ep1, *epN;

  15.         APR_RING_ENTRY(mydat) head, rhead;
  16.         APR_RING_INIT(&head, mydat, link);
  17.         APR_RING_INIT(&rhead, mydat, rlink);

  18.         myd = malloc(sizeof(struct mydat) * LEN);
  19.         for ( i = 0; i < LEN; i++ ) {
  20.                 myd[i].name = malloc(20);
  21.                 sprintf(myd[i].name, "name%d", i);
  22.                 myd[i].age = i * 10;
  23.         }
  24.         for ( i = 0; i < LEN; i++ ) {
  25.                 APR_RING_INSERT_TAIL(&head, myd+i, mydat, link);
  26.                 APR_RING_INSERT_HEAD(&rhead, myd+i, mydat, rlink);
  27.         }

  28.         APR_RING_FOREACH(cur, &head, mydat, link) {
  29.                 //printf( "Name:%s|Age:%d\n", cur->name, cur->age );
  30.         }
  31.         printf( "reverse order:\n" );
  32.         APR_RING_FOREACH(cur, &rhead, mydat, rlink) {
  33.                 //printf( "Name:%s|Age:%d\n", cur->name, cur->age );
  34.         }

  35.         /* remove name20 from link */
  36.         APR_RING_FOREACH_SAFE(cur, tmp, &head, mydat, link) {
  37.                 if (strcmp(cur->name , "name20") == 0)
  38.                 {
  39.                         APR_RING_REMOVE(cur, link);
  40.                         free(cur->name);
  41.                 }
  42.         }
  43.         printf( "after removed\n" );
  44.         APR_RING_FOREACH(tmp, &head, mydat, link) {
  45.                 printf( "Name:%s|Age:%d\n", tmp->name, tmp->age );
  46.         }


  47.         /* splice list */
  48. #if 0
  49.         APR_RING_FOREACH(cur, &head, mydat, link) {
  50.                 //printf( "Name:%s|Age:%d\n", cur->name, cur->age );
  51.                 if (strcmp(cur->name , "name05") == 0)
  52.                         epN = cur;
  53.                 if (strcmp(cur->name , "name10") == 0)
  54.                         APR_RING_SPLICE_BEFORE(cur, &head, epN, link) ;
  55.         }
  56. #endif

  57.         printf( "after removed\n" );
  58.         APR_RING_FOREACH(tmp, &head, mydat, link) {
  59.                 printf( "Name:%s|Age:%d\n", tmp->name, tmp->age );
  60.         }

  61.         /* free the entry remain in list */
  62.         APR_RING_FOREACH_SAFE(cur, tmp, &head, mydat, link) {
  63.                 APR_RING_REMOVE(cur, link);
  64.                 free(cur->name);
  65.         }
  66.         free(myd);

  67.         return 0;
  68. }

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