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

            大漠落日

            while(!dead) study++;
            posts - 46, comments - 126, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            單向鏈表逆序

            Posted on 2011-06-23 17:01 亂78糟 閱讀(1420) 評論(0)  編輯 收藏 引用 所屬分類: 算法&數(shù)據(jù)結(jié)構(gòu)
            /***********************************
            *    單向鏈表逆序
            *    yanzh 2011-6-23
            ***********************************
            */
            #include 
            <iostream>

            using namespace std;

            class Node{
            public:
                Node(
            int v):value(v),next(NULL){}
                
            int value;
                Node
            * next;
            };

            Node
            * init_list(int n)
            {
                Node 
            *head, *node, *tail;
                
                head 
            = tail = NULL;
                
            for (int i = 0; i < n; i++)
                {
                    node 
            = new Node(i);

                    
            if (i == 0)
                    {
                        head 
            = node;
                        tail 
            = node;
                    }
                    
            else
                    {
                        tail
            ->next = node;
                        tail 
            = node;
                    }
                }
                
            return head;
            }

            void uninit_list(Node *head)
            {
                Node 
            *node = head;
                
            while (head != NULL)
                {
                    node 
            = node->next;
                    delete head;
                    head 
            = node;
                }
                head 
            = NULL;
            }

            void reverse_list(Node **list)
            {
                Node 
            *head, *tail, *mid;

                head 
            = *list;

                
            //一個(gè)節(jié)點(diǎn),直接返回
                if (head->next == NULL)
                    
            return;

                
            //兩個(gè)節(jié)點(diǎn)或以上節(jié)點(diǎn)
                mid = head->next;
                head
            ->next = NULL;
                tail 
            = mid->next;

                
            while (tail != NULL)
                {
                    mid
            ->next = head;
                    head 
            = mid;
                    mid 
            = tail;
                    tail 
            = tail->next;
                }

                mid
            ->next = head;
                
            *list = mid;
            }

            void output(Node *list)
            {
                
            int i = 0;
                Node 
            *node = list;

                
            while (node != NULL)
                {
                    cout
            <<"node["<<i++<<"] = "<<node->value<<endl;
                    node 
            = node->next;
                }
            }

            int main(int argc, char *argv[])
            {
                
            int len = 5;
                Node 
            *list = NULL;

                
            if (argc > 1)
                    len 
            = atoi(argv[1]);
                
                list 
            = init_list(len);
                output(list);

                reverse_list(
            &list);

                
            //輸出
                cout<<"\n逆序后\n"<<endl;
                output(list);

                uninit_list(list);
                
                
            return 0;    
            }
            久久精品成人欧美大片| 久久99国产精品久久99果冻传媒| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久天天躁狠狠躁夜夜96流白浆| 久久97久久97精品免视看秋霞| 亚洲精品tv久久久久久久久 | 久久久噜噜噜久久中文福利| 人妻无码αv中文字幕久久琪琪布| 欧美麻豆久久久久久中文| 国产精品久久波多野结衣| 久久精品国产72国产精福利| 久久精品青青草原伊人| 国产精品久久久99| 国产精品久久久久久搜索| 婷婷伊人久久大香线蕉AV| 色综合久久夜色精品国产| 亚洲伊人久久综合中文成人网| 久久AAAA片一区二区| 色综合久久中文综合网| 久久亚洲国产欧洲精品一| 91精品婷婷国产综合久久| 国产91久久综合| 久久久久亚洲AV无码专区首JN| 久久青青国产| 国内精品久久久久久久久电影网 | 国内精品久久久久国产盗摄| 久久无码av三级| 亚洲国产精品无码久久一区二区| 国产成人无码精品久久久久免费 | 2020久久精品亚洲热综合一本| 久久99久久无码毛片一区二区| 久久久久久久久久久免费精品 | 热re99久久精品国产99热| 99久久人人爽亚洲精品美女| 久久亚洲视频| 激情久久久久久久久久| 国产一级做a爰片久久毛片| 久久久久久亚洲精品不卡| 91精品国产色综合久久| 五月丁香综合激情六月久久| 武侠古典久久婷婷狼人伊人|