TAILQ用法的小例子,出&LT的; SYS / queue.h&GT;图书馆 [英] Minimal example of TAILQ usage out of <sys/queue.h> library
问题描述
有人能提供TAILQ用法的小例子,从Linux系统库在C的解释,可以在Linux下使用gcc编译?
Can some one provide minimal example of TAILQ usage out of linux system library with explanation in c which can be compiled using gcc in Linux?
推荐答案
的 TAILQ_ENTRY
宏用于建立用于将项目插入列表中的指针。你把它变成你要列出你的结构。
The TAILQ_ENTRY
macro is used to establish the pointers used to insert items into the list. You place it into your structure that you want to list up.
struct foo {
TAILQ_ENTRY(foo) tailq;
int datum;
/* ... */
};
的 TAILQ_HEAD
被用来定义将作为您的链接列表元素的容器的结构。你为它提供一个结构的名称,其将包含在类型名称
The TAILQ_HEAD
is used to define a structure that will act as the container for your link list elements. You provide it with a structure name, and the name of the type that it will contain.
TAILQ_HEAD(fooq, foo);
使用 TAILQ_INIT
来初始化列表容器的一个实例。
Use TAILQ_INIT
to initialize an instance of your list container.
struct fooq q;
TAILQ_INIT(&q);
使用 TAILQ_INSERT _ *
宏添加元素。
struct foo data[3] = { foo(3), foo(7), foo(1) };
TAILQ_INSERT_HEAD(&q, &data[0], tailq);
TAILQ_INSERT_AFTER(&q, &data[0], &data[1], tailq);
TAILQ_INSERT_TAIL(&q, &data[2], tailq);
您可以使用 TAILQ_FOREACH
和 TAILQ_FOREACH_REVERSE
来遍历列表。
You can use TAILQ_FOREACH
and TAILQ_FOREACH_REVERSE
to traverse the list.
struct foo *p;
TAILQ_FOREACH(p, &q, tailq) {
printf(" %d", p->datum);
}
puts("");
如果你想遍历列表,同时消除所有的元素,它可能是更容易使用while循环,并使用 TAILQ_EMPTY
和 TAILQ_FIRST
宏。
If you want to iterate over the list while removing all its elements, it is probably easier to use a while loop and use the TAILQ_EMPTY
and TAILQ_FIRST
macros.
while (!TAILQ_EMPTY(&q)) {
p = TAILQ_FIRST(&q);
TAILQ_REMOVE(&q, p, tailq);
/* ... */
}
以上code大部分是从逐字我写了一个例子,并采取测试的 IDEONE 。
这篇关于TAILQ用法的小例子,出&LT的; SYS / queue.h&GT;图书馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!