APR的list用法感觉比linux内核的list_head难用,网上没搜索到相关说明,记录下
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include "apr_ring.h"
-
-
#define LEN 30
-
int main( int argc, char **argv )
-
{
-
struct mydat {
-
char *name;
-
int age;
-
APR_RING_ENTRY(mydat) link;
-
APR_RING_ENTRY(mydat) rlink; /* reverse order */
-
};
-
int i;
-
struct mydat *myd, *tmp, *cur, *ep1, *epN;
-
-
APR_RING_ENTRY(mydat) head, rhead;
-
APR_RING_INIT(&head, mydat, link);
-
APR_RING_INIT(&rhead, mydat, rlink);
-
-
myd = malloc(sizeof(struct mydat) * LEN);
-
for ( i = 0; i < LEN; i++ ) {
-
myd[i].name = malloc(20);
-
sprintf(myd[i].name, "name%d", i);
-
myd[i].age = i * 10;
-
}
-
for ( i = 0; i < LEN; i++ ) {
-
APR_RING_INSERT_TAIL(&head, myd+i, mydat, link);
-
APR_RING_INSERT_HEAD(&rhead, myd+i, mydat, rlink);
-
}
-
-
APR_RING_FOREACH(cur, &head, mydat, link) {
-
//printf( "Name:%s|Age:%d\n", cur->name, cur->age );
-
}
-
printf( "reverse order:\n" );
-
APR_RING_FOREACH(cur, &rhead, mydat, rlink) {
-
//printf( "Name:%s|Age:%d\n", cur->name, cur->age );
-
}
-
-
/* remove name20 from link */
-
APR_RING_FOREACH_SAFE(cur, tmp, &head, mydat, link) {
-
if (strcmp(cur->name , "name20") == 0)
-
{
-
APR_RING_REMOVE(cur, link);
-
free(cur->name);
-
}
-
}
-
printf( "after removed\n" );
-
APR_RING_FOREACH(tmp, &head, mydat, link) {
-
printf( "Name:%s|Age:%d\n", tmp->name, tmp->age );
-
}
-
-
-
/* splice list */
-
#if 0
-
APR_RING_FOREACH(cur, &head, mydat, link) {
-
//printf( "Name:%s|Age:%d\n", cur->name, cur->age );
-
if (strcmp(cur->name , "name05") == 0)
-
epN = cur;
-
if (strcmp(cur->name , "name10") == 0)
-
APR_RING_SPLICE_BEFORE(cur, &head, epN, link) ;
-
}
-
#endif
-
-
printf( "after removed\n" );
-
APR_RING_FOREACH(tmp, &head, mydat, link) {
-
printf( "Name:%s|Age:%d\n", tmp->name, tmp->age );
-
}
-
-
/* free the entry remain in list */
-
APR_RING_FOREACH_SAFE(cur, tmp, &head, mydat, link) {
-
APR_RING_REMOVE(cur, link);
-
free(cur->name);
-
}
-
free(myd);
-
-
return 0;
-
}
阅读(1080) | 评论(0) | 转发(0) |