Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Recursive call on linked list

Posted on 1998-07-30
10
204 Views
Last Modified: 2012-05-04
Hi,

The problem Im having is that I need to traverse a linked list of integer data and total all data in the list and store it in a  node on the same list. Ive been working at this for awhile now and am at a loss.

I have know trouble doing this if Im using a separate int varible to store the total from the list.

Any help would be appreciated!

Here's the code that Ive done so far:

void Add(ptr* LList)
{
   Curr = LList;
   ptr* Total;
   Total = new data;

    if(Curr != NULL)
    {
       Total->data = Curr->data;
       Add(Curr->Next);
     }

   cout << Total->Data;
}

Thanks,

windyw
0
Comment
Question by:windyw
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1169107
one way you could do this is like this:

typedef struct  {
int val;
int total;
mystuff  *next;
mystuff   *prev;
} mystuff;

mystuff  *list;

inside the program,

while (list!=NULL)
{
   if (list->prev!=NULL)
   {
      list->total=list->prev->val;    /* Not first node */
      /* add another node here,  */
   }
   else  
   {
       list->total=list->val;  /* first node  */
      /* add node here */
   }

}
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1169108
This part should be:

  if (list->prev!=NULL)
       {
          list->total=list->prev->val + list->prev->total;    /* Not first node */
          /* add another node here,  */

       }


NOT:
  if (list->prev!=NULL)
       {
          list->total=list->prev->val;    /* Not first node */
          /* add another node here,  */
       }
0
 

Author Comment

by:windyw
ID: 1169109
It does need to be a recursive call!
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:nietod
ID: 1169110
Since this is for an assignment we cannot provide a complete solution.  To do so would be a violation of the customer service agreement we all agreed to.   But we can provide help.  We can answer SPECIFIC questions.  We can look at your work and ofter reasonable suggestions.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169111
You first problem is that a recursive design often involves 2 functions and you have only one (Add())  You need two functions.  The first function is the one that "starts the ball rolling".  It is not recursive.   it calls the 2nd function, which is recursive,  to perform a task (like calculate the total) and then when recursion stops, execution returns to the first function so it can use the result.  

Second problem: the recursive function must operate on a node in the list.  It will be passed a node (pointer) and will calculate the total of that node and all the nodes after it.

Third problem.  The recursive function must return a value.  In this case the value will be the total of the nodes in the list from the node it was called for to the end..

Does this get you started?
0
 

Author Comment

by:windyw
ID: 1169112
Adjusted points to 100
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169113
Do you have any questions or do you have some code to post that we can look at?
0
 

Author Comment

by:windyw
ID: 1169114
Ive been trying to accomplish this own my own for some time now without success and Iam not wanting any one to do this for me. Iam simply wanting a suggested approach to solving the problem.
0
 
LVL 10

Accepted Solution

by:
rbr earned 100 total points
ID: 1169115
Recursive way would not be the bestway for this problem. I can give you 2 solutions.
1.) non recursive
void Add(ptr* LList)
    {
       Curr = LList;
       ptr* Total;
       Total = new data;

        while(Curr != NULL)
        {
           Total->data += Curr->data;
           Curr=Curr->Next;
         }

       cout << Total->Data;
    }

2.) Recursive, which is more complicate
void Add(ptr* LList,ptr *Total)
    {
       Curr = LList;
       if (Total == NULL)
            Total = new data;


        if(Curr != NULL)
        {
           Total->data += Curr->data;
           Add(Curr->Next,Total);
         }

       cout << Total->Data;
    }
This will print total quite as often as the depth of your list.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169116
rbr, this is for an assigment!!!!  You can be removed from EE for unethical behavior.  Think before you answer.

windyw, the recursive solution rbr presents is really not the best way.  You are better of using two functions, one of which is recursive and does the calculation. and the other which prints the results..  You can then return values usign the regular "return" statement rather than having to pass them back with a pointer.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question