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;
}


jxhardingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
That should IMO be like

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



int main() {

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

 int sum = sumSquares(node);
 
  return 0;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jxhardingAuthor Commented:
will do tonight and reply tomorrow!
thank you very much!
marchentCommented:
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~
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

nafis_devlprCommented:
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
jxhardingAuthor Commented:
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?

nafis_devlprCommented:
jxharding,

then you should declare your node this way
template<class T>
struct nodeType
    {
        T info;
        nodeType<T> *link;
    };

and inside the buildListForward function
nodeType<int> *first, *last,*newNode;
  int num;
  cout<<"Enter A list of integers ending with -999.\n";
  first = NULL;
  cin >> num;
  while (num != -999)
  {
  newNode = new nodeType<int>;
//  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


Hope This Helps

Nafis
nafis_devlprCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.