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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品亚洲中文字幕无码麻豆| 国产午夜精品理论片久久影视| 合区精品久久久中文字幕一区| 国产精品免费久久久久影院| 欧美性大战久久久久久| 亚洲中文字幕无码久久精品1| 99久久免费国产精品热| 久久午夜无码鲁丝片午夜精品| 久久婷婷色香五月综合激情| 久久99国产综合精品| 久久久久九九精品影院| 久久青青草原亚洲av无码app | 久久久久久国产a免费观看不卡| 久久久久久国产a免费观看黄色大片| 久久综合国产乱子伦精品免费| 久久精品成人欧美大片| 韩国免费A级毛片久久| 一本久久精品一区二区| 色噜噜狠狠先锋影音久久| 婷婷久久久亚洲欧洲日产国码AV| 欧美日韩精品久久久免费观看| 亚洲午夜精品久久久久久人妖| 日韩精品久久久久久免费| 午夜福利91久久福利| 中文字幕久久欲求不满| 久久99国内精品自在现线| 国内精品久久久久影院薰衣草| 久久精品成人影院| 久久国产午夜精品一区二区三区| 国产精品久久久久久| 久久精品www人人爽人人| 久久久久亚洲AV片无码下载蜜桃| 综合久久一区二区三区 | 久久亚洲精品人成综合网| 久久亚洲国产成人精品无码区| 国产精品成人久久久久久久| 2021精品国产综合久久| 久久久精品人妻一区二区三区四| 久久久无码精品亚洲日韩按摩| 精品免费久久久久久久| 国产精品久久午夜夜伦鲁鲁|