|
-
March 14th, 2003, 03:57 AM
#1
recursive link list problem. Is it really wrong. my tutor cannot solve it
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
-
March 14th, 2003, 07:56 AM
#2
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
-
March 14th, 2003, 08:59 AM
#3
thank you for you reply
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
-
March 14th, 2003, 11:28 AM
#4
if it crashes, try debugging it and learn what mistakes you made. Seems to me a better advise than correcting your prog.
-
March 15th, 2003, 04:56 AM
#5
thanks
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|