《数据结构》课程设计
约瑟夫环的答案是:
typedef struct node
{int num;
struct node *next;
} linklist;
linklist *creat(int n)
{linklist *s,*p,*head;
int i;
s=(linklist*)malloc(sizeof(linklist));
head=s;
s->num=1;
p=s;
for(i=2;inum=i;
p->next=s;
p=s;
}
p->next=head;
return (head);
}
void slect(linklist *head,int m,int n)
{linklist *p,*q;
int i,t,k=1;
p=head;
t=1;
q=p;
do
{p=q->next;
t=t+1;
if(t%m==0)
{printf(%4d,p->num);
q->next=p->next;
k++;
free(p);
}
else q=p;
}while(k
}
void main()
{int n,m;
linklist *a,*head;
printf(Input the total numbern);
scanf(%d,&n);
printf(Input the number to call:n);
scanf(%d,&m);
head=creat(n);
slect(head,m)
;
}
至于,八皇后的问题.不太清楚!