Skip to content

Commit c07cc82

Browse files
Cycle in a linked list in C
1 parent d1b14f1 commit c07cc82

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

linked-list/cycle-in-linked-list.c

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<stdio.h>
7+
#include<stdlib.h>
8+
9+
struct node
10+
{
11+
int info;
12+
struct node *link;
13+
};
14+
15+
struct node *createList(struct node *start);
16+
void displayList(struct node *start);
17+
struct node *insertInBeginning(struct node *start,int data);
18+
struct node *insertAtEnd(struct node *start,int data);
19+
void insertCycle(struct node *start, int x);
20+
struct node *findCycle(struct node *start);
21+
void removeCycle(struct node *start, struct node *pC);
22+
23+
main()
24+
{
25+
struct node *start=NULL;
26+
struct node *ptr;
27+
int x;
28+
29+
start=createList(start);
30+
31+
if(start==NULL)
32+
exit(1);
33+
34+
x=40;
35+
insertCycle(start,x);
36+
37+
ptr=findCycle(start);
38+
39+
if(ptr==NULL)
40+
printf("List is NULL terminated\n");
41+
else
42+
{
43+
printf("Cycle found in the list\n");
44+
removeCycle(start, ptr);
45+
printf("Cycle Removed, now list is NULL terminated\n");
46+
}
47+
displayList(start);
48+
}/*End of main()*/
49+
50+
51+
/*Insert a cycle at node containing x*/
52+
void insertCycle(struct node *start, int x)
53+
{
54+
struct node *p,*px=NULL, *prev, *ptr;
55+
56+
p=start;
57+
while(p!=NULL)
58+
{
59+
if(p->info==x)
60+
px=p;
61+
prev=p;
62+
p=p->link;
63+
}
64+
if(px!=NULL)
65+
prev->link=px;
66+
}
67+
68+
/*Returns NULL if the list is NULL terminated otherwise returns
69+
pointer to node where both slow and fast pointers meet*/
70+
struct node *findCycle(struct node *start)
71+
{
72+
struct node *slowP, *fastP;
73+
74+
if(start->link==NULL) /*only one element*/
75+
return NULL;
76+
77+
slowP=fastP=start;
78+
79+
while(fastP!=NULL && fastP->link!=NULL)
80+
{
81+
slowP=slowP->link;
82+
fastP=fastP->link->link;
83+
if( slowP==fastP )
84+
return slowP;
85+
}
86+
return NULL;
87+
}/*End of findCycle()*/
88+
89+
void removeCycle(struct node *start, struct node *pC)
90+
{
91+
struct node *p, *q;
92+
int i, lenCycle, lenRemList, lengthList;
93+
94+
printf("Node at which the cycle was detected is %d\n",pC->info);
95+
96+
p=q=pC;
97+
lenCycle=0;
98+
do
99+
{
100+
lenCycle++;
101+
q=q->link;
102+
}while(p!=q);
103+
printf("Length of cycle is : %d\n",lenCycle);
104+
105+
lenRemList=0;
106+
p=start;
107+
while(p!=q)
108+
{
109+
lenRemList++;
110+
p=p->link;
111+
q=q->link;
112+
}
113+
114+
printf("Number of nodes not included in the cycle are : %d\n",lenRemList);
115+
116+
lengthList = lenCycle + lenRemList;
117+
printf("Length of the list is : %d\n",lengthList);
118+
119+
p=start;
120+
for(i=1; i<=lengthList-1; i++)
121+
p=p->link;
122+
p->link=NULL;
123+
}
124+
125+
struct node *createList(struct node *start)
126+
{
127+
int i,n,data;
128+
printf("Enter the number of nodes : ");
129+
scanf("%d",&n);
130+
start=NULL;
131+
if(n==0)
132+
return start;
133+
134+
printf("Enter the element to be inserted : ");
135+
scanf("%d",&data);
136+
start=insertInBeginning(start,data);
137+
138+
for(i=2;i<=n;i++)
139+
{
140+
printf("Enter the element to be inserted : ");
141+
scanf("%d",&data);
142+
start=insertAtEnd(start,data);
143+
}
144+
return start;
145+
}/*End of createList()*/
146+
147+
void displayList(struct node *start)
148+
{
149+
struct node *p;
150+
if(start==NULL)
151+
{
152+
printf("List is empty\n");
153+
return;
154+
}
155+
p=start;
156+
printf("List is :\n");
157+
while(p!=NULL)
158+
{
159+
printf("%d ",p->info);
160+
p=p->link;
161+
}
162+
printf("\n\n");
163+
}/*End of displayList() */
164+
165+
struct node *insertInBeginning(struct node *start,int data)
166+
{
167+
struct node *tmp;
168+
tmp=(struct node *)malloc(sizeof(struct node));
169+
tmp->info=data;
170+
tmp->link=start;
171+
start=tmp;
172+
return start;
173+
}/*End of insertInBeginning()*/
174+
175+
struct node *insertAtEnd(struct node *start,int data)
176+
{
177+
struct node *p,*tmp;
178+
tmp=(struct node *)malloc(sizeof(struct node));
179+
tmp->info=data;
180+
p=start;
181+
while(p->link!=NULL)
182+
p=p->link;
183+
p->link=tmp;
184+
tmp->link=NULL;
185+
return start;
186+
}/*End of addAtEnd()*/
187+

0 commit comments

Comments
 (0)