• <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;    
            }
            亚洲国产成人久久综合区| 99国产欧美精品久久久蜜芽| 色婷婷久久综合中文久久一本| 亚洲精品第一综合99久久| 亚洲AV日韩AV天堂久久| 热re99久久精品国产99热| 亚洲国产高清精品线久久| 国内精品久久久久影院日本| 久久夜色精品国产www| 国产精品一久久香蕉产线看| 一本久道久久综合狠狠躁AV| 99久久99久久久精品齐齐| 香蕉aa三级久久毛片| 国产精品久久久香蕉| 久久久久亚洲AV片无码下载蜜桃| 久久精品九九亚洲精品天堂| 国产午夜免费高清久久影院| 久久精品中文无码资源站| 久久亚洲AV成人无码电影| 久久婷婷人人澡人人爽人人爱| 久久人人添人人爽添人人片牛牛| 亚洲欧美一级久久精品| 午夜不卡久久精品无码免费| 久久久久久亚洲精品不卡 | 色欲综合久久中文字幕网| 国产精品美女久久久网AV| 久久99精品久久久久子伦| 久久久久久久波多野结衣高潮| 久久久久亚洲爆乳少妇无| 丰满少妇人妻久久久久久4| 国产精品久久成人影院| 性高湖久久久久久久久| 三上悠亚久久精品| 亚洲精品国精品久久99热一| 久久夜色精品国产噜噜亚洲a| 久久久精品日本一区二区三区| 久久婷婷色综合一区二区| 亚洲综合久久夜AV | 麻豆久久久9性大片| 久久精品国产欧美日韩99热| 久久久亚洲AV波多野结衣|