Link to home
Start Free TrialLog in
Avatar of jxharding
jxharding

asked on

i made my first linklist but i dont know how to pass values in it to sumSquares, pls help!!!!!


#include <iostream>
using namespace std;

struct nodeType
{
 int info;
 nodeType *link;
};


nodeType* buildListForward()
{
  nodeType *first, *last,*newNode;
  int num;
  cout<<"Enter A list of integers ending with -999.\n";
  first = NULL;
 
  while (num != -999)
  {
  newNode = new nodeType;
  assert(newNode != NULL);
  newNode->info = num;
  newNode->link = NULL;

  if (first == NULL)
  {
        first = newNode;
        last = newNode;
  }
  else
  {
      last->link = newNode;
      last = newNode;
  }
  cin>> num;
 
  }//end while
 
  return first;
}//end buildlistforward

//int sumSquares1(nodeType<int> * list)
//{
//    if (list != NULL)
//        return (list->info*list->info) + sumSquares1(list->link);
//}



int main() {

 
 buildListForward();
  // how can i implement this buildlistforward so i can use sumSquares1?
  // pls show me as much as you can!
 

  return 0;
}


ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jxharding
jxharding

ASKER

will do tonight and reply tomorrow!
thank you very much!
the function sumSquares1(nodeType * list)  will return a garbage value when list will be equal with NULL, so use the following method, i just added a line ;)

int sumSquares1(nodeType * list) // no template argument, you aren't using templates
{
    if (list != NULL)
        return (list->info*list->info) + sumSquares1(list->link);
    else return 0;
}

~marchent~
Hi jxharding,

all the solutions are ok, but even with those correct solution you won't get the correct result as you made a mistake inside the buildListForward function

nodeType* buildListForward()
{
  nodeType *first, *last,*newNode;
  int num;
  cout<<"Enter A list of integers ending with -999.\n";
  first = NULL;
  cin>> num;//add this line here otherwise num is uninitialized<<---------------------------------------------------------------------

  while (num != -999)
  {
  newNode = new nodeType;
  assert(newNode != NULL);
  newNode->info = num;
  newNode->link = NULL;

  if (first == NULL)
  {
        first = newNode;
        last = newNode;
  }
  else
  {
      last->link = newNode;
      last = newNode;
  }
  cin>> num;
 
  }//end while
 
  return first;
}//end buildlistforward

Hope This Helps

Nafis
hi, found that error last night as well.
everything went well thanks,
the problem i have is mentioned in the // section that jkr wrote:
// no template argument, you aren't using templates

the problem im working on is using templates,
could someone point me in the right direction pls?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jxharding,

you should also then templatize the function

template<class T>
T sumSquares1(nodeType<T> * list)
{
    if (list != NULL)
        return (list->info*list->info) + sumSquares1(list->link);
   return 0;
}

although I prefer the iterative approach

template<class T>
T sumSquares1(nodeType<T> * list)
{
    nodeType<T> *ptr=list;
    T sum=0;
    while(ptr != NULL)
        {
            sum+=list->info*list->info;
            ptr=ptr->link;
        }
   return sum;
}

Hope This Helps

Nafis