• <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
            單鏈表的逆轉(zhuǎn),關(guān)鍵在于三個(gè)輔助指針,前兩個(gè)指針用于逆轉(zhuǎn),最后一個(gè)用于記錄剩下來(lái)的節(jié)點(diǎn),不然鏈表就丟失了。
            這里是用的帶頭結(jié)點(diǎn)的鏈表,考慮鏈表中沒(méi)有節(jié)點(diǎn)和只有一個(gè)節(jié)點(diǎn)的情況就不用逆轉(zhuǎn)了。

              1 #include <iostream>
              2 using namespace std;
              3 
              4 struct node
              5 {
              6     int    data;
              7     node * next;
              8 };
              9 
             10 node * init()
             11 {
             12     node * head = new node;
             13     if (head == 0)
             14     {
             15         exit(1);
             16     }
             17     head->next = 0;
             18     return head;
             19 }
             20 
             21 node * insert(int item, node * pre)
             22 {
             23     node * tmp = new node;
             24     if (tmp == 0)
             25     {
             26         exit(1);
             27     }
             28     tmp->data = item;
             29     tmp->next = pre->next;
             30     pre->next = tmp;
             31     return tmp;
             32 }
             33 
             34 node * create(node * head)
             35 {
             36     for (int i = 0; i < 10++i)
             37     {
             38         insert(i, head);
             39     }
             40     return head;
             41 }
             42 
             43 node * reverse(node * head)
             44 {
             45     node * p1 = head->next;
             46     if (p1 == 0)
             47     {
             48         return head;
             49     }
             50 
             51     node * p2 = p1->next;
             52     if (p2 == 0)
             53     {
             54         return head;
             55     }
             56 
             57     node * p3;
             58 
             59     while (p2 != 0)
             60     {
             61         p3 = p2->next;
             62         p2->next = p1;
             63         if (p1 == head->next)
             64         {
             65             p1->next = 0;
             66         }
             67         p1 = p2;
             68         p2 = p3;
             69     }
             70     head->next = p1;
             71     return head;
             72 }
             73 
             74 void clear_(node * head)
             75 {
             76     node * p = head->next, * q;
             77     while (p != 0)
             78     {
             79         q = p->next;
             80         delete p;
             81         p = q;
             82     }
             83     head->next = 0;
             84 }
             85 
             86 void clear(node * head)
             87 {
             88     clear_(head);
             89     delete head;
             90 }
             91 
             92 node * copy(node * des, node * src)
             93 {
             94     clear_(des);
             95     node * p = src->next, * q, * d = des;
             96     while (p != 0)
             97     {
             98         q = new node;
             99         q->data = p->data;
            100         q->next = 0;
            101         d->next = q;
            102         d = q;
            103         p = p->next;
            104     }
            105     return des;
            106 }
            107 
            108 void print(node * head)
            109 {
            110     node * p = head->next;
            111     while (p != 0)
            112     {
            113         cout << p->data << ' ';
            114         p = p->next;
            115     }
            116     cout << endl;
            117 }
            118 
            119 int main()
            120 {
            121     node * t1 = init();
            122     create(t1);
            123     print(t1);
            124 
            125     node * t2 = init();
            126     copy(t2, t1);
            127     print(t2);
            128     reverse(t2);
            129     print(t2);
            130 
            131     return 0;
            132 }
            posted on 2011-04-27 13:48 unixfy 閱讀(475) 評(píng)論(0)  編輯 收藏 引用
            久久精品国产亚洲AV影院| 青青青伊人色综合久久| 精品久久久久久久久午夜福利| 久久国产精品成人片免费| 精品久久久久久久无码| 国产99久久久国产精免费| 久久精品免费大片国产大片| 久久久受www免费人成| 国产精品美女久久久久| 久久成人18免费网站| 久久国产欧美日韩精品| 999久久久免费国产精品播放| 一本久道久久综合狠狠爱| 欧美日韩精品久久久久| 99久久这里只有精品| 亚洲va国产va天堂va久久| 久久伊人精品一区二区三区| 国产精品成人精品久久久| 一本色道久久99一综合| 亚洲国产精品综合久久网络 | 亚洲精品美女久久久久99小说| 久久午夜伦鲁片免费无码| 亚洲香蕉网久久综合影视| 国产精品久久久天天影视香蕉| 久久亚洲美女精品国产精品| 无码人妻久久一区二区三区蜜桃 | 久久亚洲春色中文字幕久久久 | 亚洲愉拍99热成人精品热久久| 久久99热这里只有精品国产| 国产韩国精品一区二区三区久久| 精品国产青草久久久久福利| 国产99久久久国产精品小说| 欧美亚洲另类久久综合婷婷 | 久久大香香蕉国产| 日韩精品久久久久久免费| 伊人久久大香线蕉综合影院首页| 天天做夜夜做久久做狠狠| 人妻丰满?V无码久久不卡| 久久精品国产亚洲7777| 人妻丰满?V无码久久不卡| 伊人色综合九久久天天蜜桃|