码迷,mamicode.com
首页 > 其他好文 > 详细

带头结点的循环链表实现约瑟夫环

时间:2019-10-05 14:25:44      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:can   oid   col   cout   span   pos   scan   circle   please   

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define sc scanf
  4 #define pr printf
  5 
  6 typedef struct LNode{
  7     int data;
  8     struct LNode *next;
  9 }LNode,*LinkList; 
 10 
 11 void Create_CircleList(LinkList &L,int n)
 12 {
 13     LNode *p,*q;
 14     L = (LNode *)malloc(sizeof(LNode));
 15     L->data = n + 1;
 16     L->next = NULL;
 17     
 18     //创建最后一个结点 
 19     q = (LNode *)malloc(sizeof(LNode));
 20     scanf("%d",&q->data);
 21     q->next = L->next;
 22     L->next = q;
 23     
 24     for(int i = 1;i < n;i++)
 25     {
 26         p = (LNode *)malloc(sizeof(LNode));
 27         scanf("%d",&p->data);
 28         p->next = L->next;
 29         L->next = p;
 30     }
 31     //因为是循环链表,所以最后一个结点的指针指向头结点 
 32     q->next = L;
 33     
 34 }
 35 
 36 //遍历循环链表  
 37 void Traver_CircleList(LinkList &L)
 38 {
 39   LNode *p = L;
 40   while(p->next != L)
 41   {
 42        p = p->next;
 43      cout << p->data << " "; 
 44   }    
 45   puts("");
 46 } 
 47 
 48 int getLength(LinkList &L)
 49 {
 50   LNode *p = L;
 51   int cnt = 0;
 52   while(p->next != L)
 53   {
 54        p = p->next;
 55      cnt++;
 56   }    
 57   return cnt;
 58 }
 59 
 60 //删除循环链表的某个结点
 61 bool Delete_Node(LinkList &L,int i,int &e)
 62 {
 63     int len = getLength(L);
 64     
 65     if(L->next == NULL)
 66     {
 67         puts("Sorry! Empty LinkList!");
 68         return 0;
 69     }
 70     if(i < 1 || i > len)
 71     {
 72         puts("Oh! Baby! The position is invalid. Please re-enter the position!");
 73         return 0;
 74     }
 75     LNode *p = L;
 76     int j = 0;
 77     while(p->next != L && j < i - 1)
 78     {
 79          p = p->next;
 80          j++;
 81     }    
 82     LNode *q;
 83     q = p->next;
 84     p->next = q->next;
 85     e = q->data;
 86     free(q);
 87     return 1;
 88 } 
 89 
 90 void Joseph(LinkList &L,int m,int k,int n)
 91 {
 92     int cnt = 0,tot = 1;
 93     LNode *p = L,*q;
 94     for(int i = 1;i < k;i++)
 95     {
 96         p = p->next;
 97     }
 98     
 99     while(tot < n )
100     {
101         cnt++;
102         //ty++;
103         if( (p->next)->data == n + 1 ) {
104             p = p->next;
105             cnt--; 
106             continue;
107         }
108         
109         if(cnt % m == 0)
110         {
111             tot++;
112             LNode *tem = p->next;
113             p->next = tem->next;
114             //p = tem->next;
115             cout << tem->data << " ";
116             free(tem);
117         }
118         else p = p->next;
119     }
120     q = L->next;
121     puts("");
122     pr("The Lucky guy is ");
123     cout << q->data << endl;
124     return;
125 }
126 int main()
127 {
128     LNode *la;
129     int n,m,k;
130     scanf("%d%d%d",&n,&m,&k);
131     Create_CircleList(la,n);
132     
133     cout << "La length = " << getLength (la) << endl;
134     Traver_CircleList(la);
135     
136     Joseph(la,m,k,n);
137     cout << "La length = " << getLength (la) << endl;
138     int e;
139     bool ok = Delete_Node(la,1,e);
140     if(ok)
141     {
142         cout << "The delete number is " << e << endl;
143         cout << "after delete La length = " << getLength(la) << endl;
144         pr("after delete La is ");
145         Traver_CircleList(la);
146     }
147     return 0;
148 }

 

带头结点的循环链表实现约瑟夫环

标签:can   oid   col   cout   span   pos   scan   circle   please   

原文地址:https://www.cnblogs.com/mch5201314/p/11624386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!