首页 > 其他 > 详细

猴王问题,环形链表删除操作

时间:2014-01-16 00:09:22      阅读:331      评论:0      收藏:0      [点我收藏+]

环形链表,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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!