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

            Problem Solving using C++

            Algorithm Study using C++

            單鏈表的翻轉

            非常簡單的實現,設置3個指針,分別指向當前節點、前一個節點、后一個節點,然后依次處理所有的節點就OK了。
            具體代碼為:
            #include <iostream>

            using namespace std;

            #ifndef 
            null
            #define 
            null (void*)0
            #endif

            typedef struct node
            {
                struct node
            * next;
                
            int data;
            }node;

            node
            * head=(node*)null;

            void reverse(node* root)
            {
                node 
            *cur,*pre,*next;
                
                pre
            =(node*)null;
                cur
            =root;
                next
            =cur->next;
                
                
            while(next)
                {
                    cur
            ->next=pre;
                    pre
            =cur;
                    cur
            =next;
                    next
            =cur->next;
                }
                
                head
            =cur;
                cur
            ->next=pre;
            }

            void insert(node* p)
            {
                p
            ->next=head;
                head
            =p;
            }

            void del(node* p)
            {
                node 
            *cur,*next;
                cur
            =p;
                next
            =p->next;
                
                
            while(next)
                {
                    delete cur;
                    cur
            =next;
                    next
            =cur->next;
                }
                
                delete cur;
            }

            void print(node* p)
            {
                
            while(p)
                {
                    cout
            <<p->data<<" ";
                    p
            =p->next;
                }
                
                cout
            <<endl;
            }

            int main(int argc,char** argv)
            {
                
            for(int i=0;i<10;i++)
                {
                    node
            * p=new node;
                    p
            ->next=(node*)null;
                    p
            ->data=i;
                    
                    insert(p);
                }
                print(head);
                cout
            <<"reverse order:"<<endl;
                
                reverse(head);
                print(head);
                
                del(head);
                
                
                system(
            "pause");
                
            return 0;
            }


            posted on 2007-09-05 14:41 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(2026) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国産精品久久久久久久| 亚洲综合精品香蕉久久网| 无码人妻精品一区二区三区久久久 | 久久中文字幕视频、最近更新| 久久久女人与动物群交毛片| 午夜精品久久久久久影视riav| 婷婷久久精品国产| 国产无套内射久久久国产| 久久国产精品免费| 一日本道伊人久久综合影| 久久综合伊人77777麻豆| 热久久视久久精品18| 青青草原综合久久大伊人| 国产aⅴ激情无码久久| 亚洲人成网亚洲欧洲无码久久| 亚洲欧美成人综合久久久| A狠狠久久蜜臀婷色中文网| 久久高清一级毛片| 2021国产精品久久精品| 国内精品欧美久久精品| 欧美日韩久久中文字幕| 国内精品久久人妻互换| 久久精品亚洲乱码伦伦中文| 久久亚洲sm情趣捆绑调教| 蜜桃麻豆www久久| 亚洲AV日韩精品久久久久| 久久九九久精品国产免费直播| 伊人久久亚洲综合影院| 99久久精品日本一区二区免费| 国产2021久久精品| 国产精品久久久久久久久鸭| 久久婷婷色综合一区二区| 国产精品九九久久免费视频 | 久久久无码精品亚洲日韩蜜臀浪潮| 久久99精品久久久久久久不卡| 2021国内精品久久久久久影院| 久久人人爽人人爽人人片AV东京热| 久久久久人妻精品一区二区三区| 亚洲精品午夜国产va久久| 狠狠色综合久久久久尤物| 国产精品成人99久久久久 |