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

            單鏈表的翻轉(zhuǎn)

            非常簡(jiǎn)單的實(shí)現(xiàn),設(shè)置3個(gè)指針,分別指向當(dāng)前節(jié)點(diǎn)、前一個(gè)節(jié)點(diǎn)、后一個(gè)節(jié)點(diǎn),然后依次處理所有的節(jié)點(diǎn)就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) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            狠狠精品干练久久久无码中文字幕 | 久久精品国产一区| 久久久久久久97| 国产精品内射久久久久欢欢| 午夜福利91久久福利| A狠狠久久蜜臀婷色中文网| 亚洲精品美女久久久久99小说| av国内精品久久久久影院| 亚洲国产成人久久精品影视 | 一本色道久久综合狠狠躁| 久久综合综合久久97色| 亚洲AV乱码久久精品蜜桃| 日本加勒比久久精品| 久久久亚洲欧洲日产国码是AV| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 999久久久无码国产精品| 午夜精品久久久久| 欧美激情精品久久久久久| 久久国产成人精品麻豆| 合区精品久久久中文字幕一区 | 久久久久成人精品无码| 欧美亚洲另类久久综合| 久久国产亚洲精品| 狠狠人妻久久久久久综合| 熟妇人妻久久中文字幕| 久久亚洲国产最新网站| 久久中文字幕一区二区| 国内精品伊人久久久久av一坑| 一本大道久久香蕉成人网| 国产一区二区精品久久凹凸| 成人妇女免费播放久久久| 亚洲乱码中文字幕久久孕妇黑人| 日本久久久久久久久久| 国产精品欧美久久久久无广告| 久久超乳爆乳中文字幕| 久久综合给合久久狠狠狠97色| 无遮挡粉嫩小泬久久久久久久| 中文字幕无码久久久| 怡红院日本一道日本久久 | 久久久久亚洲AV无码观看| 蜜桃麻豆www久久国产精品|