• <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 閱讀(2025) 評(píng)論(0)  編輯 收藏 引用


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲狠狠久久综合一区77777| 欧美亚洲日本久久精品| 人妻无码久久一区二区三区免费| 人妻精品久久久久中文字幕69 | 亚洲综合婷婷久久| 久久se精品一区二区影院| 久久综合色之久久综合| 久久久久亚洲AV无码专区体验| 国产亚洲欧美成人久久片| 久久久久综合中文字幕| 久久精品亚洲日本波多野结衣 | 久久国产影院| 奇米影视7777久久精品| 久久久无码精品亚洲日韩软件| 老色鬼久久亚洲AV综合| 欧美久久久久久午夜精品| 亚洲国产另类久久久精品| 久久精品无码专区免费| 国产精品毛片久久久久久久| 国内精品伊人久久久久777| 久久精品国产99国产精品| 香港aa三级久久三级| 久久久久亚洲AV片无码下载蜜桃| 久久精品国产男包| 欧美精品丝袜久久久中文字幕 | 97久久超碰国产精品旧版| 亚洲欧洲精品成人久久曰影片| 91久久精品国产91性色也| 精品综合久久久久久97超人 | 久久www免费人成看国产片| 久久久久人妻一区精品性色av| 久久久无码精品亚洲日韩京东传媒| 久久国产成人午夜aⅴ影院| 99久久精品免费看国产免费| 久久99精品久久久久久 | 国产午夜精品久久久久九九电影 | 怡红院日本一道日本久久| 久久99国产精一区二区三区| 久久精品国产亚洲一区二区| 久久久九九有精品国产| 91亚洲国产成人久久精品网址|