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

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲综合婷婷久久| 亚洲国产成人精品91久久久| 国产成人无码精品久久久性色| 久久久久亚洲精品无码网址| 亚洲国产成人久久综合野外| 久久免费看黄a级毛片| .精品久久久麻豆国产精品| 99久久精品免费| 亚洲精品无码久久久久久| 精品国产91久久久久久久| 亚洲国产成人久久一区久久| 99999久久久久久亚洲| 日韩精品久久久久久久电影| 99精品国产在热久久无毒不卡| 日本久久中文字幕| 亚洲国产精品久久66| 亚洲色婷婷综合久久| 久久综合久久鬼色| 国产91久久精品一区二区| 免费久久人人爽人人爽av| 久久婷婷国产麻豆91天堂| 亚洲欧美伊人久久综合一区二区| 999久久久国产精品| 久久国产热精品波多野结衣AV| 久久频这里精品99香蕉久| 国产精品九九久久免费视频| 久久久久AV综合网成人| 一本色综合网久久| 久久久久波多野结衣高潮| 久久久久亚洲精品男人的天堂| 91久久精品国产91性色也| 久久亚洲精品中文字幕三区| 久久99精品久久久久久动态图 | 久久精品蜜芽亚洲国产AV| 久久亚洲天堂| 久久久久无码国产精品不卡| 久久国产乱子伦精品免费强| 色综合久久久久无码专区| 婷婷久久久亚洲欧洲日产国码AV| 久久综合亚洲色HEZYO社区| 伊人久久大香线蕉无码麻豆|