By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

C++ question

P: n/a
Can someone briefly explain what this recursive algorithm involving circularly linked lists does? Thanks in advance.

bool nine(node *p, node *q)
{ if (p == NULL && q == NULL) return TRUE;
else if (p->data == q->data)
{ p=p->link; q=q->link; nine(p,q); }
else return FALSE;
}
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
so*****@somedomain.com.invalid wrote:
Can someone briefly explain what this recursive algorithm involving circularly linked lists does? Thanks in advance.

bool nine(node *p, node *q)
{ if (p == NULL && q == NULL) return TRUE;
else if (p->data == q->data)
{ p=p->link; q=q->link; nine(p,q); }
else return FALSE;
}


Has the semester started already?

At least you could have changed the name of the function -- erm, `nine'
pretty much gives it away, doesn't it?

Also, you really _should_ have provided the definition of `node'.

[Even worse, it doesn't even work. Do you know why?]

--ag

--
Artie Gold -- Austin, Texas

Jul 19 '05 #2

P: n/a
Gianni Mariani wrote:
so*****@somedomain.com.invalid wrote:
bool nine(node *p, node *q)
{ if (p == NULL && q == NULL) return TRUE;
else if (p->data == q->data)
{ p=p->link; q=q->link; nine(p,q); }
else return FALSE;
}


I suspect there is a bug.

This line:
{ p=p->link; q=q->link; nine(p,q); }

should probably read:

{ p=p->link; q=q->link; return nine(p,q); } //<<<< note the return

The compiler should have picked that one.


Another bug:

Suppose one list is shorter than the other. In that case, either p will
become NULL before q, or q will become NULL before p. Either way, the
first if condition will be false, and the else if will dereference a
NULL pointer.

--
Russell Hanneken
rh*******@pobox.com

Jul 19 '05 #3

P: n/a
hi
bool nine(node *p, node *q)
{ if (p == NULL && q == NULL) return TRUE;
else if (p->data == q->data)
{ p=p->link; q=q->link; nine(p,q); }
else return FALSE;
}


since you have C++ in your headline, why don't you use
true/false instead of TRUE/FALSE

?
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.