Pls check my code from the line 37 to 52. code is at the link https://ide.codingblocks.com/s/233769 . I am unable to make the cycle in the linked list. It is showing runtime error.
Circular linked list
void makeCycle(node *&head){
node *i=head;
node *prev;
while(i!=NULL){
node *j=head;
while(j!=i){
if(j->data==i->data){
prev->next=j;
return;
}
j=j->next;
}
prev=i;
i=i->next;
}
}
prev is not initialized it contains garbage’
and you are writing prev->next
hence it give runtime error
but where you want to make cycle? at end ??
we are given input like 1 2 3 4 5 2 3 -1
So we have to make a cycle from 5 to 2 because after 5 , 2 and 3 are repeating. So we have to make a cycle. And I have initialized prev in the lower part of the outer for loop. Prev-> next should not give an error here. Even if I am initializing prev with head, it is giving the same error.
if you intialize the prev then it should not give error
as in first iteration
i=head;
j=head;
so inner while loop condition hold true
and inside it if is also true
so prev->next =j
and then you return
modify your logic