how to check whether a linked list is circular.
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
How can I Create a C program in splitting set of characters to specific subsets. Example: INPUT SET OF CHARACTERS: Therefore, my dear brothers and sisters, stand firm. Let nothing move you. Always give yourselves fully to the work of the Lord, because you know that your labor in the Lord is not in vain. SPLIT INTO HOW MANY CHARACTERS PER SUBSETS: 10 OUTPUT: Therefore, my dear b rothers an d sisters, stand fir m. Let not hing move you. Alway s give you rselves fu lly to the work of t he Lord, b ecause you know that your labo r in the L ord is not in vain.
main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }
main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }
Finding a number which was log of base 2
Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number
Is the following code legal? struct a { int x; struct a *b; }
write a c program to print magic square of order n when n>3 and n is odd?
Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2])
#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }
Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector
Question: We would like to design and implement a programming solution to the reader-writer problem using semaphores in C language under UNIX. We assume that we have three readers and two writers processes that would run concurrently. A writer is to update (write) into one memory location (let’s say a variable of type integer named temp initialized to 0). In the other hand, a reader is to read the content of temp and display its content on the screen in a formatted output. One writer can access the shared data exclusively without the presence of other writer or any reader, whereas, a reader may access the shared memory for reading with the presence of other readers (but not writers).
main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?