C++ question

G

Guest

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;
}
 
A

Artie Gold

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
 
R

Russell Hanneken

Gianni said:
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.
 
D

Daniel Schüle

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

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,141
Messages
2,570,812
Members
47,357
Latest member
sitele8746

Latest Threads

Top