# queue.h usage

81人阅读 评论(0)

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
*/
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. */
/* 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.
*/
}
/* Traverse the tail queue forward. */
printf("Forward traversal: ");
printf("%d ", item->value);
}
printf("n");
/* Insert a new item after the item with value 5. */
printf("Adding new item after 5: ");
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;
entries);
break;
}
}
/* Do another forward traversal to show the newly added item. */
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. */
/* 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. */
printf("%d ", item->value);
}
printf("n");
/* Free the entire tail queue. */
free(item);
}
/* The tail queue should now be empty. */
printf("tail queue is NOT empty!n");
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：168472次
• 积分：2341
• 等级：
• 排名：第16023名
• 原创：17篇
• 转载：309篇
• 译文：0篇
• 评论：6条
文章分类
评论排行
最新评论