• <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   博問   Chat2DB   管理


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久午夜羞羞影院免费观看| 久久人人青草97香蕉| 99精品国产在热久久| 久久av无码专区亚洲av桃花岛| 97精品久久天干天天天按摩| 久久久久亚洲精品中文字幕| 久久久久人妻一区精品性色av| 51久久夜色精品国产| 久久精品无码一区二区WWW| 狠狠色丁香婷综合久久| 伊人色综合久久天天网| 久久久精品一区二区三区| 精品久久久久久中文字幕大豆网 | 久久免费小视频| 尹人香蕉久久99天天拍| 国内精品久久久久久久涩爱| 久久久久亚洲精品天堂| 国产精品美女久久福利网站| 久久久久久一区国产精品| 国产精品久久久天天影视| 久久精品国产亚洲AV无码麻豆| 久久精品国产亚洲7777| 99久久婷婷免费国产综合精品| 久久久久青草线蕉综合超碰| 国产日韩久久免费影院| 亚洲成人精品久久| 国产欧美一区二区久久| 国产精品18久久久久久vr| 久久综合精品国产二区无码| 中文国产成人精品久久不卡| 综合久久精品色| 久久笫一福利免费导航| 伊人伊成久久人综合网777| 久久午夜无码鲁丝片秋霞 | 91超碰碰碰碰久久久久久综合| 2020久久精品国产免费| 久久精品麻豆日日躁夜夜躁| 久久精品国产亚洲av水果派| 久久国产精品国产自线拍免费| 久久美女网站免费| 亚洲?V乱码久久精品蜜桃|