• <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   博問(wèn)   Chat2DB   管理


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久久久久精品久久久久| 久久国产三级无码一区二区| 国内精品伊人久久久影院| 久久人妻AV中文字幕| 人妻久久久一区二区三区| 91久久香蕉国产熟女线看| 久久久久99这里有精品10 | 久久精品一本到99热免费| 999久久久国产精品| 香蕉久久av一区二区三区| 99久久精品国产一区二区| 亚洲综合精品香蕉久久网| 亚洲精品高清久久| 久久久久亚洲av无码专区喷水| 777久久精品一区二区三区无码 | 亚洲精品tv久久久久久久久| 97久久精品人人澡人人爽| 亚洲女久久久噜噜噜熟女| 久久精品二区| 一级做a爱片久久毛片| 欧美熟妇另类久久久久久不卡| 久久强奷乱码老熟女网站| 99久久超碰中文字幕伊人| 中文字幕无码久久精品青草| 成人亚洲欧美久久久久| 国内精品久久久久影院一蜜桃| 2021国内精品久久久久久影院| 国产99久久久国产精品~~牛| 国内精品久久九九国产精品| 人妻少妇久久中文字幕 | 亚洲人成伊人成综合网久久久| 久久久亚洲精品蜜桃臀| 国产精品99久久久久久www| 久久久久一区二区三区| 国产精品欧美久久久天天影视| 久久精品亚洲一区二区三区浴池| 伊人久久综合成人网| 亚洲精品无码久久一线| 热re99久久6国产精品免费| 久久久久久九九99精品| 精品久久8x国产免费观看|