![](zzz/editor/img/code.gif)
程序代码:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
#define APPLY (pNode)malloc(sizeof(Node))
typedef struct Node
{
int data;
struct Node *next;
}Node, *pNode;
pNode CreatLink()
{
pNode head, temp, pre;
head = APPLY;head->next = NULL;
pre = head;
for (int i = 1;i <= SIZE;++i)
{
temp = APPLY;
temp->data = i;
pre->next = temp;
pre = temp;
}
temp->next = head->next;
free(head);
return temp->next;
}
int Rand()
{
int key;
srand(time(0));
key = rand() % (SIZE * 2) + 2;
printf("随机产生关键字 KEY = %d", key);
getchar();
return key;
}
void Display(pNode head)
{
pNode temp = head;
puts("当前剩余编号为:");
do {
printf("%d ", temp->data);
temp = temp->next;
} while(temp != head);
puts("");
}
void Josephus(pNode head, int key)
{
for (pNode p = head;p != p->next;p = p->next)
{
Display(p);
for (int temp = key;--temp;p = p->next);
pNode q = p->next;p->next = q->next;
printf("剔除编号:%d\n\n", q->data);
// getchar();
free(q);
}
printf("最后剩余编号为:%d\n", p->data);
free(p);
}
int main()
{
system("color 9e");
Josephus(CreatLink(), Rand()-1);
return 0;
}