• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴(yán)以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            TAILQ Example

            from:http://unx.ca/log/2006/08/19/tailq-example/

            I plan to use this blog to post codebits that I find myself sending to people over and over again. The first example is how to use a TAILQ from sys/queue.h found on the BSDs. Many Linux distributions also have sys/queue.h but may be missing certain macros such as TAILQ_FOREACH(). At the very least I hope this enticies some newer C programmer to use queue.h rather than rolling their own linked list. I’ll jump directly to the code as I feel it is more or less self explanatory.

            The following code can be downloaded here.

            /*
             * 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;
            }
            

            posted on 2007-08-21 01:45 楊粼波 閱讀(1236) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久久久久九九99精品| 狠狠色丁香婷婷久久综合五月| 国内精品九九久久久精品| 久久99精品国产自在现线小黄鸭| 国产亚洲欧美成人久久片| 国产亚洲美女精品久久久| 亚洲精品国精品久久99热| 久久精品国产久精国产果冻传媒| 久久亚洲私人国产精品| 国产高清国内精品福利99久久| 亚洲国产香蕉人人爽成AV片久久| 日本久久久久亚洲中字幕| 欧美亚洲另类久久综合| 久久婷婷是五月综合色狠狠| 久久精品九九亚洲精品| 久久亚洲电影| 97久久精品人妻人人搡人人玩| 久久精品亚洲福利| 久久精品国产99久久久| 一本久久免费视频| 国产精品久久久久影视不卡| 亚洲人成网站999久久久综合| 久久精品国产99久久无毒不卡 | 久久精品一本到99热免费| av午夜福利一片免费看久久| 人人狠狠综合88综合久久| 国产国产成人精品久久| 久久久久亚洲AV成人网人人网站| 久久国产精品99精品国产987| 国内精品人妻无码久久久影院导航| 久久亚洲精品视频| 久久久久人妻一区精品色| 伊人色综合久久天天人守人婷| 国产成人精品综合久久久| 色综合久久无码中文字幕| 波多野结衣久久一区二区| 国内精品欧美久久精品| 天天久久狠狠色综合| 国产婷婷成人久久Av免费高清| 国产精品久久久久免费a∨| 久久涩综合|