• <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>

            A Za, A Za, Fighting...

            堅信:勤能補拙

            帶隨機指針的鏈表復制

            題目來源:
            http://zhedahht.blog.163.com/blog/static/254111742010819104710337/

            題目:有一個復雜鏈表,其結點除了有一個m_pNext指針指向下一個結點外,還有一個m_pSibling指向鏈表中的任一結點或者NULL。其結點的C++定義如下:

                            struct ComplexNode

            {

                int m_nValue;

                ComplexNode* m_pNext;

                ComplexNode* m_pSibling;

            };


            代碼:
            #include<stdio.h>
            #include
            <stdlib.h>
            #include
            <string.h>

            struct Node {
                
            char value;

                
            struct Node *next;
                
            struct Node *random;
            };

            void test_print(struct Node *);

            struct Node *
            list_copy_with_random_pointer(
            struct Node *head)
            {
                
            struct Node *tmp, *ptr, *ret;

                ptr 
            = head;
                
            while(ptr != NULL) {
                    tmp 
            = (struct Node *)malloc(sizeof(struct Node));
                    tmp
            ->value = (ptr->value)-32/* from lowercase to uppercase, just for testing */
                    tmp
            ->next = ptr->next;
                    tmp
            ->random = NULL;

                    ptr
            ->next = tmp;

                    ptr 
            = ptr->next->next;
                }

                ptr 
            = head;
                
            while(ptr != NULL) {
                    ptr
            ->next->random = ptr->random==NULL ? NULL : ptr->random->next;

                    ptr 
            = ptr->next->next;
                }

                ptr 
            = head;
                ret 
            = (head==NULL ? NULL : (head->next));
                
            while(ptr != NULL) {
                    tmp 
            = ptr->next;
                    ptr
            ->next = ptr->next->next;
                    tmp
            ->next = ptr->next==NULL ? NULL : ptr->next->next;

                    ptr 
            = ptr->next;
                }

                
            return ret;
            }

            void
            test_print(
            struct Node *head)
            {
                
            while(head != NULL) {
                    printf(
            "%c: [%c, %c]\n", head->value, head->next==NULL?'-':head->next->value, head->random==NULL?'-':head->random->value);

                    head 
            = head->next;
                }
            }

            int
            main(
            int argc, char **argv)
            {
                
            struct Node d = {'d', NULL, NULL};
                
            struct Node c = {'c'&d, NULL};
                
            struct Node b = {'b'&c, NULL};
                
            struct Node a = {'a'&b, NULL};
                a.random 
            = &c;
                d.random 
            = &b;

                test_print(
            &a);

                
            struct Node *copy = list_copy_with_random_pointer(&a);

                printf(
            "\n\n");
                test_print(
            &a);
                printf(
            "\n\n");
                test_print(copy);

                
            return 0;
            }

            posted on 2011-06-09 11:35 simplyzhao 閱讀(383) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導航

            <2011年6月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品美女久久久免费| 精品人妻伦一二三区久久| 18岁日韩内射颜射午夜久久成人| 亚洲国产精品无码久久久秋霞2| 一本色道久久99一综合| 精品久久久久久无码专区| 国产精品美女久久久久| 91精品国产9l久久久久| 久久精品无码一区二区三区免费| 久久无码人妻精品一区二区三区 | 久久久精品波多野结衣| 久久精品中文字幕一区| 久久人搡人人玩人妻精品首页| 日韩va亚洲va欧美va久久| 新狼窝色AV性久久久久久| 久久久久免费看成人影片| 日日狠狠久久偷偷色综合免费| 精品无码久久久久久久动漫| 久久久国产亚洲精品| 国产日产久久高清欧美一区| 伊人久久亚洲综合影院| 久久精品这里热有精品| 亚洲国产精品无码久久久不卡| 久久人人爽人人爽人人片av高请| 日韩电影久久久被窝网| 性高湖久久久久久久久| 一本伊大人香蕉久久网手机| 亚洲日本va午夜中文字幕久久| 韩国免费A级毛片久久| 亚洲AV伊人久久青青草原| 天天躁日日躁狠狠久久| 成人精品一区二区久久 | 亚洲乱码精品久久久久.. | 国产精品久久久久久久久鸭| 久久只有这里有精品4| 久久精品一区二区国产| 亚洲精品成人久久久| 91精品国产91久久久久久| 无码人妻久久一区二区三区免费丨| 久久国产成人午夜aⅴ影院| 久久狠狠高潮亚洲精品|