queue.h usage

转载 2016年08月29日 17:21:20

from http://jiangpingyu.blog.163.com/blog/static/55453685200831032237166/

/*
 * TAILQ example program.
 */
#include <stdlib.h>
#include <stdio.h>
/*
 * On many OpenBSD/NetBSD/FreeBSD you could include <sys/queue.h>, but
 * for portability we’ll include the local copy.
 */
#include "queue.h"
/*
 * This structure defines each item in our tail queue.  It must also
 * contain an item (TAILQ_ENTRY) that points to the next and previous
 * items in the tail queue.
 *
 * For simplicity, we will be creating a list of integers.
 */
struct tailq_entry {
int value;
/*
         * This holds the pointers to the next and previous entries in
         * the tail queue.
         */
TAILQ_ENTRY(tailq_entry) entries;
};
/*
 * Our tail queue requires a head, this is defined using the
 * TAILQ_HEAD macro.
 */
TAILQ_HEAD(, tailq_entry) my_tailq_head;
int
main(int argc, char **argv)
{
/* Define a pointer to an item in the tail queue. */
struct tailq_entry *item;
/* In some cases we have to track a temporary item. */
struct tailq_entry *tmp_item;
int i;
/* Initialize the tail queue. */
TAILQ_INIT(&my_tailq_head);
/* Add 10 items to the tailq queue. */
for (i = 0; i < 10; i++) {
/*
                 * Each item we want to add to the tail queue must be
                 * allocated.
                 */
item = malloc(sizeof(*item));
if (item == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
/* Set the value. */
item->value = i;
/*
                 * Add our item to the end of tail queue. The first
                 * argument is a pointer to the head of our tail
                 * queue, the second is the item we want to add, and
                 * the third argument is the name of the struct
                 * variable that points to the next and previous items
                 * in the tail queue.
                 */
TAILQ_INSERT_TAIL(&my_tailq_head, item, entries);
}
/* Traverse the tail queue forward. */
printf("Forward traversal: ");
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("n");
/* Insert a new item after the item with value 5. */
printf("Adding new item after 5: ");
TAILQ_FOREACH(item, &my_tailq_head, entries) {
if (item->value == 5) {
struct tailq_entry *new_item =
malloc(sizeof(*new_item));
if (new_item == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
new_item->value = 10;
TAILQ_INSERT_AFTER(&my_tailq_head, item, new_item,
entries);
break;
}
}
/* Do another forward traversal to show the newly added item. */
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("n");
/*
         * Delete the item with the value 3.
         *
         * We can’t use TAILQ_FOREACH here as TAILQ_FOREACH is not
         * safe against deletions during the traversal.  Some variants
         * of queue.h have TAILQ_FOREACH_MUTABLE or TAILQ_FOREACH_SAFE
         * which are safe against deletions.
         */
printf("Deleting item with value 3: ");
for (item = TAILQ_FIRST(&my_tailq_head); item != NULL; item = tmp_item)
{
tmp_item = TAILQ_NEXT(item, entries);
if (item->value == 3) {
/* Remove the item from the tail queue. */
TAILQ_REMOVE(&my_tailq_head, item, entries);
/* Free the item as we don’t need it anymore. */
free(item);
break;
}
}
/* Another forward traversal to show that the value 3 is now gone. */
TAILQ_FOREACH(item, &my_tailq_head, entries) {
printf("%d ", item->value);
}
printf("n");
/* Free the entire tail queue. */
while (item = TAILQ_FIRST(&my_tailq_head)) {
TAILQ_REMOVE(&my_tailq_head, item, entries);
free(item);
}
/* The tail queue should now be empty. */
if (!TAILQ_EMPTY(&my_tailq_head))
printf("tail queue is NOT empty!n");
return 0;
}

相关文章推荐

queue.h usage (二)

 【转】http://bachmozart.javaeye.com/blog/292836 这两天想看看memcached的实现,所以先学习了libevent,使用起来还是比较简单的,其实是对sele...

【DataStructure】One of queue usage: Simulation System

Statements: This blog was written by me, but most of content  is quoted from book【Data Structure wit...

sys/queue.h分析

[转自:@astrotycoon](http://blog.csdn.net/astrotycoon/article/details/42917367) 这两天有兴趣学习使用了下系统头文件s...

queue.h的链表使用

from  https://segmentfault.com/a/1190000000596749 在FreeBSD中有queue.h这样一个头文件(Linux也有),它定义了一系列的宏操作...

Libevent0.1学习之queue.h

最近打算研究一下libevent网络库,发现用了FreeBSD的queue.h中的尾队列来存储各种事件队列。下面就是一些学习queue.h的一些笔记。 queue.h中定义了一系列的宏操作去实现单链表...
  • LYH66
  • LYH66
  • 2015年06月02日 13:44
  • 1533

linux内核list代码queue.h实现“头插法”

linux内核list代码queue.h/** @addtogroup M_COMMON_QUEUE 队列库 @par 1. 功能概述队列公共库函数提供了几类不同类型的队列的操作函数。可提供队列初...

stl_queue.h

stl_queue.h // Filename: stl_queue.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // B...

UESTC 2016 Summer Training #1 Div.2 H - Queue (A) 贪心

贪心 每次找钱,都是优先使用 大票, 因为小票具有大额票的所有功能, 而且具有大额票所不具有的功能, 所以每次优先使用大额飘 //由于用了自己的一键测试多组数据的版 //!前面有些数据没有重置, 白白...

使用linux中标准的queue.h头文件插件链表,队列,循环队列

#include #include #include #include int test_list() {     LIST_HEAD(listhead, entry)...

queue.h之tailq.h尾队列理解使用

一、连接、组织方式 如图: 每个entry有两个关键元素:tqe_next(简称为next)、tqe_prev(简称为prev)。next指向下个entry的地址,prev指向上个entry的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:queue.h usage
举报原因:
原因补充:

(最多只允许输入30个字)