Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Recursive call on linked list

Posted on 1998-07-30
10
Medium Priority
?
225 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
Industry Leaders: 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 400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 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