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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲乱码中文字幕久久孕妇黑人| 国产亚洲精品自在久久| 亚洲欧美成人久久综合中文网 | 久久婷婷久久一区二区三区| 国产亚州精品女人久久久久久| 久久狠狠爱亚洲综合影院| 国内精品久久人妻互换| 亚州日韩精品专区久久久| www.久久99| 亚洲v国产v天堂a无码久久| 99久久er这里只有精品18| 一本一本久久a久久精品综合麻豆| 无遮挡粉嫩小泬久久久久久久| 久久精品亚洲欧美日韩久久| 潮喷大喷水系列无码久久精品| 久久精品无码一区二区WWW| 久久久国产一区二区三区| …久久精品99久久香蕉国产| 亚洲精品乱码久久久久久| 伊人久久五月天| 少妇久久久久久被弄到高潮 | 国产精品免费久久久久影院| 久久亚洲私人国产精品| 久久久久久久久久久| 精品国产日韩久久亚洲| 久久综合九色综合欧美就去吻| avtt天堂网久久精品| 久久精品aⅴ无码中文字字幕不卡| 久久亚洲精品无码aⅴ大香| 婷婷久久综合| 亚洲精品无码久久久久AV麻豆| 国产成人综合久久精品尤物| 久久精品国产精品亚洲精品| 久久精品国产一区| 精品综合久久久久久97超人| 国产精品18久久久久久vr| 91精品无码久久久久久五月天| 久久香蕉国产线看观看99| 99久久精品免费| 亚洲国产成人久久综合野外| 99久久香蕉国产线看观香|