//约瑟夫环问题:41个人排成一个圆圈,由第1个人开始报数,
// 每报数到第3人该人就必须自杀,
// 然后再由下1个人重新报数,直到所有人都自杀身亡为止
//循环链表:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int n = 41;
typedef struct {
int data;
struct Node* next;
}Node, * CircleList;
void InitList(CircleList L)
{
L = (CircleList*)malloc(sizeof(CircleList));
printf("初始化成功\n");
}
void DeleteList(CircleList head, int i)// 删除表中下标为i的元素
{
Node* r = (Node*)malloc(sizeof(Node));
r= head;
int j = 0;
while (r && (i - 1) > j)
{
r = r->next;
j++;
}
Node* m = (Node*)malloc(sizeof(Node));
m = r->next;
r->next = m->next;
free(m);
head = r;
printf("del ok\n");
}
void Add(CircleList head) {//初始化人的序号
Node* a = (Node*)malloc(sizeof(Node));
a=head;
a->data = 1;
a->next = NULL;
Node* aa = a;
int i = 0;
for (i = 2; i < n+1; i++)
{
Node* p = (Node*) malloc(sizeof(Node));
p->data = i;
p->next = NULL;
aa->next=p;
aa = aa->next;
}
aa->next = a;
printf("add ok\n");
}
int Live(CircleList head) //返回存活的人的编号
{
int total = n;
int j = 1;
int count = 0;
Node* p = (Node*)malloc(sizeof(Node));
p = head;
while(total>2)
{
p = p->next;
count++;
int judge = count % 3;
if (judge==0) {
DeleteList(p, 0); total--;
count = 0;
}
}
return p->data;
}
int main()
{
CircleList L=NULL;
InitList(L); //传地址
Add(&L);
DeleteList( &L, 2);
int res = 0;
res = Live(&L);
printf("%d", res);
return 0;
}
请问我哪里出错了呀,运行不出来结果,这是关于约瑟夫环的问题,还有为什么调用add那些函数不传地址就不能运行呢?求