This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Question on tree-walking and mutually-recursive types
- From: Zack Weinberg <zack at codesourcery dot com>
- To: kenner at vlsi1 dot ultra dot nyu dot edu (Richard Kenner)
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 28 Jun 2004 20:48:01 -0700
- Subject: Re: Question on tree-walking and mutually-recursive types
- References: <10406290338.AA05436@vlsi1.ultra.nyu.edu>
kenner@vlsi1.ultra.nyu.edu (Richard Kenner) writes:
> How about the usual two-pointers collapsing loop algorithm to detect
> the cycle?
>
> Sorry, I don't recognize this reference (and neither does Google).
Like this:
struct S { struct S *next; };
bool
detect_cycle(struct S *list)
{
struct S *p, *q;
bool advance_q = false;
if (list == 0)
return false;
q = list;
p = list->next;
do
{
if (q == p)
return true;
if (advance_q)
q = q->next;
p = p->next;
advance_q = !advance_q;
}
while (p);
return false;
}
zw