这段代码由约瑟夫问题更改而来,只不过每次间隔个数是随机的(由被删除的结构中data数值决定),可是问题就是出现在倒数第十一行的m,把判断条件改成定值3程序就能正常完成,如果直接这样的代码编译,会出现segmentation fault ,pointer being freed was not allocated等等错误,这个该怎么修改才能正常啊
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) (rand()%x)
#define N 41 //人数
#define M 23 //报数上限
struct List
{
int num;
int data;
struct List *next;
};
struct List *CreatList(int n);
void PrintList(struct List *head);
void Suicide(struct List *head);
int main(void)
{
srand((int)time(0));
struct List *head = CreatList(N);
//PrintList(head);
Suicide(head);
return 0;
}
struct List *CreatList(int n)
{
struct List *temp;
struct List *tail = (struct List *)malloc(sizeof(struct List));
struct List *head = tail;
tail->num = n;
for(int i = 1; i <= n; i++)
{
temp = (struct List *)malloc(sizeof(struct List));
temp->num = n - i;
do
{
temp->data = random(M);
}while(!(temp->data));
temp->next = head;
head = temp;
}
do
{
tail->data = random(M);
}while(!(tail->data));
tail->next = head->next;
return head;
}
void PrintList(struct List *head)
{
struct List *temp = head->next;
do
{
printf("%d\t", temp->num);
printf("%d\n", temp->data);
temp = temp->next;
}while(temp != head->next);
}
void Suicide(struct List *head)
{
int count = 0;
int m = head->next->data;
struct List *temp = head;
struct List *prior = NULL;
while(1)
{
count++;
prior = temp;
temp = temp->next;
if(temp->next == temp)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
free(temp);
break;
}
if(count == m)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
free(temp);
count = 0;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) (rand()%x)
#define N 41 //人数
#define M 23 //报数上限
struct List
{
int num;
int data;
struct List *next;
};
struct List *CreatList(int n);
void PrintList(struct List *head);
void Suicide(struct List *head);
int main(void)
{
srand((int)time(0));
struct List *head = CreatList(N);
//PrintList(head);
Suicide(head);
return 0;
}
struct List *CreatList(int n)
{
struct List *temp;
struct List *tail = (struct List *)malloc(sizeof(struct List));
struct List *head = tail;
tail->num = n;
for(int i = 1; i <= n; i++)
{
temp = (struct List *)malloc(sizeof(struct List));
temp->num = n - i;
do
{
temp->data = random(M);
}while(!(temp->data));
temp->next = head;
head = temp;
}
do
{
tail->data = random(M);
}while(!(tail->data));
tail->next = head->next;
return head;
}
void PrintList(struct List *head)
{
struct List *temp = head->next;
do
{
printf("%d\t", temp->num);
printf("%d\n", temp->data);
temp = temp->next;
}while(temp != head->next);
}
void Suicide(struct List *head)
{
int count = 0;
int m = head->next->data;
struct List *temp = head;
struct List *prior = NULL;
while(1)
{
count++;
prior = temp;
temp = temp->next;
if(temp->next == temp)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
free(temp);
break;
}
if(count == m)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
free(temp);
count = 0;
}
}
}
归海一刀


