Solved

Recursive call on linked list

Posted on 1998-07-30
10
221 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

630 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