本文介绍了帮助:约瑟夫斯之环的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是CPL的初学者。我使用DoubleLinkList数据结构编写了一个关于约瑟夫斯的Ring

问题的程序。 />
我很困惑,我认为我的

代码中确实没有错误。但是,编译器有时会显示一些奇怪的错误,有时可以

在运行时遇到一个未知的麻烦,并且没有

结果。那么,有人可以给我一些帮助吗?非常感谢!

这是我的代码:

------------------------ -------------------------------------------------- ------------------------


#include< stdio.h>

#include< stdlib.h>


typedef struct DuLNode

{

int data;

struct DuLNode * previous;

struct DuLNode * next;

} DuLNode,* DuLinkList;


int ex_16( int *,int,int);


int main(int argc,char * argv [])

{

int a [] = {1,2,3,4,5,6,7,8,9,10,11,12,-1}; //使用-1作为

的标志数组的结尾

int n = 8,k = 5;

printf(最后一个数字是%d,ex_16(a,n,k)) ;

返回0;

}


int ex_16(int * a,int n,int k)

{

DuLinkList L;

L =(DuLinkList)malloc(sizeof(DuLNode));

L->数据= 0; L-> next = L; L-> prior = L; //使用

头节点创建DuLinkList


int * p = a;

while(* p!= - 1)

{

DuLinkList DL ;

DL =(DuLinkList)malloc(sizeof(DuLNode));

DL-> data = * p;

DL-> ; previous = L-> prior; L-> prior-> next = DL;

DL-> next = L; L-> prior = DL;

L-> data ++; // L->数据是List的长度排除

头节点

p ++;

} //启动DuLinkList


DuLinkList q; int i,temp;


while(1)

{

if(L-> next; = L)

{

q = L;

for(i = 0; i< (n-1)%L->数据; i ++)

{

q = q-> next;

temp = q- >数据;

}

q-> prior-> next = q-> next;

q-> next - > previous = q-> previous;

free(p);

L-> data - ;

}

其他休息;


如果(L->先前!= L)

{

q = L;

for(i = 0; i<(k-1)%L->数据; i ++)

{

q = q-> previous;

temp = q-> data;

}

q-> prior-> next = q-> next;

q-> next-> prior = q-> previous;

free(p);

L-> data--;

}

else break;

} //删除位于该点的节点/>

返回temp;

} // ex_16

---------------------------- -------------------------------------------------- ----------------


请告诉我代码中的任何错误或错误习惯。

Hi,
I''m the beginner of the CPL.I write a program about the problem of Ring
of Josephus,using DoubleLinkList data structure.
I''m very confused that I think there is really no error in my
code.But,the compiler sometimes show some strange errors,sometimes can
pass through with an unknown trouble when it is running,and no
result.So,could anyone give me some help? Thank you very much!
Here is my code:
--------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

typedef struct DuLNode
{
int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;

int ex_16(int *,int,int);

int main(int argc, char *argv[])
{
int a[]={1,2,3,4,5,6,7,8,9,10,11,12,-1};//use -1 as the flag of
ending of the array
int n=8,k=5;
printf("The last number is %d",ex_16(a,n,k));
return 0;
}

int ex_16(int *a,int n,int k)
{
DuLinkList L;
L=(DuLinkList)malloc(sizeof(DuLNode));
L->data=0;L->next=L;L->prior=L;//create the DuLinkList with the
head-node

int *p=a;
while(*p!=-1)
{
DuLinkList DL;
DL=(DuLinkList)malloc(sizeof(DuLNode));
DL->data=*p;
DL->prior=L->prior;L->prior->next=DL;
DL->next=L;L->prior=DL;
L->data++; //L->data is the length of the List exclude the
head-node
p++;
}//initiate the DuLinkList

DuLinkList q;int i,temp;

while(1)
{
if(L->next!=L)
{
q=L;
for(i=0;i<(n-1)%L->data;i++)
{
q=q->next;
temp=q->data;
}
q->prior->next=q->next;
q->next->prior=q->prior;
free(p);
L->data--;
}
else break;

if(L->prior!=L)
{
q=L;
for(i=0;i<(k-1)%L->data;i++)
{
q=q->prior;
temp=q->data;
}
q->prior->next=q->next;
q->next->prior=q->prior;
free(p);
L->data--;
}
else break;
}//delete the node which is at the point

return temp;
}//ex_16
----------------------------------------------------------------------------------------------

Please tell any faults or wrong habits in my code.

推荐答案




p只是指向你的数组,你没有malloc。你的意思是免费(q),

我想。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999


电子邮件:rjh在上面的域名(但显然放弃了www)



p just points to your array, which you didn''t malloc. You meant to free(q),
I think.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)





我从来没有听说过约瑟夫之环。所以我查了一下。


对于实际的约瑟夫之环, n = 13且k = 3。但是,所有戒指都停在

nth元素上。你有n = 8,k = 5。不幸的是,因为第13个元素是-1而不是第8个元素,所以你的戒指不会停留在第8个元素的第b个元素上。此外,你需要

数组中的第n个元素,即n = 13时缺少13个元素。您可能希望将
设置为'a''作为指针,malloc()为其空间,然后通过n和-1填充1



Rod Pemberton



I''d never heard of the "Ring of Josephus." So I looked it up.

For the actual "Ring of Josephus," n=13 and k=3. But, all rings stop on the
nth element. You have n=8, and k=5. Unfortunately, your ring won''t stop on
the 8th element since the 13th element is -1, not the 8th. Also, you need
the nth element in the array, i.e., 13 is missing for n=13. You may want to
setup ''a'' as a pointer, malloc() the space for it, and then fill it with 1
through n and -1.
Rod Pemberton



这篇关于帮助:约瑟夫斯之环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:06