473,494 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

loop in a linked list

6 New Member
Q: Plz send me the code for this question.

How to check whether there exists any loop in a linked list. thanks.
regards
sanjay
Jun 17 '07 #1
3 1524
weaknessforcats
9,208 Recognized Expert Moderator Expert
Traverse the linked list and keep the addresses of each node in a table sorted by address. If the address of a next node is already in the table, you have a loop.

I assume you are using C.

In C++ you use the list template which does not have these problems.
Jun 18 '07 #2
JosAH
11,448 Recognized Expert MVP
Traverse the linked list and keep the addresses of each node in a table sorted by address. If the address of a next node is already in the table, you have a loop.
There's a nice trick that doesn't need any table: imagine two ants running over
your list; one of them runs twice as fast as the other; when they meet again
during the run there's a loop in the list.

kind regards,

Jos
Jun 18 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
There's a nice trick that doesn't need any table: imagine two ants running over
your list; one of them runs twice as fast as the other; when they meet again
during the run there's a loop in the list.
Always a slick solution. I haven't heard about this one.
Jun 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
4093
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
11438
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
2987
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
16894
by: sonu | last post by:
Hi All, here i have a daught how can i find wheather a linked list contains a loop or not.
3
2744
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
3388
by: finer | last post by:
i'm new to C#, but i'm trying to write a circular doubly linked list. theres a bug in my code somewhere (i think in my find() method or ToGo() method) that causes an endless loop after i insert a...
2
2327
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
6424
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
8605
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...
7
5756
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
7157
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
7195
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...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5453
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 project—planning, coding, testing,...
1
4889
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...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.