• <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)論排行榜

            久久天天躁狠狠躁夜夜躁2O2O| 久久乐国产精品亚洲综合| 久久久精品人妻一区二区三区四| 中文字幕乱码人妻无码久久| 国产一久久香蕉国产线看观看| 久久久精品无码专区不卡| 日日噜噜夜夜狠狠久久丁香五月| 国产精自产拍久久久久久蜜| 精品国产青草久久久久福利| 精品久久久久久99人妻| 男女久久久国产一区二区三区| 精品久久人人妻人人做精品 | 久久久女人与动物群交毛片| 国产视频久久| 精品久久久久久久| 无遮挡粉嫩小泬久久久久久久| 久久久久国产精品三级网| 久久超碰97人人做人人爱| 久久久久久久精品妇女99| 精品久久人人妻人人做精品 | 久久国产精品无码网站| 国产精品一久久香蕉国产线看| 99久久99久久精品国产片果冻| 国产午夜精品理论片久久 | 国内精品久久久久| 麻豆AV一区二区三区久久| 国产亚洲精品久久久久秋霞| 女同久久| 久久精品无码一区二区WWW| 色天使久久综合网天天 | 久久久久高潮综合影院| 久久夜色精品国产| 国产精品久久久久久久午夜片 | 天天久久狠狠色综合| 久久99精品久久只有精品| 蜜臀久久99精品久久久久久小说| 亚洲国产一成人久久精品| 久久精品国产99久久久古代| 国产偷久久久精品专区 | 日本精品久久久中文字幕| 久久精品人人做人人爽电影|