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

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            996久久国产精品线观看| 9999国产精品欧美久久久久久| 久久精品国产91久久麻豆自制| 99久久这里只有精品| 99久久国产主播综合精品| 久久婷婷午色综合夜啪| 久久国产高潮流白浆免费观看| 国产激情久久久久影院老熟女免费| 久久久久99精品成人片三人毛片 | 91久久九九无码成人网站| 久久久久久亚洲精品不卡| 97精品依人久久久大香线蕉97 | 精品久久久久香蕉网| 久久久久国产一区二区 | 久久精品国产亚洲AV不卡| 久久综合噜噜激激的五月天| 久久成人精品| 国产成人精品久久亚洲| 久久棈精品久久久久久噜噜| 一级做a爰片久久毛片看看 | 国产精品成人久久久| 欧美久久精品一级c片片| 久久精品国产亚洲AV久| 日本久久久久久久久久| 久久婷婷久久一区二区三区| 99国产欧美久久久精品蜜芽| 久久无码国产专区精品| 亚洲国产视频久久| 久久人人爽人人澡人人高潮AV| 国内精品久久久久久麻豆| 成人国内精品久久久久一区| 亚洲国产精品18久久久久久| 四虎国产精品成人免费久久| 人妻丰满?V无码久久不卡| 久久人人爽人人爽人人片AV麻豆 | 看久久久久久a级毛片| 亚洲AV无一区二区三区久久| 国产精品久久久久久五月尺| 久久丝袜精品中文字幕| 亚洲成av人片不卡无码久久| 久久精品国产亚洲AV不卡|