采用循环单链表的方法:
每当遇到m的时候就把这个值从链表中删去,从新从它的下一个链表开始数数。
#include <iostream>
using namespace std;
typedef struct circleLink
{
int data;
struct circleLink *next;
}node;
node *create(int n)
{
node *head = new node();
head->data = 1;
node *p = head;
int m = n-1;
int i = 2;
while(m)
{
node *s = new node();
s->data = i;
p->next = s;
p = s;
m--;
i++;
}
p->next = head;
return head;
}
void findx(node *head , int m)
{
node *p = head ;
int i = 1;
while(p->next != p)
{
node *s = p;
p = p->next ;
i++;
if(i == m)
{
cout<<p->data<<endl;
s->next = p->next;
p = s->next ;
i = 1;
}
}
cout<<p->data<<endl;
}
void main()
{
node *head;
head = create(6);
findx(head , 3);
}