• <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糟 閱讀(1431) 評論(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;

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

                
            //兩個節(jié)點或以上節(jié)點
                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;    
            }
            中文字幕成人精品久久不卡| 久久婷婷五月综合97色直播| 久久久中文字幕| 伊人久久综合热线大杳蕉下载| 久久五月精品中文字幕| 国产精品99久久久精品无码| 精品久久久久久国产91| 日本高清无卡码一区二区久久| 人妻精品久久无码专区精东影业 | 精品久久久久久无码中文字幕| 久久午夜综合久久| 国产精品视频久久久| 国产精品99久久久精品无码| 国产精品成人无码久久久久久| 婷婷伊人久久大香线蕉AV | 久久久av波多野一区二区| 久久久久无码专区亚洲av| 欧美久久综合性欧美| 色综合久久中文字幕无码| 中文字幕无码久久人妻| 2021国产成人精品久久| 国产精品久久久久久| 久久婷婷激情综合色综合俺也去 | 97久久精品午夜一区二区| 久久中文字幕精品| 四虎影视久久久免费观看| 久久精品无码一区二区三区日韩| 久久精品国产亚洲欧美| 久久99精品国产一区二区三区| 久久人人爽人人爽人人AV东京热 | 99久久精品免费看国产| 成人资源影音先锋久久资源网| 欧美丰满熟妇BBB久久久| 99精品国产99久久久久久97 | 中文字幕精品久久| 久久这里只有精品首页| 久久综合久久自在自线精品自| 久久久精品人妻一区二区三区四 | 精品免费tv久久久久久久| 欧美激情精品久久久久| 国产日韩欧美久久|