Thread: Recursive List Printing function

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    63

    Recursive List Printing function

    Hey fellas:

    //this is my list structure:

    struct batter_node{
    char batter_name[NAME_LENGTH]; //set to 20 for now
    batter_node *next;
    };

    batter_node *first//head pointer
    batter_node *current_batter;//my current node

    //this is my recursive function:

    void print_recurse(){
    current_batter=first;

    if((current_batter->next)==NULL){
    cout<<current_batter->batter_name<<endl;
    return;
    }else{
    current_batter=current_batter->next;//to move through my list
    print_recurse();//to call itself again
    cout<<current_batter->batter_name<<endl;//once returns print current node
    }
    }

    This keeps on core dumping me.. i can't see what i'm doing wrong.. Thanks for your time
    SS3X

  2. #2
    Unregistered
    Guest
    each time you call the function you set current_batter to first. Therefore you never move on down the line to get to the end. Set current_batter to first outside the function. Then send current_batter to the function as an argument. Within the function move current_batter to current_batter->next if current_batter-> != NULL and send the new current_batter to the next call of the function.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    so it should look like this? it compiles, but infinte loops, how do i pass a link node to a function i guess is my problem now..

    Code:
    //this is my list structure: 
    
    struct batter_node{ 
    char batter_name[NAME_LENGTH]; //set to 20 for now 
    batter_node *next; 
    }; 
    
    batter_node *first//head pointer 
    batter_node *current_batter;//my current node 
    
    //this is my recursive function: 
    //i increment current_batter=first;  in the main function and
    //call it with pint_recurse(current_batter);
    void print_recurse(batter_node* current_batter){ 
    
    
    if((current_batter->next)==NULL){ 
    cout<<current_batter->batter_name<<endl; 
    return; 
    }else{ 
    current_batter=current_batter->next;//to move through my list 
    print_recurse(current_batter);//to call itself again 
    cout<<current_batter->batter_name<<endl;//once returns print current node 
    } 
    }
    how should i initialize/declare this fuction?
    thanks
    SS3X

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void print_recurse(struct batter_node* pCurrentNode)
    { 
        if( pCurrentNode != NULL )
        {
            cout<<pCurrentNode->batter_name<<endl;
            print_recurse(pCurrentNode->next);
        } 
    }
    Call by passing it the pointer to the first node in the list.
    Code:
    print_recurse(first);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Unregistered
    Guest
    I would do this:


    void print_recurse(batter_node * current_batter)
    {
    if(current_batter != NULL )
    {
    //to print top to bottom uncomment the following line
    //cout << current_batter->batter_name << endl;

    //then move current_batter to next node
    current_batter = current_batter->next;

    //and send it to print_recurse for next level of recursion
    print_recurse(current_batter);

    //if you want to print out list of batter names backward
    //uncomment the following line
    //cout << current_batter->batter_name << endl;
    }

    in main() I would have this line just before the call to print_recurse:

    current_batter = first;

    and call the first level of recursion like this:

    print_recurse(current_batter);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM