提问:链表应用的一个程序:围圈报数
某书上摘抄的程序:
13个人围成一圈,从第一个人开始顺序报号1、2、3。凡报到“3”者退出圈子。找出最后留在圈子中的人的原来的序号。
若程序执行正常,应有如下显示。
-----------------------------
sequence that persons leave the circle:
3 6 9 12 2 7 11 4 10 5 1 8
The last one is 13.
-----------------------------
不知道哪里错了?程序运行时就死循环了好像。。。
<程序源码>
-----------
#include <stdio.h>
#define N 13
struct person
{
int number;
int nextp;
}link[N+1];
void main()
{
int i,count,h;
for(i=1;i<N;i++)
{
if(i==N)
link[i].nextp=1;
else
link[i].nextp=i+1;
link[i].number=i;
}
printf("\n");
count=0;
h=N;
printf("sequence that persons leave the circle:\n");
while(count<N-1)
{
i=0;
while(i!=3)
{
h=link[h].nextp;
if(link[h].number)
i++;
}
printf("%4d",link[h].number );
link[h].number=0;
count++;
}
printf("\nThe last one is");
for(i=1;i<=N;i++)
if(link[i].number )
printf("%3d",link[i].number );
}