recursivly "adding" a linked list

This is a discussion on recursivly "adding" a linked list within the C++ Programming forums, part of the General Programming Boards category; hey guys, in preperation for my midterm I'm doing a whole bunch of different exercises with linked lists and recursion. ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    recursivly "adding" a linked list

    hey guys, in preperation for my midterm I'm doing a whole bunch of different exercises with linked lists and recursion. The following is one which I'm having a bit of trouble with, but I think I'm on the right track;

    The question asks for a recursive function which takes a pointer to a singly linked list, where the elements are in order, (ie. 1 3 4 5). without creating a new list make the lists elements add up in the following way:
    Code:
    //original list:  2, 3, 4, 7
    //output list:   16 ,14 ,11 ,7
    I hope all you know what the question is asking for. Anyhow, here is the code I wrote. Can anyone tell me if it is right or wrong. By my logic it looks pretty good.
    Code:
    void suffix(cNode * L)
        {
           if(L == NULL || L->next == NULL)
                     return L;
           else
           {
             suffix(L->next);
             L->elem = L->elem + L->next->element;
           }
        }
    thanks,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    With the exception of a few syntactical errors, it looks right. Why don't you test it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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