• <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>
            posts - 183,  comments - 10,  trackbacks - 0

            合并兩個有序的單鏈表

            http://www.shnenglu.com/jake1036/archive/2011/06/15/148692.html

              1 #include <iostream>
              2 using namespace std;
              3 
              4 struct node
              5 {
              6     int data;
              7     node* next;
              8 };
              9 
             10 void clear(node* head)
             11 {
             12     node* t = head->next, * p;
             13     while (t != 0)
             14     {
             15         p = t;
             16         t = t->next;
             17         delete p;
             18     }
             19     delete head;
             20 }
             21 
             22 void print(node* head)
             23 {
             24     while (head->next != 0)
             25     {
             26         cout << head->next->data << ' ';
             27         head = head->next;
             28     }
             29     cout << endl;
             30 }
             31 
             32 node* init()
             33 {
             34     node* ret = new node;
             35     ret->next = 0;
             36     return ret;
             37 }
             38 
             39 node* build(node* head, int item)
             40 {
             41     node* t = new node;
             42     t->next = 0;
             43     t->data = item;
             44     head->next = t;
             45     return t;
             46 }
             47 
             48 node* merge(node* h, node* h1, node* h2)
             49 {
             50     h1 = h1->next;
             51     h2 = h2->next;
             52     node* t;
             53     while (h1 != 0 && h1 != 0)
             54     {
             55         if (h1->data < h2->data)
             56         {
             57             t = new node;
             58             t->data = h1->data;
             59             t->next = 0;
             60             h->next = t;
             61             h = h->next;
             62 
             63             h1 = h1->next;
             64         }
             65         else if (h1->data > h2->data)
             66         {
             67             t = new node;
             68             t->data = h2->data;
             69             t->next = 0;
             70             h->next = t;
             71             h = h->next;
             72 
             73             h2 = h2->next;
             74         }
             75         else
             76         {
             77             t = new node;
             78             t->data = h1->data;
             79             t->next = 0;
             80             h->next = t;
             81             h = h->next;
             82             t = new node;
             83             t->data = h2->data;
             84             t->next = 0;
             85             h->next = t;
             86             h = h->next;
             87 
             88             h1 = h1->next;
             89             h2 = h2->next;
             90         }
             91     }
             92     while (h1 != 0)
             93     {
             94         t = new node;
             95         t->data = h1->data;
             96         t->next = 0;
             97         h->next = t;
             98         h = h->next;
             99 
            100         h1 = h1->next;
            101     }
            102     while (h2 != 0)
            103     {
            104         t = new node;
            105         t->data = h2->data;
            106         t->next = 0;
            107         h->next = t;
            108         h = h->next;
            109 
            110         h2 = h2->next;
            111     }
            112 
            113     return h;
            114 }
            115 
            116 int main()
            117 {
            118     node* h1 = init(), * h2 = init();
            119     node* t = h1;
            120     for (int i = 1; i <= 10; i += 2)
            121     {
            122         t = build(t, i);
            123     }
            124     t = h2;
            125     for (int i = 0; i <= 10; i += 2)
            126     {
            127         t = build(t, i);
            128     }
            129     print(h1);
            130     print(h2);
            131 
            132     node* h = init();
            133 
            134     merge(h, h1, h2);
            135 
            136     print(h);
            137 
            138     clear(h1);
            139     clear(h2);
            140     clear(h);
            141 }

             


            posted on 2011-07-31 18:25 unixfy 閱讀(579) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产精品久久99| 91精品国产色综久久| 国产精品成人99久久久久91gav| 综合久久国产九一剧情麻豆| 深夜久久AAAAA级毛片免费看| 亚洲国产精品热久久| 国内精品久久久久影院日本| 久久天天躁狠狠躁夜夜avapp| 99久久99久久精品免费看蜜桃| 亚洲国产精品成人久久| 国产精品久久久天天影视| 色婷婷久久综合中文久久蜜桃av| 天天爽天天狠久久久综合麻豆 | 日日狠狠久久偷偷色综合免费 | 亚洲欧洲久久久精品| 亚洲精品无码久久久影院相关影片| 性做久久久久久久久浪潮| 日本人妻丰满熟妇久久久久久| 久久99精品久久久久子伦| 狠狠狠色丁香婷婷综合久久五月 | 亚洲国产精品一区二区久久hs| 亚洲愉拍99热成人精品热久久| 人妻无码中文久久久久专区| 久久久91精品国产一区二区三区| 伊人伊成久久人综合网777| 久久久久国产精品人妻| 国产精品久久久久久久午夜片| 欧美久久综合九色综合| 少妇久久久久久被弄高潮| 9191精品国产免费久久| 色妞色综合久久夜夜| 久久久久亚洲AV成人网人人网站| 久久久久无码精品国产| 性欧美大战久久久久久久| 人妻精品久久久久中文字幕69 | 久久国产高潮流白浆免费观看| 岛国搬运www久久| 亚洲香蕉网久久综合影视| 四虎国产精品免费久久5151| 无码国内精品久久人妻麻豆按摩| 潮喷大喷水系列无码久久精品|