Thread: Interative to Recursive help

  1. #1
    Registered User
    Join Date
    Jan 2011

    Interative to Recursive help

    How would I convert this iterative function to recursive? (link2 h) is the list.

    void printlist (link2 h) 
     link2 head = h;
     link move;
     int x;
     move = head->front;
     while (move != NULL) 
       x = move->dat;
       printf("%d ", x);
       move = move->next;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Decide what information you need to the function. Decide what to do to stop the recursion. Decide what piece you need to pass to the next time through the function. Stick all three of those together and see what you come up with.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Las Vegas
    Quzah's comments were right on. You're evidently using a linked list. How do you know you've reached the end of a linked list when using iteration? The next pointer is NULL. It should come as no surprise that is a logical thing to use as a base case to stop your recursion. The next pointer is also the one thing that "links" the whole linked list together from one node to another. It should be the focus of your quest to answer Quzah's other requirement - deciding what you need to pass to the next recursive call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  2. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  3. merge sort: recursive is fasfter than non-recursive
    By rio_cat in forum C Programming
    Replies: 8
    Last Post: 12-04-2006, 12:52 AM
  4. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM