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

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品成人久久久久三级午夜电影| 久久强奷乱码老熟女| 国产精品久久国产精麻豆99网站| 狠狠色丁香婷婷综合久久来| 亚洲一区二区三区日本久久九| 久久乐国产精品亚洲综合| 国产三级观看久久| 99久久精品免费看国产一区二区三区| 久久精品国产影库免费看| 久久久青草久久久青草| 亚洲精品美女久久久久99| 久久99亚洲综合精品首页| 久久偷看各类wc女厕嘘嘘| 久久亚洲精品无码播放| 亚洲精品国精品久久99热一| 国产精品久久久久久一区二区三区| 久久se精品一区精品二区国产| 亚洲国产视频久久| 国产精品99久久久久久猫咪| 怡红院日本一道日本久久 | 久久er热视频在这里精品| 久久亚洲国产精品五月天婷| 9久久9久久精品| 成人久久免费网站| 亚洲AⅤ优女AV综合久久久| 91久久精品国产91性色也| 亚洲AV乱码久久精品蜜桃| 亚洲国产成人久久一区久久| 精品国产乱码久久久久久浪潮| 人妻无码久久一区二区三区免费| 久久97久久97精品免视看秋霞 | 久久天天躁狠狠躁夜夜网站| 久久精品女人天堂AV麻| 人妻无码精品久久亚瑟影视| 久久精品国产精品青草app| 久久99久国产麻精品66| 丰满少妇高潮惨叫久久久| 亚洲综合婷婷久久| 久久久久国产精品| 99久久精品免费看国产| 韩国免费A级毛片久久|