#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int data_t;
typedef struct node {
data_t data;
struct node* next;
} node_t;
node_t* CreateJos(int mynmemb)
{
node_t* list;
node_t* q;
int i = mynmemb;
list =(node_t*) malloc(sizeof(node_t));
if (list == NULL)
return NULL;
memset(list, 0, sizeof(node_t));
list->data = 1;
list->next = list;
while (i > 1) {
q =(node_t*) malloc(sizeof(node_t));
if (q == NULL) {
while (list->next != list) {
q = list->next;
list->next = q->next;
free(q);
}
free(list);
return NULL;
}
q->data = i--;
q->next = list->next;
list->next = q;
}
return list;
}
void DisplayJos(node_t* list)
{
node_t* p;
if (list == NULL)
return;
printf("%d ", list->data);
p = list->next;
while (p != list) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
node_t* killJos(node_t* list, int kill_num, int start_num)
{
int num = 0;
node_t* p = list;
node_t* q;
/* 找到起始报数结点的前一个结点 */
while (1) {
if (p->next->data == start_num)
break;
p = p->next;
}
while (p != p->next) {
/* 报数 */
num++;
if (num == kill_num) {
/* 删除结点 */
q = p->next;
p->next = q->next;
printf("%d\n", q->data);
free(q);
num = 0;
}
else
p = p->next;
}
return p;
}
int main()
{
node_t* list;
list = CreateJos(10);
if (list == NULL)
return -1;
DisplayJos(list);
killJos(list, 4, 5);
}
03-13
433
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)