CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2003
    Posts
    3

    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

  2. #2
    Join Date
    Oct 2001
    Location
    Venezuela
    Posts
    35
    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

  3. #3
    Join Date
    Mar 2003
    Posts
    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

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158
    if it crashes, try debugging it and learn what mistakes you made. Seems to me a better advise than correcting your prog.

  5. #5
    Join Date
    Mar 2003
    Posts
    3

    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
  •  





Click Here to Expand Forum to Full Width

Featured