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

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


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产综合免费精品久久久| 亚洲精品国产综合久久一线| 精品综合久久久久久888蜜芽| 亚洲精品无码久久久久去q| 麻豆成人久久精品二区三区免费 | 久久亚洲精品无码AV红樱桃| 91久久精品91久久性色| 超级碰久久免费公开视频| 久久婷婷五月综合成人D啪| 国产91久久精品一区二区| 久久久免费观成人影院| 色88久久久久高潮综合影院| 久久精品18| 国产精品禁18久久久夂久| 色婷婷久久久SWAG精品| 久久99久久99小草精品免视看| 无码八A片人妻少妇久久| 久久er国产精品免费观看2| 久久亚洲日韩看片无码| 国产呻吟久久久久久久92| 无码精品久久久久久人妻中字| 久久精品无码一区二区app| 91视频国产91久久久| 久久精品国产男包| 国内精品久久国产| 久久www免费人成看国产片| 久久99精品久久久久久久久久| 久久亚洲AV成人无码| 亚洲国产成人久久综合野外| 久久精品国产亚洲7777| 久久国产成人午夜aⅴ影院 | 久久久久久久人妻无码中文字幕爆| 久久亚洲欧洲国产综合| 久久国产精品一区| 久久国产精品免费| 国产无套内射久久久国产| 国产精品丝袜久久久久久不卡| 亚洲一区二区三区日本久久九| 久久精品成人免费网站| 91精品国产91久久久久久蜜臀| 亚洲国产精品久久久久|