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

            堅(jiān)信:勤能補(bǔ)拙

            帶隨機(jī)指針的鏈表復(fù)制

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

            題目:有一個復(fù)雜鏈表,其結(jié)點(diǎn)除了有一個m_pNext指針指向下一個結(jié)點(diǎn)外,還有一個m_pSibling指向鏈表中的任一結(jié)點(diǎn)或者NULL。其結(jié)點(diǎn)的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 閱讀(381) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導(dǎo)航

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久97久久97精品免视看秋霞| 97超级碰碰碰久久久久| 热久久国产精品| 久久精品国产黑森林| 亚洲精品美女久久777777| 精品久久8x国产免费观看| 97久久精品国产精品青草| 久久久久久久国产免费看| 亚洲国产一成人久久精品| 青青草原综合久久| 欧美日韩精品久久久免费观看 | 日本精品久久久久中文字幕| 久久久久国产精品嫩草影院| 性做久久久久久久| 亚洲欧美精品一区久久中文字幕| 国产∨亚洲V天堂无码久久久 | 奇米影视7777久久精品人人爽| 69久久精品无码一区二区| 久久久久九九精品影院| 777米奇久久最新地址| 国产A级毛片久久久精品毛片| 久久久国产精华液| 国产产无码乱码精品久久鸭| 无码国内精品久久人妻| 中文字幕精品久久| 久久久久久久久久久免费精品| 99久久国产亚洲高清观看2024| 久久99国产综合精品女同| 久久精品国产亚洲AV香蕉| 亚洲欧美日韩精品久久亚洲区 | 久久99九九国产免费看小说| 国产精品丝袜久久久久久不卡 | 国产综合精品久久亚洲| 久久久久综合网久久| 久久久久久久尹人综合网亚洲| 久久99精品久久久久久久不卡| 少妇内射兰兰久久| 亚洲国产精品无码成人片久久| 国产精品一久久香蕉国产线看| 久久亚洲精品国产亚洲老地址| 久久精品国产只有精品66|