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
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <sys/queue.h>
-
-
LIST_HEAD(listhead, entry) head;
-
struct listhead *headp; /* List head. */
-
struct entry {
-
int value;
-
LIST_ENTRY(entry) entries; /* List. */
-
} *n1, *n2, *np;
-
-
int main(int argc, char *argv[])
-
{
-
int i = 0;
-
LIST_INIT(&head); /* Initialize the list. */
-
-
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
-
n1->value = 10000;
-
LIST_INSERT_HEAD(&head, n1, entries);
-
-
for(i = 0; i < 10; i++)
-
{
-
n2 = malloc(sizeof(struct entry)); /* Insert after. */
-
n2->value = i;
-
LIST_INSERT_AFTER(n1, n2, entries);
-
}
-
/* Forward traversal. */
-
for (np = head.lh_first; np != NULL; np = np->entries.le_next)
-
printf("value is %d\n", np->value);
-
-
while (head.lh_first != NULL) /* Delete. */
-
{
-
LIST_REMOVE(head.lh_first, entries);
-
}
-
return 0;
-
}
编译/运行后结果如下:
代码简短.不解释
-------------------------------------------------
b. TailQueue接口代码TailQueue.c
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <sys/queue.h>
-
-
TAILQ_HEAD(tailhead, entry) head;
-
struct tailhead *headp; /* Tail queue head. */
-
struct entry {
-
int value;
-
TAILQ_ENTRY(entry) entries; /* Tail queue. */
-
} *n1, *n2, *np;
-
-
int main(int argc, char *argv[])
-
{
-
int i;
-
TAILQ_INIT(&head); /* Initialize the queue. */
-
-
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
-
n1->value = 10000;
-
TAILQ_INSERT_HEAD(&head, n1, entries);
-
-
for(i = 0; i < 10; i++)
-
{
-
n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
-
n1->value = i;
-
TAILQ_INSERT_TAIL(&head, n1, entries);
-
}
-
-
for(i = 10; i < 20; i++)
-
{
-
n2 = malloc(sizeof(struct entry)); /* Insert after. */
-
n2->value = i;
-
TAILQ_INSERT_AFTER(&head, n1, n2, entries);
-
}
-
/* Forward traversal. */
-
for(np = head.tqh_first; np != NULL; np = np->entries.tqe_next)
-
{
-
printf("value is %d\n", np->value);
-
}
-
/* Delete. */
-
while (head.tqh_first != NULL)
-
{
-
TAILQ_REMOVE(&head, head.tqh_first, entries);
-
}
-
-
return 0;
-
}
编译/运行后结果如下:
代码简短.不解释
-------------------------------------------------
c. CircularQueues接口代码CircularQueues.c
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <sys/queue.h>
-
-
CIRCLEQ_HEAD(circleq, entry) head;
-
struct circleq *headp; /* Circular queue head. */
-
struct entry {
-
int value;
-
CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
-
} *n1, *n2, *np;
-
-
int main(int argc, char *argv[])
-
{
-
int i;
-
CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
-
-
n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
-
n1->value = 10000;
-
CIRCLEQ_INSERT_HEAD(&head, n1, entries);
-
-
for(i = 0; i < 10; i++)
-
{
-
n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
-
n1->value = i;
-
CIRCLEQ_INSERT_TAIL(&head, n1, entries);
-
}
-
-
//for(i = 10; i < 20; i++)
-
for(i = 100; i < 101; i++)
-
{
-
n2 = malloc(sizeof(struct entry)); /* Insert after. */
-
n2->value = i;
-
CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
-
}
-
-
//for(i = 20; i < 30; i++)
-
for(i = 200; i < 201; i++)
-
{
-
n2 = malloc(sizeof(struct entry)); /* Insert before. */
-
n2->value = i;
-
CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
-
}
-
-
/* Forward traversal. */
-
for (np = head.cqh_first; np != (void *)&head; np = np->entries.cqe_next)
-
{
-
printf("value is %d\n", np->value);
-
}
-
printf("\n----------------------------\n");
-
/* Reverse traversal. */
-
for (np = head.cqh_last; np != (void *)&head; np = np->entries.cqe_prev)
-
{
-
printf("value is %d\n", np->value);
-
}
-
/* Delete. */
-
while (head.cqh_first != (void *)&head)
-
{
-
CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
-
}
-
-
return 0;
-
}
编译/运行后结果如下:
代码简短.不解释
----------------------------------------------
2. 更多东东
找"man queue"
阅读(752) | 评论(0) | 转发(0) |