類實現 約瑟夫環問題

來源:互聯網
上載者:User
#include <iostream>  using namespace std; class LNode {     friend class List;     public:         LNode():m(0),next(0){}     private:         int m;         int num;         LNode *next; }; class Head {     friend class List;     public:         Head():top(0),rear(0){}     private:         LNode *top;         LNode *rear; }; class List {     public:         bool Insert(Head &p,int e);         bool traverse(Head &p,int m);         bool ss(Head &p)         {             int s=0;             LNode *q=p.top;             while(q)             {                 cout<<q->m<<' ';                 q=q->next;                 s++;                 if(s>10)                     return true;             }         }         static int s; }; int List::s=1 ; bool List::traverse(Head &p,int m) {     LNode *q=p.top;     int j=1;     while(q!=q->next)     {         j=1;         while(j!=m-1)         {             q=q->next;             j++;         }         LNode *h=q->next;         m=h->m;         cout<<h->num<<' ';         q->next=h->next;         q=h->next;         delete h;     }     return true; } bool List::Insert(Head &p,int e) {     LNode *q=new LNode;     if(!q)         return false;     q->m=e;     q->num=s++;     if(p.top!=0&&p.rear!=0)     {         p.rear->next=q;         p.rear=q;         p.rear->next=p.top;     }     else     {         p.top=p.rear=q;     }     return true; } int main() {     int n;     cin>>n;     int e;     Head p;     List l;     while(n--)     {         cin>>e;         l.Insert(p,e);     }     //l.ss(p);     l.traverse(p,5);     return 0; }
第一次用類實現比較繁瑣的問題額~~~~juststeps

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.