约瑟夫问题的升级版,判断总是出错
c语言吧
全部回复
仅看楼主
level 12
知表不言 楼主
这段代码由约瑟夫问题更改而来,只不过每次间隔个数是随机的(由被删除的结构中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;
}
}
}
2017年08月13日 07点08分 1
level 12
知表不言 楼主
编译能通过,都是在运行时出现错误的[委屈]
2017年08月13日 07点08分 2
level 8
当head->next被free(游戏进行到一定程度第一个人肯定会死的),tail->next仍然指向head->next而没有重新赋值,然后就像
这样
2017年08月13日 08点08分 3
level 8
还有一个,也是在最后一个函数里。如果count==m成立成功执行if下面的语句并成功free掉temp并进入下一轮循环,那么问题来了,prior=temp是不是会出错呢。。
2017年08月13日 09点08分 4
temp=temp->next更是有毛病
2017年08月13日 09点08分
感谢大神终于想通了,删除链表时函数得传入**head,还有count重置前得让temp跳到被删除的下一个节点,都是因为访问到已经被free释放了的地址
2017年08月13日 10点08分
@知表不言 楼上的问题要怎么解决
2017年08月13日 10点08分
@知表不言 想了半天然后感觉自己不适合学数据结构
2017年08月13日 10点08分
level 8
试下这段代码,看有什么bug
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) (rand()%x)
#define N 41 //人数
#define M 23 //报数上限
struct List
{
int flag; //标志变量,1为存活,0则死亡
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;
tail->flag=1;
for(int i = 1; i <= n; i++)
{
temp = (struct List *)malloc(sizeof(struct List));
temp->num = n - i;
temp->flag=1;
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->flag);
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,i=0;
int m = head->next->data;
struct List *temp = head;
//struct List *p;
while(i<N)
{
temp=temp->next;
if(temp->flag==0)
;
else
{
count++;
if(count == m)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
temp->flag=0;
count = 0;
i++;
}
}
}
}
2017年08月13日 11点08分 5
你这代码运行也是正常的[哈哈]
2017年08月13日 13点08分
level 8
我跑出来没毛病
[滑稽]
2017年08月13日 11点08分 6
我修改完的代码在楼下
2017年08月13日 13点08分
level 12
知表不言 楼主
#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;
if(temp == *head)
{
*head = temp->next;
}
free(temp);
break;
}
else if(count == m)
{
printf("号码为%2d的人自杀了!\t",temp->num);
m = temp->data;
printf("他的报数是%2d\n",m);
prior->next = temp->next;
if(temp == *head)
{
*head = temp->next;
}
free(temp);
temp = prior;
count = 0;
}
}
}
2017年08月13日 13点08分 7
1