Click to See Complete Forum and Search --> : recursive link list problem. Is it really wrong. my tutor cannot solve it


nomadass
March 14th, 2003, 02:57 AM
this program is used to reverse the link list using a recursive way.
//Hi, sir:
//about tut 6. question 3, my solution is:
void recursiveReverse(Node* &n){
node *temp1 = n, *temp2 = n;
static node* temp3 = n->getNext(), *temp4 = n;

//temp1, temp2 temp3 will traver the linked list;
// temp4 holds the head;

temp2 = temp3;
temp3 = temp3->getNext();

if (temp3 == NULL){
temp2->setNext(temp1);
temp4->setNext(0);
temp3 = temp4;
}

temp2->setNext(temp1);
recursiveReverse(temp2);
}

//this is different from the tutor's answer.
//and he said this one will not work;
//I wonder why? can you piont out for me
//what is more, he said the special case
//that only one node or two nodes, I think
//this solution no need consider that, for
// they are aready included by , I think

KabalProg
March 14th, 2003, 06:56 AM
Hi. Your program has several problems. Some of them:

1) It will never stop.
2) When you're on the last node of the list,

temp3=n->getNext(); // temp3 == NULL

later,

temp3=temp3->getNext(); // CRASH!!!

You can't call getNext() here, because temp3 points to NULL!


Hope this helps,

KabalProg

nomadass
March 14th, 2003, 07:59 AM
I think while temp3 = NULL. temp3->getNext(); will return NULL;


can anyone make some changes to my code if you find

anything wrong

thank you

Richard.J
March 14th, 2003, 10:28 AM
if it crashes, try debugging it and learn what mistakes you made. Seems to me a better advise than correcting your prog.

nomadass
March 15th, 2003, 03:56 AM
no, my program run on my pc.

but the tutor said i only get three marks.

so i wonder why.

the tutors are all stupid in Singapore,

so I try to find somebody for help.