Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


how to check whether a linked list is circular.

Answers were Sorted based on User's Feedback



how to check whether a linked list is circular...

Answer / coder_1

Create two pointers, each set to the start of the list.
Update each as follows:

while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next; if (pointer2)
pointer2=pointer2->next;
if (pointer1 == pointer2) {
print (\"circular\n\");
}
}

If a list is circular, at some point pointer2 will wrap
around and be either at the item just before pointer1, or
the item before that. Either way, it?s either 1 or 2 jumps
until they meet.

Is This Answer Correct ?    86 Yes 34 No

how to check whether a linked list is circular...

Answer / pratyu

node *ptr1,*ptr2;
ptr1=head;
ptr2=head->next;
while(ptr2)
{
if(ptr1==ptr2)
printf("circular linked list");

ptr2=ptr2->next;
}
printf("not a circular linked list");

Is This Answer Correct ?    31 Yes 7 No

how to check whether a linked list is circular...

Answer / priya

This is the function to check if the linklist is circular:

bool CLinklist::ifcircular()
{
node *ptr1,*ptr2;
ptr1 = front;
ptr2 = front;

while(ptr1)
{
ptr1 = ptr1->link;
ptr2 = ptr2->link;

if(ptr2)
ptr2 = ptr2->link;

if(ptr1 == ptr2)
return 1;
}
}

Is This Answer Correct ?    15 Yes 7 No

how to check whether a linked list is circular...

Answer / shruti

consider home pointer as the starting pointer of the linked
list.
consider temp as the temporary pointer.


temp = home;
while(temp != NULL)
{
if(temp -> next == start)
{
flag = 1;
break;
}
else
flag = 0;
temp = temp -> next;
}

if(flag == 1)
printf("Circular");
else
printf("Not circular");

Is This Answer Correct ?    30 Yes 30 No

how to check whether a linked list is circular...

Answer / nirwal

hi All

specialy the two pointer solution techies


what about the loop in the link list ?

how you differentiate b/w circular list and list having the
loop?

Think again...........

Is This Answer Correct ?    4 Yes 4 No

how to check whether a linked list is circular...

Answer / abhi

while(q->next->next=NULL)
{
p=p->next;
q=q->next->next;
if(p==q)
{
printf("loof is find");
break;
}
}

Is This Answer Correct ?    2 Yes 2 No

how to check whether a linked list is circular...

Answer / shahid khan abbasi

bool hasCircle(List l)
{
Iterator i = l.begin(), j = l.begin();
while (true) {
// increment the iterators, if either is at the end,
you're done, no circle
if (i.hasNext()) i = i.next(); else return false;

// second iterator is travelling twice as fast as first
if (j.hasNext()) j = j.next(); else return false;
if (j.hasNext()) j = j.next(); else return false;

// this should be whatever test shows that the two
// iterators are pointing at the same place
if (i.getObject() == j.getObject()) {
return true;
}
}
}

Is This Answer Correct ?    2 Yes 3 No

how to check whether a linked list is circular...

Answer / burmeselady

start from pointer1
check all node's next pointer is null or not starting from
pointer1 until pointer1
if a node next pointer is null=> it is not circular.
else it is circular

Is This Answer Correct ?    5 Yes 7 No

how to check whether a linked list is circular...

Answer / ishan

by checking whether link field in the last node contains
the address of the first node.

Is This Answer Correct ?    17 Yes 20 No

how to check whether a linked list is circular...

Answer / lohitha

node *ptr1,*ptr2;
ptr1=head;
ptr2=head->next;
while(ptr2)
{
if(ptr1==ptr2)
{
printf("circular linked list");
break;
}
ptr2=ptr2->next;
}
printf("not a circular linked list");

Is This Answer Correct ?    3 Yes 6 No

Post New Answer

More C Code Interview Questions

write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30

0 Answers  


main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }

1 Answers  


how to swap 3 nos without using temporary variable

4 Answers   Satyam,


main() { int i, n; char *x = “girl”; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(“%s\n”,x); x++; } }

2 Answers  


Code for 1>"ascii to string" 2>"string to ascii"

1 Answers   Aricent, Global Logic,


find A^B using Recursive function

2 Answers  


main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }

1 Answers  


main() { char not; not=!2; printf("%d",not); }

1 Answers  


Derive expression for converting RGB color parameters to HSV values

1 Answers  


code of a program in c language that ask a number and print its decremented and incremented number.. sample output: input number : 3 321123

1 Answers   HCL,


#include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }

1 Answers  


Sir... please give some important coding questions asked by product companies..

0 Answers  


Categories