forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinding_loops_in_LinkedList.c
More file actions
86 lines (86 loc) · 2.14 KB
/
Copy pathfinding_loops_in_LinkedList.c
File metadata and controls
86 lines (86 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
struct node
{
int data;
struct node *next;
}*common=NULL;
struct node *insert(struct node *newn,struct node *header){
struct node *ptr;
if(header==NULL){
header=newn;
return header;
}
ptr=header;
while (ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=newn;
return header;
}
int check_for_loop(struct node *header){
struct node *tortoise,*hare;
hare=tortoise=header;
while(hare!=NULL&&hare->next!=NULL){
hare=hare->next->next;
tortoise=tortoise->next;
if(hare==tortoise){
common=hare;
return 1;
}
}
return 0;
}
int length_of_loop(){
struct node *p;
int count=0;
p=common;
while(p->next!=common){
count++;
p=p->next;
}
return count;
}
int main()
{
srand(time(0));
int n,i=0,k;
struct node **arr;
struct node *newn,*header=NULL,*tailor=NULL;
printf("\nFirstly Creating the linked list\n");
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
arr=(struct node**)malloc(n*sizeof(struct node));
while(i<n)
{
newn=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data : ");
arr[i++]=newn;
scanf("%d",&newn->data);
newn->next=NULL;
tailor=newn;
header=insert(newn,header);
}
int random=rand()%7;
printf("\n%d\n",random);
if(random>=2&&random<=4)
tailor->next=NULL;
else
tailor->next=arr[rand()%i];
printf("\nCreation of Linked List successful which may have a loop or may not have one\n");
int check_loop=check_for_loop(header);
if(check_loop==1){
printf("\nThere is loop in the linked list\n");
printf("\nThe number of nodes in the loop is : %d\n\n",length_of_loop()+1);
}
else
{
printf("\nThere are no loops in the linked list\n");
printf("\nHence length of the loop is 0\n");
}
printf("\nPress Any Key to stop the Execution\n");
getch();
}