Recursion

This is a discussion on Recursion within the C Programming forums, part of the General Programming Boards category; How do I modify code below to print a singly linked list where the data elements are int's from the ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    11

    Recursion

    How do I modify code below to print a singly linked list where the data elements are int's from the front to back and then the back to front again. i.e., if the elements contain 1, 3, 5 ,7 ,9, the function should print out 1, 3, 5, 7, 9, 7, 5, 3, 1.
    Code:
    #include <stdio.h>
    
    void recurse ( int count ) /* Each call gets its own copy of count */
    {
        printf( "%d\n", count );
        /* It is not necessary to increment count since each function's
           variables are separate (so each count will be initialized one greater)
         */
        recurse ( count + 1 );
    }
    
    int main()
    {
      recurse ( 1 ); /* First function call, so it starts at one */
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    #include <stdio.h>
    
    void recurse ( struct node  *count ) /* Each call gets its own copy of count */
    {
        
        printf( "%d\n", count->element );
      
        recurse ( count->next );  /*this is for front sequence,but this will go on forever,u need to have a check whether it has reached the end of the list*/
    }
    
    int main()
    {
      recurse ( q ); //q is the address of the first node.
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 10:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21