473,748 Members | 5,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16923
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.c om 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/googlegroupsrep ly/>

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/googlegroupsrep ly/>

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_someh ow(&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.c om 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/googlegroupsrep ly/>


Mar 28 '06 #10

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

Similar topics

13
4118
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 dynamic array of characters int length; //number of characters } String;
5
11456
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 which will detect the loop but I am stumped how one would remove this loop. Any ideas? typedef enum { FALSE, TRUE } bool;
7
3008
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 traversing the linked list using a pointer to cycle through. int Function(...){ node * p;
10
9781
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
8715
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
2761
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 this list like this... A->B->C->D->E->F->G->H->I->NULL thanks....
2
2340
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 to NULL)
2
6450
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 address. 2) What is meant by loop then? Thanks & Regards Sathish Kumar
0
8630
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.