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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            区亚洲欧美一级久久精品亚洲精品成人网久久久久| av午夜福利一片免费看久久| 香蕉99久久国产综合精品宅男自| 久久精品国产99久久香蕉| 精品国产青草久久久久福利| 国产午夜精品久久久久九九电影| 久久久久波多野结衣高潮| 亚洲精品乱码久久久久久按摩 | 国产高潮国产高潮久久久91| 色综合久久天天综合| 久久精品中文字幕一区| 久久精品国产清高在天天线| 国产精品久久久久久搜索| 色综合色天天久久婷婷基地| 伊人久久综合精品无码AV专区| 久久国产精品一国产精品金尊| 亚洲欧美一区二区三区久久| 久久久久AV综合网成人| 久久久这里有精品| 欧美色综合久久久久久| 伊人色综合久久天天| 久久久久亚洲AV无码专区首JN| 狠狠久久综合| 99久久精品国产综合一区| 久久精品99久久香蕉国产色戒 | 91久久成人免费| 久久精品aⅴ无码中文字字幕重口| 欧美日韩精品久久久免费观看| 国产亚洲精久久久久久无码| 久久精品无码一区二区WWW| 亚洲а∨天堂久久精品| 久久精品国产亚洲精品| 国产精品伦理久久久久久| 久久久精品午夜免费不卡| avtt天堂网久久精品| 久久精品国产99久久无毒不卡 | 久久99久国产麻精品66 | 国产午夜精品理论片久久| 岛国搬运www久久| 一本久久a久久精品综合夜夜| 久久精品国产秦先生|