Thread: Please explain help me!!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    8

    Please explain help me!!

    This is a code for inverting a linked link.But I just understand a little.Please explain it clearly help me.
    Code:
     node *inverting (list &first, list &last) 
                    {
                        node *tmp;
                        if (first != NULL)
                             {
                                if ((first)->next != NULL)
                                  {
     tmp= inverting (first->next,last);
                                      (last)->next = first;
                                      last = first;
                                      (last)->next = NULL;
                                     first = tmp;
                                   }
                                return first;
                              } 
                          else
                               return NULL;
                      }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well it might be easier to follow if it were indented properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    node *inverting (list &first, list &last) 
    {
         node *tmp;
         if (first != NULL)
         {
            if ((first)->next != NULL)
            {
               tmp= inverting (first->next,last);
               (last)->next = first;
               last = first;
               (last)->next = NULL;
               first = tmp;
            }
            return first;
         } 
         else
            return NULL;
    }
    Before i answer this question, have u got any idea of how recursion function works. If not look through it and then try looking at this code.

    ssharish2005

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by KySiRo
    Code:
    node *inverting (list &first, list &last)
    Since when does C support passing by reference?!

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by zacs7 View Post
    Since when does C support passing by reference?!
    Good question, realizing that this thread should now be moved to C++

    ssharish2005

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Agreed - moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> have u got any idea of how recursion function works
    It just calls the function again. To get an idea, copy and paste that function 10 times and give it 10 different names (append like inverting1, inverting2, inverting3, etc). Then, have inverting1 call inverting2, and have inverting2 call inverting3, and so on. Have the outside code that calls it call inverting1 to start it off.

    If you understand how the code works at that point (it is just one function calling another), then you understand recursion. The only difference is that the functions are only written once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  2. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  3. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  4. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM