Y
Yuri CHUANG
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
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