<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            C語言的循環鏈表和約瑟夫環

            時間:2025-08-26 11:37:13 C語言

            C語言的循環鏈表和約瑟夫環

              約瑟夫問題)是一個數學的應用問題,對于學習C語言四非常挺有幫助的,下面是小編為大家搜集整理出來的有關于C語言的循環鏈表和約瑟夫環,一起了解下吧!

              循環鏈表的實現

              單鏈表只有向后結點,當單鏈表的尾鏈表不指向NULL,而是指向頭結點時候,形成了一個環,成為單循環鏈表,簡稱循環鏈表。當它是空表,向后結點就只想了自己,這也是它與單鏈表的主要差異,判斷node->next是否等于head。

              代碼實現分為四部分:

              1. 初始化

              2. 插入

              3. 刪除

              4. 定位尋找

              代碼實現:

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            53
            54
            55
            56
            57
            58
            59
            60
            61
            62
            63
            64
            65
            66
            67
            68
            69
            70
            71
            72
            73
            74
            75
            76
            77
            78
            79
            80
            void ListInit(Node *pNode){
                int item;
                Node *temp,*target;
                cout<<"輸入0完成初始化"<<endl; cin="">>item;
                    if(!item)
                        return ;
                    if(!(pNode)){ /pic/code>
                        pNode = new Node ;
                        if(!(pNode))
                            exit(0);/pic/code>
                        pNode->data = item;
                        pNode->next = pNode;
                    }
                    else{
                        /pic/code>
                        for(target = pNode;target->next!=pNode;target = target->next)
                            ;
                        temp = new Node;
                        if(!(temp))
                            exit(0);
                        temp->data = item;
                        temp->next = pNode;
                        target->next = temp;
                    }
                }
            }
            void ListInsert(Node *pNode,int i){ /pic/code>
                Node *temp;
                Node *target;
                int item;
                cout<<"輸入您要插入的'值:"<<endl; cin="">>item;
                if(i==1){
                    temp = new Node;
                    if(!temp)
                        exit(0);
                    temp->data = item;
                    for(target=pNode;target->next != pNode;target = target->next)
                    ;
                    temp->next = pNode;
                    target->next = temp;
                    pNode = temp;
                }
                else{
                    target = pNode;
                    for (int j=1;j<i-1;++j) target="target-">next;
                    temp = new Node;
                    if(!temp)
                        exit(0);
                    temp->data = item;
                    temp->next = target->next;
                    target->next = temp;
                }
            }
            void ListDelete(Node *pNode,int i){
                Node *target,*temp;
                if(i==1){
                    for(target=pNode;target->next!=pNode;target=target->next)
                    ;
                    temp = pNode;/pic/code>
                    pNode = pNode->next;
                    target->next = pNode;
                     temp;
                }
                else{
                    target = pNode;
                    for(int j=1;j<i-1;++j) target="target-">next;
                    temp = target->next;/pic/code>
                    target->next = target->next->next;
                     temp;
                }
            }
            int ListSearch(Node *pNode,int elem){ /pic/code>
                Node *target;
                int i=1;
                for(target = pNode;target->data!=elem && target->next!= pNode;++i)
                    target = target->next;
                if(target->next == pNode && target->data!=elem)
                    return 0;
                else return i;
            }</i-1;++j)></i-1;++j)></endl;></endl;>

              約瑟夫問題

              約瑟夫環(約瑟夫問題)是一個數學的應用問題:已知n個人(以編號1,2,3…n分別表示)圍坐在一張圓桌周圍。從編號為k的人開始報數,數到m的那個人出列;他的下一個人又從1開始報數,數到m的那個人又出列;依此規律重復下去,直到圓桌周圍的'人全部出列。這類問題用循環列表的思想剛好能解決。

              注意:編寫代碼的時候,注意報數為m = 1的時候特殊情況

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            53
            54
            55
            56
            57
            58
            59
            60
            61
            #include<iostream>
            #include<cstdio>
            using namespace std;
            typedef struct Node{
                int data;
                Node *next;
            };
             
            Node *Create(int n){
                Node *p = NULL, *head;
                head = new Node;
                if (!head)
                    exit(0);
                p = head; /pic/code>
                int item=1;
                if(n){
                    int i=1;
                    Node *temp;
                    while(i<=n){
                        temp = new Node;
                        if(!temp)
                            exit(0);
                        temp->data = i++;
                        p->next = temp;
                        p = temp;
                    }
                    p->next = head->next;
                }
                 head;
                return p->next;
            }
            void Joseph(int n,int m){
                /pic/code>
                m = n%m;
             
                Node *start = Create(n);
             
                if(m){/pic/code>
                    while(start->next!=start){
                        Node *temp = new Node;
                        if(!temp)
                            exit(0);
                        for(int i=0;i<m-1;i++) 1="" m="3%2" start="start-">next;
                        temp = start->next;
                        start->next = start->next->next;
                        start = start->next;
                        cout<<temp->data<<" ";
                         temp;
                    }
                }
                else{
                    for(int i=0;i<n-1;i++){ node="" start-="" temp="new">data<<" ";
                        temp = start;
                        start = start->next;
                         temp;
                    }
                }
                cout<<endl; last="" person="" start-="" the="">data<<endl; int="" pre="" return="">
                     
                
            </endl;></endl;></n-1;i++){></temp-></m-1;i++)></cstdio></iostream>


            【C語言的循環鏈表和約瑟夫環】相關文章:

            C語言用數組解決約瑟夫環問題11-21

            c語言鏈表的用法03-10

            C語言單向鏈表環測試并返回環起始節點的方法07-22

            C語言for循環11-14

            c語言鏈表的用法有哪些01-24

            鏈表的C語言實現方法12-10

            C語言鏈表逆序方法技巧01-27

            C語言中while循環和do...while循環02-01

            C語言循環結構12-06

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院