Thread: Reversing LinkList In Reverse Order Using Recursion

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Reversing LinkList In Reverse Order Using Recursion

    Hi All

    I was trying to reverse a linklist in reverse direction using the recursion. I was able to reverse n - 1 element but it is not printing the first one. Below is my code.

    Code:
    
    typedef struct linklist
    {
      int data;
      linklist *next;
    };
    
    void add(int data,linklist **node)
    {
      linklist *temp,*temp2;
    
      temp = (struct linklist *) malloc(sizeof(linklist));
      
      temp->data = data;
      temp->next = NULL;
    
      if (*node == NULL )
      {
        *node = temp;
        return;  
      }
    
      temp2 = *node;
    
      while ( temp2->next != NULL )
       temp2 = temp2->next;
    
      temp2->next =  temp;
      
    
    }
    
    void reverse_recur(linklist *node)
    {
      
      if (node != NULL )
      {
         node = node->next;
         reverse_recur(node);
    
       if ( node != NULL)       
          printf("\n Print In Reverse Order\n %d\n",node->data);
      
      
      }
      
    }
    
    
    int main()
    {
      
     linklist *node = NULL;
    
     add(1,&node);
     add(2,&node);
     add(3,&node);
     add(4,&node);
    
     print(&node);
    
    reverse_recur(node);
    This is happening since recursion is starting from second node, which is due to reason not printing the first one when recursion print values from stack once

    node != NULL
    Condition is met.

    Do anybody help to print the first node too.
    Currently I am using below statement for printing the first element;

    reverse_recur(node);
    printf("\n Print In Reverse Order\n %d\n",node->data);
    But this I don;t want. Any body has idea on this.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nickman
    I was trying to reverse a linklist in reverse direction using the recursion.
    If you reverse in reverse direction, doesn't that mean that you're keeping to the original?

    Quote Originally Posted by nickman
    This is happening since recursion is starting from second node, which is due to reason not printing the first one when recursion print values from stack once
    Your reverse_recur function appears to be more like "print_in_reverse_recursively". I suggest:
    Code:
    if node is not null:
        print the remaining nodes
        print the node
    That is, I expect only one test for a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Hi

    Did you mean this:

    Code:
    void reverse_recur(linklist *node)
    {
      linklist *temp = node;
      if (node != NULL )
      {
         node = node->next;
         reverse_recur(node);
       if ( node != NULL)       
          printf("\n Print In Reverse Order\n %d\n",node->data);
       else
          printf("\n Print In Reverse Order\n %d\n",node->data);
      }
      
      
    }
    But this also didn';t work as when else is executed it has NULL and program crashed.

    I have linklist of 1 2 3 4 and I want to print it as 4 3 2 1.
    Thanks

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    You can't do node->data on a NULL pointer -- so yes, this will crash. It'll get to the end of the list (node==NULL), it'll fail the if condition on line 4 and the function will return back to the previous recursive call. Since you've already done node=node->next, node is NULL.

    You need to get out of the recursion when you get to the end of the list, without trying to access node->data.
    Also, I don't think you should be doing node=node->next before the printf statements, as then you'll miss off the first element in the list (which I think you said in your original post).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nickman
    Did you mean this:
    Obviously not. You have two tests for a null pointer when I explicitly noted that I expected only one. It is so simple. All you have to do is to print the rest of the linked list in reverse, and then you print the current code, and you're done. What you are doing instead is to get to the next node, print the rest of the linked list in reverse (or at least you try), and then vainly attempt to print the current node that you no longer have a pointer to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks

    That I was doing in my code earlier.

    Code:
    reverse_recur(node);
    printf("\n Print In Reverse Order\n %d\n",temp->data);
    So basically I am printing 4 to 2 elements (1 2 3 4 as my link list elements) and then printing current.one which is 1.

    Is that you were saying?

    I want to print every node inside reverse_recur() function. as since recurision is starting from 2nd node, its not priniting the first element. Is there way I can do that?

    Thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nickman
    So basically I am printing 4 to 2 elements (1 2 3 4 as my link list elements) and then printing current.one which is 1.

    Is that you were saying?
    Yes, that's the idea.

    Quote Originally Posted by nickman
    I want to print every node inside reverse_recur() function. as since recurision is starting from 2nd node, its not priniting the first element. Is there way I can do that?
    There is: it is called iteration. But apparently your aim here is to use recursion, so you should only print the current node, letting another function handle the printing of the other nodes in reverse. It so happens that that other function is this function.

    Since you are only printing the current node, you should not write:
    Code:
    node = node->next;
    unless the only thing you do with node afterwards is to pass it to another function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  2. Replies: 30
    Last Post: 10-29-2005, 11:09 PM
  3. help with reversing something in a linklist
    By Axel in forum C Programming
    Replies: 43
    Last Post: 10-23-2005, 07:14 AM
  4. Reversing the order of the digits?
    By Mak in forum C Programming
    Replies: 15
    Last Post: 10-09-2003, 09:17 PM
  5. Reversing a set of numbers using recursion
    By Shikimo in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2002, 03:47 PM