判断一个单链表是否有环及环的链接点

来源:互联网 发布:淘宝上如何买到真牛排 编辑:程序博客网 时间:2024/05/16 01:36

给定一个单链表,只给出头指针h:

1、如何判断是否存在环?

2、如何知道环的长度?

3、如何找出环的连接点在哪里?

4、带环链表的长度是多少?

 

解法:

1、对于问题1,使用追赶的方法,设定两个指针slow、fast,从头指针开始,每次分别前进1步、2步。如存在环,则两者相遇;如不存在环,fast遇到NULL退出。

2、对于问题2,记录下问题1的碰撞点p,slow、fast从该点开始,再次碰撞所走过的操作数就是环的长度s。

3、问题3:有定理:碰撞点p到连接点的距离=头指针到连接点的距离,因此,分别从碰撞点、头指针开始走,相遇的那个点就是连接点。(证明在后面附注)

4、问题3中已经求出连接点距离头指针的长度,加上问题2中求出的环的长度,二者之和就是带环单链表的长度

void Isloop(Llink head)
{
 if(!head||!head->next)
  return;
 Llink p,q;
 bool loop=false;
 p=q=head->next;
 while(q&&q->next)//判断是否有环
 {
  p=p->next;
  q=q->next->next;
  if(p==q)
  {
   loop=true;
   break;
  }
 }
 if(!loop)
  cout<<"This link has not loop\n";
 else
 {
  cout<<"This link has a loop\n";
  Llink r=p;
  q=head->next;
  int nonloop=1,loopcount=1;

  //nonloop计算非环结点数,loopcount计算环上结点数
  do//计算环上的结点数
  {
   p=p->next;
   ++loopcount;
  }while(p!=r);
  --loopcount;
  while(p!=q)//得到环的入口结点,同时计算得到非环的结点数
  {
   p=p->next;
   q=q->next;
   ++nonloop;
  }
  --nonloop;
  cout<<"\nStart of loop: "<<p->data<<endl;  
  cout<<"\nCount of nonloop: "<<nonloop
      <<"\nCount of loop: "<<loopcount
      <<"\nCount of Linknode: "<<nonloop+loopcount<<endl;
 }
}

  

判断是否存在环的程序:

bool IsExitsLoop(slist *head)  
  1.  
  2.     slist *slow head, *fast head;  
  3.     while fast && fast->next   
  4.      
  5.         slow slow->next;  
  6.         fast fast->next->next;  
  7.         if slow == fast break 
  8.        
  9.     return !(fast == NULL || fast->next == NULL);  
  10.  

 

寻找环连接点(入口点)的程序:

slist* FindLoopPort(slist *head)  
  1.  
  2.     slist *slow head, *fast head;    
  3.     while fast && fast->next   
  4.      
  5.         slow slow->next;  
  6.         fast fast->next->next;  
  7.         if slow == fast break 
  8.        
  9.     if (fast == NULL || fast->next == NULL)  
  10.         return NULL;  
  11.     slow head;  
  12.     while (slow != fast)  
  13.      
  14.          slow slow->next;  
  15.          fast fast->next;  
  16.      
  17.     return slow;  

亦可以用类似与hash表的方法,即设立一个数组,将链表结点中的值做数组下标,当赋值冲突时就是环的接入点

  1.  bool isloop(Llink p)
    {
     if(!p||!p->next)
      return true;
     int a[MAXSIZE],n=0;
     memset(a,0,sizeof(int)*MAXSIZE);
     p=p->next;
     while(p)
     {
      if(a[p->data]==-1)//存在环时,会发生冲突
      {
       cout<<"\nLoop node: "<<p->data<<endl
        <<"\nLen of node: "<<n<<endl;
       return true;
      }
      a[p->data]=-1;
      ++n;
      p=p->next;
     }
     return false;
    }
    Llink CreatlinkLoop()
  2. //创建一个有环的链表
    {
     Llink head=new Lnode;
     //head->data=0;
     head->next=NULL;
     Lelemtype e;
     Llink q=head;
     int N=0;
     cout<<"input elems:";
     while(cin>>e)
     {
      Llink p=new Lnode;
      ++N;
      p->data=e;
      p->next=q->next;
      q->next=p;
      q=p;
     }
     cin.clear();
     cin.sync();
     srand(time(0));
     q->next=Findnode(head,rand()%N);//随机产生环的接入点
     return head;
    }
    Llink Findnode(Llink head,int n)//找出链表中的第n个结点
    {
     if(n<=0)
      return head;
     Llink p=head->next;
     for(int i=1;p&&i<n;++i)
      p=p->next;
     return p;
    }
0 0
原创粉丝点击