环形链表,5个猴子坐成一圈,每数3个出去一个,最后剩下的就是猴王。供环形链表删除操作参考! 结构体是借用原来的学生,代表猴子了,哈哈,学生都像猴子一样聪明嘛,哈哈
#include "stdio.h"
#include "conio.h"
#include <string.h>
#include "math.h"
#define MNum 5
struct STUDENTINFO
{
long Num; /*代表猴子的编号*/
struct STUDENTINFO *Next;
};
int main()
{
int LIndex = 0;
int Count = 0;
struct STUDENTINFO Student[MNum],*Head,*p1,*p2;
/*初始化每个节点*/
for(LIndex=0;LIndex<MNum; LIndex++)
{
Student[LIndex].Num = LIndex + 1;
Student[LIndex].Next = &Student[LIndex + 1];
if(LIndex == MNum-1)
{
Student[LIndex].Next = &Student[0];
}
}
Head = &Student[MNum-1];
p1 = Head;
while(p1 != p1->Next)
{
Count++;
p2 = p1->Next;
p2 = p2->Next;
p1 = p2->Next;
printf("%d======%d is out\r\n",Count,p1->Num);
p2->Next = p1->Next;
};
printf("the hou wang is:%d\r\n",p2->Num);
getch();
return 1;
}
运行结果:
1======3 is out
2======1 is out
3======5 is out
4======2 is
out
5======4 is out
the hou wang is:4
原文:http://www.cnblogs.com/sky-inter/p/3516713.html