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


C++

Avatar of undefined
Last Comment
nafis_devlpr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jxharding

ASKER
will do tonight and reply tomorrow!
thank you very much!
marchent

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~
nafis_devlpr

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
jxharding

ASKER
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
nafis_devlpr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
nafis_devlpr

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