473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

loop in a single linked list

Hi All,

here i have a daught how can i find wheather a linked list contains a
loop or not.

Mar 27 '06 #1
10 16890
one way is to mark the element you visited

Mar 27 '06 #2
suppose my node contains like the following declaration.

struct node
{
int info;
struct node *next_ptr;
}
then how can i mark the node is visited or not.
please specify the code

Mar 27 '06 #3
*if the linked list is read only, take two pointer approach..both
pointing to beginning of linked list. now increment p1 by 1 and p2 by 2
and compare both. if they are equal there is a cycle. repeat this
untill p2 points to null.

*if u have the condition not to modify the node but u can change the
links, then reverse the linked list. if u reach the head node then
there is a cycle ....

Mar 27 '06 #4
Can u wtite the code. suppose we have 100 nodes then 100 node is
pointing 49the node then what will be the situation

Mar 27 '06 #5
sonu wrote:
Can u wtite the code. suppose we have 100 nodes then 100 node is
pointing 49the node then what will be the situation

Please don't use abbreviations like "u". They make your post extra hard
to read. Also, notice how almost everyone else quotes the previous post
in replies? You should as well. See below.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 27 '06 #6
zh*****@gmail.com wrote:
sonu wrote:
Hi All,

here i have a daught how can i find wheather a linked list contains a loop or not.
one way is to mark the element you visited


You've already been requested to include context in your posts. Notice
this reply to you, wherein, I'm quoting both you and the poster before.
This ensures that each post is semi-independent and can be easily
understood by readers who may use an unthreaded newsreader or those who
may be reading your post after the previous ones have expired. *Please*
read the material at the following URLs:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>

Mar 27 '06 #7
sonu wrote:
suppose my node contains like the following declaration.

struct node
{
int info;
struct node *next_ptr;
}
then how can i mark the node is visited or not.
please specify the code


You could define a member of type bool whose initial value could be
false, and which you can set to true after you've visited the node.

Oh, and by the way, include context in your posts, where possible.
Please review the following URLs:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>

Mar 27 '06 #8
the code for the first method goes as

#include <stdio.h>

struct node
{
struct node *next;
int data;
}

int main()
{
struct node *list, *p2, *p1;

load_list_somehow(&list);

/* Test trivial no-loop and trivial loop */
if (list == NULL)
{
/* No loop, uninitialised list */
printf("No loop\n");
}

if (list->next == list)
{
/* Trivial loop */
printf("Loop detected\n");
return 0;
}

p1 = list;
p2 = list->next;

while (p2 != NULL && p1 != p2)
{
/* Increment p1 once, p2 twice */
p1 = p1->next;
p2 = p2->next;
if (p2 != NULL)
p2 = p2->next;
}

if (p2 == NULL)
{
/* We got to the end of the list */
printf ("No loop\n");
}
else
{
/* p1 == p2, and the p2 "lapped" the p1
* in the loop */
printf ("Loop detected\n");
}
return 0;
}

the second method is obvious , try reversing a linked list with a cycle
...you will realize.

i hope the solution is clear enough
there is on more most efficient solution of this problem:
if you want to go through it try google on
Floyd's cycle-finding algorithm or The Tortoise and the Hare Algorithm

enjoy
thanks
jitendra

Mar 27 '06 #9

santosh 写道:
zh*****@gmail.com wrote:
sonu wrote:
Hi All,

here i have a daught how can i find wheather a linked list contains a loop or not.

one way is to mark the element you visited


You've already been requested to include context in your posts. Notice
this reply to you, wherein, I'm quoting both you and the poster before.
This ensures that each post is semi-independent and can be easily
understood by readers who may use an unthreaded newsreader or those who
may be reading your post after the previous ones have expired. *Please*
read the material at the following URLs:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>


Mar 28 '06 #10
santosh wrote:
zh*****@gmail.com wrote:
sonu wrote:
Hi All,

here i have a daught how can i find wheather a linked list contains a loop or not.

one way is to mark the element you visited


You've already been requested to include context in your posts. Notice
this reply to you, wherein, I'm quoting both you and the poster before.
This ensures that each post is semi-independent and can be easily
understood by readers who may use an unthreaded newsreader or those who
may be reading your post after the previous ones have expired. *Please*
read the material at the following URLs:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>


Thanks for your suggestion!!

Mar 28 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
5
by: Jani Yusef | last post by:
Based on an interview question I heard of but did not know the answer to....... How do you find and remove a loop from a singly linked list? In a google groups search I found the following code...
7
by: ssantamariagarcia | last post by:
I have found a problem while using a while statement and a linked list. I had never met this matter before....and I am wondering that if you have , please tell me what it is wrong. I am...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
11
by: sam_cit | last post by:
Hi Everyone, I want to actually reverse a single linked list without using many variables, i have a recurssive solution, but i wanted an iterative one. can anyone help me on this?
3
by: rameshwar83 | last post by:
Suppose we have a linked list..... haviing loop..... suggest me some algo to remove loop from list..... Ex. A->B->C->D->E->F->G->H->I->E in this list node iI points to node E .... make...
2
by: Rohit kumar Chandel | last post by:
How to find whether there is a loop in a linked list. suppose I have a linked list in the shape of numeral 9(next pointer of last node points to somewhere in the middle of list instead of pointing...
2
by: sathishc58 | last post by:
Hi Can anyone explain me the difference between circular linked list and looped linked list? 1) In Circular linked list Next part of last node will be pointing to the first nodes starting...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.