linked list... - c++

This is a discussion on linked list... - c++ ; How can I find a loop in a single linked list? I thought about keeping a flag or traversing list more than once...but it will require another data structure to store all these things... Also if the list is having ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

linked list...

  1. Default linked list...

    How can I find a loop in a single linked list?
    I thought about keeping a flag or traversing list more than once...but
    it will require another data structure to store all these things...
    Also if the list is having many nodes then this won't ork..
    So is there a full proof solution?


  2. Default Re: linked list...

    On Jun 20, 10:11 pm, Shraddha <shraddhajosh...@> wrote:
    > How can I find a loop in a single linked list?
    > I thought about keeping a flag or traversing list more than once...but
    > it will require another data structure to store all these things...
    > Also if the list is having many nodes then this won't ork..
    > So is there a full proof solution?


    Try the Hare and Tortoise approach. Basically have 2 ptrs both
    pointing to the start of the list. Increment one pointer by 1 and
    another by 2. After each increment comapre if they are equal. If
    there is a loop the pointers would meet.


  3. Default Re: linked list...

    On Jun 21, 7:11 am, Shraddha <shraddhajosh...@> wrote:
    > How can I find a loop in a single linked list?
    > I thought about keeping a flag or traversing list more than once...but
    > it will require another data structure to store all these things...
    > Also if the list is having many nodes then this won't ork..


    If you can modify the nodes in the list, you can add a flag,
    visited, which is initialized false. Loop, setting the flag
    true, until you find the end of the list, or a node with the
    flag true. If you encountered the end of the list, there's no
    cycle, loop again resetting the flag false (for the next time).
    If you encountered a node with the flag true, there's a cycle.
    To reset the flags, loop from the beginning, until you encounter
    a node with the flag reset.

    If you can't modify the nodes in the list, you'll need some sort
    of look-aside cache with the addresses of the nodes already
    visited; if the list is long, this could require a lot of extra
    memory.

    --
    James Kanze (GABI Software, from CAI) email:james.kanze@
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


  4. Default Re: linked list...

    James Kanze wrote:
    >
    > If you can modify the nodes in the list, you can add a flag,
    > visited, which is initialized false. Loop, setting the flag
    > true, until you find the end of the list, or a node with the
    > flag true. If you encountered the end of the list, there's no
    > cycle, loop again resetting the flag false (for the next time).
    > If you encountered a node with the flag true, there's a cycle.
    > To reset the flags, loop from the beginning, until you encounter
    > a node with the flag reset.
    >


    You don't need to go through the loop a second time if you also keep a
    flag telling you the state that you left visited nodes in the last time.
    Before starting the main loop you toggle that flag's value, then go
    through nodes checking whether the node's flag is equal to the outer
    flag; if so, you've got a loop; if not, set the node's flag equal to the
    outer flag.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)

  5. Default Re: linked list...

    On 21 Jun, 06:19, Naresh Rautela <nraut...@> wrote:
    > On Jun 20, 10:11 pm, Shraddha <shraddhajosh...@> wrote:
    >
    > > How can I find a loop in a single linked list?
    > > I thought about keeping a flag or traversing list more than once...but
    > > it will require another data structure to store all these things...
    > > Also if the list is having many nodes then this won't ork..
    > > So is there a full proof solution?

    >
    > Try the Hare and Tortoise approach. Basically have 2 ptrs both
    > pointing to the start of the list. Increment one pointer by 1 and
    > another by 2. After each increment comapre if they are equal. If
    > there is a loop the pointers would meet.


    let me see if I understand this.

    so if I have a list

    1 1 3

    p1 -> 1
    p2 -> 1

    first increment
    p1 = p1+1
    p2 = p2+2
    p1 -> 1
    p2 -> 3
    p1 != p2

    and ... well you missed the loop.

    I guess you meant something like
    (assuming there are begin, end and there is ++
    operator that does something like p=p->next)

    for(p1 = start; p1 != end; ++p1)
    {
    p2 = p1;
    ++p2
    for(; p2 != end; ++p2)
    if(p1 == p2)
    ! there is a loop !
    }

    not very efficient.

    regards

    DS


  6. Default Re: linked list...

    On 21 Jun, 06:19, Naresh Rautela <nraut...@> wrote:
    > Try the Hare and Tortoise approach. Basically have 2 ptrs both
    > pointing to the start of thelist. Increment one pointer by 1 and
    > another by 2. After each increment comapre if they are equal. If
    > there is a loop the pointers would meet.


    On Jun 21, 1:20 pm, dasjotre <dasjo...@googlemail.com> wrote:
    > let me see if I understand this.
    > so if I have alist
    > 1 1 3


    That's not a loop. That's a repeated element,
    not a repeated node.

    A loop in a singly linked list is necessarily
    an infinite loop.

    > I guess you meant something like
    > for(p1 = start; p1 != end; ++p1)
    > {
    > p2 = p1;
    > ++p2
    > for(; p2 != end; ++p2)
    > if(p1 == p2)
    > ! there is a loop !
    >
    > }


    No, the classic technique is this:

    p1=start;
    p2=start;
    while(p1!=NULL && p2!=NULL) {
    if (p1==p2)
    return "there is a (infinite) loop";
    p1=p1+1;
    p2=p2+2;
    }
    return "there is no loop";

    If the linked list has no loop, then it is
    doing a traversal all the way to the end,
    which is O(n), and no worse than doing a element lookup.

    If the linked list has a loop, then p1 and p2
    will eventually meet (since no matter where the
    loop starts, the length before the loop,
    and the length inside the loop is always either odd,
    or even. And so either p1 and p2 meet during the first
    pass thru the loop, or during the second loop.
    So it is O(2n), which is O(n) also.

    - JT



  7. Default Re: linked list...

    On Jun 21, 1:33 pm, JT <jackt...@> wrote:
    > the classic technique is this:
    >
    > p1=start;
    > p2=start;
    > while(p1!=NULL && p2!=NULL) {
    > if (p1==p2)
    > return "there is a (infinite) loop";
    > p1=p1+1;
    > p2=p2+2;
    > }
    > return "there is no loop";


    My use of "+" in my pseudocode is misleading.
    Instead, this is the real code:

    class node {
    int element;
    node* next;
    }

    ....

    node *p1=start;
    node *p2=start;
    while(p1!=NULL && p2!=NULL) {
    if (p1==p2)
    return true; // Infinite loop found!
    p1=p1->next;
    p2=p2->next;
    if (p2!=NULL) p2=p2->next;
    }
    return false; // No loop



  8. Default Re: linked list...

    On Jun 21, 1:37 pm, JT <jackt...@> wrote:
    > node *p1=start;
    > node *p2=start;
    > while(p1!=NULL && p2!=NULL) {
    > if (p1==p2)
    > return true; // Infinite loop found!
    > p1=p1->next;
    > p2=p2->next;
    > if (p2!=NULL) p2=p2->next;
    > }
    > return false; // No loop


    Argg!!! I have egg on my face.



    I don't have my copy of the algorithm book with me,
    so I'm writing from memory. But there is a clear off-by-one
    error in my code.

    Let me try the 3rd time: (But surely, you get
    the idea by now. Whether my quick code has bug
    is irrelevant to whether this is a powerful classic technique)

    if (start==NULL)
    return false; // No loop, obviously
    node *p1=start;
    node *p2=start->next;
    while(p1!=NULL && p2!=NULL) {
    if (p1==p2)
    return true; // Infinite loop found!
    p1=p1->next;
    p2=p2->next;
    if (p2!=NULL) p2=p2->next;
    }
    return false; // No loop


  9. Default Re: linked list...

    On 2007-06-21 07:19, Naresh Rautela wrote:
    > On Jun 20, 10:11 pm, Shraddha <shraddhajosh...@> wrote:
    >> How can I find a loop in a single linked list?
    >> I thought about keeping a flag or traversing list more than once...but
    >> it will require another data structure to store all these things...
    >> Also if the list is having many nodes then this won't ork..
    >> So is there a full proof solution?

    >
    > Try the Hare and Tortoise approach. Basically have 2 ptrs both
    > pointing to the start of the list. Increment one pointer by 1 and
    > another by 2. After each increment comapre if they are equal. If
    > there is a loop the pointers would meet.


    What is it that I am missing here, wouldn't the simplest approach be to
    store a pointer p1 to the "beginning" and then use another pointer p2
    which walks the list, and for each new node p2 visits it tests to see if
    it's the one p1 points to. If there is a loop then you will discover
    that in N increments, and the same is true if there's no loop.

    If we look at the turtle and hare strategy we see that since the hare
    moves twice as fast as the turtle it will make two laps (if there's a
    loop) in the same time as the turtle makes one. So this means that we
    will find if there's a loop in 3N increments with this strategy and if
    there's no loop it will take 1.5N increments.

    --
    Erik Wikström

  10. Default Re: linked list...

    On Jun 21, 2:17 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
    > What is it that I am missing here, wouldn't the simplest approach be to
    > store a pointer p1 to the "beginning" and then use another pointer p2
    > which walks the list, and for each new node p2 visits it tests to see if
    > it's the one p1 points to. If there is a loop then you will discover
    > that in N increments, and the same is true if there's no loop.


    Your method works if the loop is a complete loop,
    but it doesn't work (and in fact goes into an infinite loop)
    if the loop begins half way.

    Say Node1.next == Node2
    and Node2.next == Node3
    and Node3.next == Node2

    Then the tortoise/hare method will find it.



+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Loop in a linked list
    By Application Development in forum C
    Replies: 2
    Last Post: 09-28-2007, 03:20 PM
  2. Linked-list example
    By Application Development in forum Fortran
    Replies: 0
    Last Post: 06-21-2007, 08:19 AM
  3. Double linked list
    By Application Development in forum C
    Replies: 0
    Last Post: 06-11-2007, 09:42 AM
  4. linked list
    By Application Development in forum C
    Replies: 0
    Last Post: 06-11-2007, 07:41 AM