Solved

Recursive call on linked list

Posted on 1998-07-30
10
197 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now