2008年8月28日木曜日

sys/queue.h

の使い方覚え書き。


#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct test {
int cnt;
TAILQ_ENTRY(test) next;
};

int
main (void)
{
int x;
struct test *t, *p;
TAILQ_HEAD(, test) thead;
TAILQ_INIT(&thead);

for (x = 1; x < 20; x++) {
t = malloc(sizeof(struct test));
memset(t, 0, sizeof(struct test));
t->cnt = x;
if (x == 5)
p = t;
TAILQ_INSERT_TAIL(&thead, t, next);
}

TAILQ_FOREACH(t, &thead, next) {
printf("t->cnt:%d\n", t->cnt);
}
for (t = TAILQ_FIRST(&thead); t != NULL; t = TAILQ_NEXT(t, next)) {
printf("t->cnt:%d\n", t->cnt);
};

TAILQ_REMOVE(&thead, p, next);
for (t = TAILQ_FIRST(&thead); t != NULL; t = TAILQ_NEXT(t, next)) {
printf("t->cnt:%d\n", t->cnt);
};

for (t = TAILQ_FIRST(&thead); t != NULL; t = p) {
p = TAILQ_NEXT(t, next);
TAILQ_REMOVE(&thead, t, next);
free(t);
};

if (TAILQ_EMPTY(&thead))
printf("list is empty\n");

return 0;
}

0 件のコメント: