#include <cstdio>
#include <iostream>
#include <cstdlib>
using namespace std;
struct node
{
int index;
node * next;
};
typedef node * LNode;
class CLL
{
public:
LNode head = NULL;
public:
static LNode getnew()//get new node or point
{
LNode L;
if (L=(LNode)malloc(sizeof(node)))
{L->next=NULL;return L;}
else {
cout << "memory error"<<endl;
exit(-1);
}
}
void print()
{
LNode L = head;
if (!L) cout << "Unlickily , it's empty" << endl;
cout << L->index;
L->next?cout<<"->":cout<<endl;
LNode tail = L->next;
while(tail != L){
cout<<tail->index;
tail->next!=L?cout<<"->":cout<<endl;
tail=tail->next;
}
}
CLL(int n)
{
if (n<=0) exit(-1);
head = getnew();
head->index=0;
LNode tail = head;
for (int i=1;i<n;i++)
{
tail->next = getnew();
tail->next->index=i;
tail = tail->next;
}
tail->next=head;
}
void circle(int m)
{
int cnt=0;
LNode t = head;
LNode b = NULL;
while (t->next!=t)
{
cnt++;
if (cnt == m)
{
b->next = t->next;
free(t);
t = b;
cnt=0;
}
b = t;
t = t->next;
}
cout << t->index+1 << endl;
}
};
int main()
{
int n;
cin >> n;
CLL cll(n);
cll.print();
int m;
cin >> m;
cll.circle(m);
return 0;
}
转载:https://blog.csdn.net/Fight_Feature/article/details/102490868
查看评论