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

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久99热这里只有精品国产| 久久久久久狠狠丁香| 亚洲国产精品成人久久蜜臀| 色婷婷噜噜久久国产精品12p| 日本福利片国产午夜久久| 欧美777精品久久久久网| 武侠古典久久婷婷狼人伊人| 亚洲精品成人网久久久久久| 欧美日韩中文字幕久久久不卡| 中文字幕久久亚洲一区| 久久久久久夜精品精品免费啦| 午夜人妻久久久久久久久| 麻豆亚洲AV永久无码精品久久| a级毛片无码兔费真人久久| 亚洲中文久久精品无码ww16 | 精品久久无码中文字幕| 久久久精品波多野结衣| 国产精品禁18久久久夂久| 亚洲国产视频久久| 久久久久亚洲精品无码网址| 国产精品视频久久久| 久久免费的精品国产V∧| 国内精品伊人久久久影院| 国产—久久香蕉国产线看观看 | 99精品国产在热久久无毒不卡| 色狠狠久久综合网| 久久久久国产精品嫩草影院| 亚洲国产精品久久久久婷婷软件 | 久久久噜噜噜久久| 中文精品久久久久国产网址 | 99久久婷婷国产综合精品草原| 国产精品久久久久AV福利动漫| 色诱久久久久综合网ywww| 久久国语露脸国产精品电影| 久久久久高潮综合影院| 久久久久精品国产亚洲AV无码| 无码人妻久久一区二区三区蜜桃| 久久青青草原精品国产不卡| 久久这里只有精品视频99| 亚洲国产一成久久精品国产成人综合| 久久精品国产亚洲5555|