问题描述:
据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。然而Josephus和他的朋友并不想遵从,Josephus要他的朋友先假装遵从,他将朋友与自己安排在第16个与第31个位置,于是逃过了这场死亡游戏。
#include<stdio.h>
#include<malloc.h>
#include<malloc.h>
typedef struct node {
int data;
struct node *next;
}Node;
int data;
struct node *next;
}Node;
typedef struct list {
Node *head;
Node *rear;
}List;
/*
**@注意头指针无论什么操作都不能动,需要用到头指针的时候,用一个变量代替头指针
Node *head;
Node *rear;
}List;
/*
**@注意头指针无论什么操作都不能动,需要用到头指针的时候,用一个变量代替头指针
*/
List *initCirList() {
List *list = (List *)malloc(sizeof(List));
//创建头节点
list->head = (Node *)malloc(sizeof(Node));
List *list = (List *)malloc(sizeof(List));
//创建头节点
list->head = (Node *)malloc(sizeof(Node));
list->head->data = 1;
//头指针和尾指针都指向头结点
list->head->next = list->head;
list->rear = list->head;
return list;
}
/*
**尾插法插入节点
*/
void listInsert_r(List *list,int insertData) {
//创建新节点
Node *temNode = (Node *)malloc(sizeof(Node));
temNode->data = insertData;
//尾插法核心代码
list->rear->next = temNode; //尾节点先指向新节点,让新节点成为尾节点
temNode->next = list->head; //新的尾节点指向头节点
list->rear = temNode; //尾指针指向新的尾节点
}
/*
**头插法插入节点
*/
void listInsert_h(List *list,int insertData) {
Node *tmpNode = (Node *)malloc(sizeof(Node));
tmpNode->data = insertData;
//头插法核心代码
tmpNode->next = list->head->next; //新节点先指向头节点的下一个节点,所以初始化的时候若 list->head->next = NULL是错误的,尾插法就可以
list->head->next = tmpNode; //头节点指向新节点
//第一个节点来的时候,设置为尾节点
if (list->head == list->rear) {
list->rear = tmpNode;
//list->rear->next = list->head;
}
}
//头指针和尾指针都指向头结点
list->head->next = list->head;
list->rear = list->head;
return list;
}
/*
**尾插法插入节点
*/
void listInsert_r(List *list,int insertData) {
//创建新节点
Node *temNode = (Node *)malloc(sizeof(Node));
temNode->data = insertData;
//尾插法核心代码
list->rear->next = temNode; //尾节点先指向新节点,让新节点成为尾节点
temNode->next = list->head; //新的尾节点指向头节点
list->rear = temNode; //尾指针指向新的尾节点
}
/*
**头插法插入节点
*/
void listInsert_h(List *list,int insertData) {
Node *tmpNode = (Node *)malloc(sizeof(Node));
tmpNode->data = insertData;
//头插法核心代码
tmpNode->next = list->head->next; //新节点先指向头节点的下一个节点,所以初始化的时候若 list->head->next = NULL是错误的,尾插法就可以
list->head->next = tmpNode; //头节点指向新节点
//第一个节点来的时候,设置为尾节点
if (list->head == list->rear) {
list->rear = tmpNode;
//list->rear->next = list->head;
}
}
bool Josephus(List *list) {
Node *delNode = NULL;
if (list->head == list->rear)
return false;
else
{
int count = 0;
Node *p = list->head;
while (p->next!=p)
{
for (int i = 1; i < 2; i++)
{
p = p->next;
}
printf("第%d个人被杀\n", p->next->data);
if (p->next == list->head) {
list->head = list->head->next;
}
if (p->next==list->rear)
{
list->rear = p;
}
delNode = p->next;
p->next = p->next->next;
p = p->next;
free(delNode);
}
printf("最后活下来的是第%d个人", p->data);
}
}
Node *delNode = NULL;
if (list->head == list->rear)
return false;
else
{
int count = 0;
Node *p = list->head;
while (p->next!=p)
{
for (int i = 1; i < 2; i++)
{
p = p->next;
}
printf("第%d个人被杀\n", p->next->data);
if (p->next == list->head) {
list->head = list->head->next;
}
if (p->next==list->rear)
{
list->rear = p;
}
delNode = p->next;
p->next = p->next->next;
p = p->next;
free(delNode);
}
printf("最后活下来的是第%d个人", p->data);
}
}
/*
**打印链表中的所有数据
*/
void showList(List *list) {
//如果链表非空
if (list->head != list->rear) {
Node *p = list->head->next;
printf("%d\n", list->head->data);
while(p != list->head) {
printf("%d\n", p->data);
p = p->next;
}
}
}
int main() {
List *cirList= initCirList();;
for (int i = 2; i <=41; i++)
{
listInsert_r(cirList, i);
}
//listInsert_h(cirList, 10);
//listInsert_h(cirList, 20);
//listInsert_h(cirList, 30);
//listInsert_h(cirList, 40);
showList(cirList);
Josephus(cirList);
getchar();
return 0;
}
**打印链表中的所有数据
*/
void showList(List *list) {
//如果链表非空
if (list->head != list->rear) {
Node *p = list->head->next;
printf("%d\n", list->head->data);
while(p != list->head) {
printf("%d\n", p->data);
p = p->next;
}
}
}
int main() {
List *cirList= initCirList();;
for (int i = 2; i <=41; i++)
{
listInsert_r(cirList, i);
}
//listInsert_h(cirList, 10);
//listInsert_h(cirList, 20);
//listInsert_h(cirList, 30);
//listInsert_h(cirList, 40);
showList(cirList);
Josephus(cirList);
getchar();
return 0;
}