判断单链表是否有环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了判断单链表是否有环相关的知识,希望对你有一定的参考价值。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 typedef struct Node{
  5     int data;
  6     struct Node* next;
  7 }Node;
  8 typedef struct Node* LinkList;
  9 
 10 void InitialLinkList(LinkList *L){
 11     (*L) = (LinkList)malloc(sizeof(struct Node));
 12     if(!(*L)){
 13         printf("Error:InitialLinkList:malloc\n");
 14         exit(1);
 15     }
 16     (*L)->next = NULL;
 17 }
 18 void CreateSimpleCycleList_withoutLoop(LinkList *L,int number){
 19     int count;
 20     LinkList new,tail = (*L);
 21     printf("Create SimpleCycleList_withoutLoop\n");
 22     for(count = 1;count <= number; count++){
 23         new = (LinkList)malloc(sizeof(struct Node));
 24         if(!(new)){
 25             printf("Error:CreateSimpleCycleList_withoutLoop\n");
 26             exit(1);
 27         }
 28         printf("please enter %d element:  ",count);
 29         scanf("%d",&(new->data));
 30         new->next = tail->next;
 31         tail->next = new;
 32         tail = new;
 33     }
 34 }
 35 void CreateSimpleCycleList_withLoop(LinkList *L,int number,int loopInIndex){
 36 /* 第一个参数LinkList是要创建的有环的链表
 37  * 第二个参数number是链表的长度
 38  * 第三个参数loopInIndex是指示环由链表的末尾指向了第几个元素,0则表示是指向了头结点。
 39  * */
 40     int count;
 41     LinkList new,temp = *L,temp1 = *L;
 42     if(loopInIndex > number){
 43         printf("Error:CreateSimpleCycleList_withLoop:loopInIndex\n");
 44     }
 45     else{
 46         printf("Create SimpleCycleList_withLoop\n");
 47         for(count = 1; count <= number; count++){
 48             new = (LinkList)malloc(sizeof(struct Node));
 49             if(!new){
 50                 printf("Error:CreateSimpleCycleList_withLoop\n");
 51                 exit(1);
 52             }
 53             printf("please enter %d element  ",count);
 54             scanf("%d",&(new->data));
 55             temp->next = new;
 56             new->next = *L;
 57             temp = new;
 58         }
 59         for(count = 0;count < loopInIndex; count++){
 60             temp1 = temp1->next;
 61         }
 62         temp->next = temp1;
 63     }
 64 }
 65 void JodgeLoop(LinkList L){
 66     LinkList p = L,q = L;
 67     int step_o = 0,step_i = 0;
 68     int isLoop = 0;
 69     while(p){
 70         step_i = 0;
 71         q = L;
 72         while(q != p){
 73             q = q->next;
 74             step_i++;
 75         }
 76         if(step_o != step_i){
 77             isLoop = 1;
 78             break;
 79         }
 80         p = p->next;
 81         step_o++;
 82     }
 83     if(isLoop == 1)
 84         printf("Exist loop in this List.Appear in position %d\n",step_i);
 85     else
 86         printf("Dose no Exist loop in this List!\n");
 87 }
 88 void JodgeLoop_2(LinkList L){
 89     LinkList quick = L,slow = L;
 90     int isLoop = 0;
 91     while(quick != NULL && quick->next != NULL){
 92         quick = quick->next->next;
 93         slow = slow->next;    
 94         if(quick == slow){
 95             isLoop = 1;
 96             break;
 97         }
 98     }
 99     if(isLoop)
100         printf("Exist loop in this List.\n");
101     else
102         printf("Dose no Exist loop in this List!\n");
103 }
104 void Display_withoutLoop(LinkList L){
105     while(L->next != NULL){
106         L = L->next;
107         printf("%d ",L->data);
108     }
109     printf("\n");
110 }
111 
112 int main(){
113     LinkList L1;
114     LinkList L2;
115     InitialLinkList(&L1);
116     InitialLinkList(&L2);
117     CreateSimpleCycleList_withLoop(&L1,5,1);
118     CreateSimpleCycleList_withoutLoop(&L2,4);
119     JodgeLoop(L1);
120     JodgeLoop(L2);
121     JodgeLoop_2(L1);
122     JodgeLoop_2(L2);
123     return 0;
124 }

 

以上是关于判断单链表是否有环的主要内容,如果未能解决你的问题,请参考以下文章

判断单链表是否有环

判断一个单链表是否有环

判断链表中是否有环 ----- 有关单链表中环的问题

判断单链表是否有环

数据结构与算法判断一个单链表是否有环及环入口

如何判断一个单链表是否有环?