• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            單向鏈表逆序

            Posted on 2011-06-23 17:01 亂78糟 閱讀(1423) 評論(0)  編輯 收藏 引用 所屬分類: 算法&數據結構
            /***********************************
            *    單向鏈表逆序
            *    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;

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

                
            //兩個節點或以上節點
                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;    
            }
            精品免费tv久久久久久久| 婷婷久久综合九色综合绿巨人| 久久受www免费人成_看片中文| 亚洲精品美女久久久久99小说| 日韩欧美亚洲综合久久| 久久久久99精品成人片直播| 久久亚洲国产精品一区二区| 久久久久久av无码免费看大片| 久久人做人爽一区二区三区 | 久久国产精品成人免费| 99国内精品久久久久久久| 狠狠色丁香久久婷婷综合_中| 久久一日本道色综合久久| 久久久久亚洲精品无码网址| 久久人人爽人人爽人人AV| 久久久久女教师免费一区| 乱亲女H秽乱长久久久| 香蕉久久夜色精品国产尤物| 伊人久久综合热线大杳蕉下载| 久久精品国产亚洲av麻豆蜜芽| 国产成人精品综合久久久| 麻豆成人久久精品二区三区免费 | 久久久久亚洲av无码专区导航| 国产精品va久久久久久久| 国产精品久久久亚洲| 久久天天躁狠狠躁夜夜不卡 | 99久久99久久精品国产片果冻| 丁香五月综合久久激情| 久久久久久久人妻无码中文字幕爆 | 亚洲精品WWW久久久久久| 国产高潮国产高潮久久久91 | 久久午夜福利电影| 久久国产美女免费观看精品| 国产一区二区三区久久| 久久99热只有频精品8| 亚洲国产精品无码久久SM| 狠狠色丁香久久婷婷综合| 久久只有这精品99| 精品一二三区久久aaa片| 77777亚洲午夜久久多喷| 亚洲国产精品无码久久一线|